Number类:
Java中为每个内置数据类型定义了封装类:
Integer Long Byte Double Float Short
这些类都是抽象类Number的子类
示例:
Integer m = 10; Integer n = 20; System.out.println(m+n); |
Number类的类型转换方法:
方法 | 描述 |
byteValue() | 以 byte 形式返回指定的数值。 |
doubleValue() | 以 double 形式返回指定的数值。 |
floatValue() | 以 float 形式返回指定的数值。 |
intValue() | 以 int 形式返回指定的数值。 |
longValue() | 以 long 形式返回指定的数值。 |
shortValue() | 以 short 形式返回指定的数值。 |
其他Number类相关方法示例:
// equals Integer m = 10; Short n = 10; boolean compareResult = m.equals(n); // false
// valueOf // static Integer valueOf(int i) // static Integer valueOf(String s) // static Integer valueOf(String s, int radix) Integer a = Integer.valueOf("10"); // 10 Float b = Float.valueOf("3.1415"); // 3.1415 Double c = Double.valueOf(10); // 10.0 Integer d = Integer.valueOf("1001000", 2); // 72
// parseType() // static int parseInt(String s) // static int parseInt(String s, int radix) Integer e = Integer.parseInt("123"); // 123 Double f = Double.parseDouble("1.2345"); // 1.2345 Boolean g = Boolean.parseBoolean("true"); // true Integer h = Integer.parseInt("7f0fee", 16); // 8327150
// compare, used to compare same type Integer i = 10; Integer j = 9; int cr = i.compareTo(j); // 1 j++; cr = i.compareTo(j); // 0 j++; cr = i.compareTo(j); // -1
// toString // String toString() // static String toString(int i) Double k = 10.12345; String s = k.toString(); // "10.12345" String s1 = Double.toString(k); // "10.12345" |
Math类:
Math类中提供了大量静态方法用于数学计算,例如:
方法 | 描述 |
abs() | 返回参数的绝对值。例如:Math.abs(-10); // 10 |
ceil() | 对整形变量向左取整,返回类型为double型。例如:Math.cail(3.1415); // 4.0 |
floor() | 对整型变量向右取整。返回类型为double类型。例如:Math.floor(3.1415); // 3.0 |
rint() | 返回与参数最接近的整数。返回类型为double。例如:Math.rint(1.5); // 2.0 |
round() | 返回一个最接近的int、long型值。例如:Math.round(1.5); // 2 |
min() | 返回两个参数中的最小值。例如:Math.min(1, 2); // 1 |
max() | 返回两个参数中的最大值。例如:Math.max(1, 2); // 2 |
exp() | 返回自然数底数e的参数次方。例如:Math.exp(2); // e*e = 7.38905609893065 |
log() | 返回参数的自然数底数的对数值。例如:Math.log(Math.exp(2)); // 2.0 |
pow() | 返回第一个参数的第二个参数次方。例如: Math.pow(10, 3); // 1000.0 |
sqrt() | 求参数的算术平方根。例如:Math.sqrt(0.36); // 0.6 |
sin() | 求指定double类型参数的正弦值。例如:Math.sin(Math.toRadians(30)); // 0.5 |
cos() | 求指定double类型参数的余弦值。例如:Math.cos(Math.toRadians(60)); // 0.5 |
tan() | 求指定double类型参数的正切值。 |
asin() | 求指定double类型参数的反正弦值。 |
acos() | 求指定double类型参数的反余弦值。 |
atan() | 求指定double类型参数的反正切值。 |
atan2() | 将笛卡尔坐标转换为极坐标,并返回极坐标的角度值。 |
toDegrees() | 将参数转化为角度。 |
toRadians() | 将角度转换为弧度。 |
random() | 返回一个取值范围在 [0.0, 1.0]的随机数。例如:Math.round(Math.random() * 100); // [0, 100] |
Character类:
用于封装基本类型char,例如:
Character c1 = new Character('a'); Character c2 = 'a'; |
Character类的常用方法:
方法 | 描述 |
isLetter() | 判断是否是一个字母 |
isDigit() | 判断是否是一个数字字符 |
isWhitespace() | 判断是否是一个空格 |
isUpperCase() | 判断是否是大写字母 |
isLowerCase() | 判断是否是小写字母 |
toUpperCase() | 返回指定字母的大写形式 |
toLowerCase() | 返回指定字母的小写形式 |
toString() | 返回字符的字符串形式,字符串的长度仅为1 |
String类:
初始化String:
String a = "Hello world"; char[] chs = {'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}; String b = new String(chs); int x = 10, y = 20; String c = String.format("%s + %s = %d", "x", "y", x+y); |
String类的常用方法:
方法 | 描述 |
char charAt(int index) | 返回指定索引处的 char 值。 例如:"Hello world".charAt(1); // 'e' |
int compareTo(Object o) | 把这个字符串和另一个对象比较。 例如:"abc".compareTo("bcd"); // -1 |
int compareTo(String anotherString) | 按字典顺序比较两个字符串。 例如:"abc".compareTo("bcd"); // -1 |
int compareToIgnoreCase(String str) | 按字典顺序比较两个字符串,忽略大小写。 例如:"ab".compareTo("AC"); // 0 |
String concat(String str) | 将指定字符串连接到此字符串的结尾。 例如:"abc".concat("def"); // "abcdef" |
boolean contentEquals(StringBuffer sb) | 当且仅当字符串与指定的StringBuffer有相同顺序的字符时候返回真。 例如:"AbcD".contentEquals("AbcD"); // true |
static String copyValueOf(char[] data) | 返回指定数组中表示该字符序列的 String。 例如: char[] a = {'a', 'b', 'c'}; String.copyValueOf(a); // "abc" |
static String copyValueOf(char[] data, int offset, int count) | 返回指定数组中表示该字符序列的 String,offset和count指定了起始字符的位置以及返回的字符数量。 例如: char[] b = {'a', 'b', 'c', 'd', 'e'}; String.copyValueOf(b, 1, 3); // "bcd" |
boolean endsWith(String suffix) | 测试此字符串是否以指定的后缀结束。例如:"Hello".endsWith("llo"); // true |
boolean equals(Object anObject) | 将此字符串与指定的对象比较。 例如:"Hello".equals("hello"); // false |
boolean equalsIgnoreCase(String anotherString) | 将此 String 与另一个 String 比较,不考虑大小写。 例如:"Hello".equalsIgnoreCase("hello"); // true |
byte[] getBytes() | 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 例如:byte[] buf = "Hello".getBytes(); |
byte[] getBytes(String charsetName) | 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 例如:byte[] unicodeBuf = "Hello".getBytes("UTF-8"); |
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) | 将字符从此字符串复制到目标字符数组。 例如: char[] c = {'1', '2', '3', '4', '5'}; "Hello".getChars(2, 4, c, 2); // c = {'1', '2', 'l', 'l', '5'},即将"Hello"索引在[2, 4)范围内的字符(即"ll")依次拷贝到c中,起始位置是2(即从数字3开始替换) |
int hashCode() | 返回此字符串的哈希码。例如:"Hello".hashCode(); // 69609650 |
int indexOf(char ch) | 返回指定字符在此字符串中第一次出现处的索引。 例如:"Helloworld".indexOf('w'); // 5 |
int indexOf(char ch, int fromIndex) | 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。 例如:"Helloworld".indexOf('o', 5); // 6 |
int indexOf(String str) | 返回指定子字符串在此字符串中第一次出现处的索引。 例如:"123abc123abc".indexOf("abc"); // 3 |
int indexOf(String str, int fromIndex) | 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。 例如:"123abc123abc".indexOf("abc", 4); // 9 |
String intern() | 返回字符串对象的规范化表示形式。对于任意两个字符串 s 和 t,当且仅当 s.equals(t)为 true 时,s.intern() == t.intern()才为 true。 |
int lastIndexOf(char ch) | 返回指定字符在此字符串中最后一次出现处的索引。使用方法与indexOf类似。 |
int lastIndexOf(char ch, int fromIndex) | 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。使用方法与indexOf类似。 |
int lastIndexOf(String str) | 返回指定子字符串在此字符串中最右边出现处的索引。使用方法与indexOf类似。 |
int lastIndexOf(String str, int fromIndex) | 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。使用方法与indexOf类似。 |
int length() | 返回此字符串的长度。 例如:"Hello".length(); // 5 |
boolean matches(String regex) | 比较此字符串是否匹配给定的正则表达式。 例如:"someone@outlook.com".matches("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$"); // true |
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) | 测试两个字符串区域是否相等。 例如:"This is a test string...".regionMatches(true, 10, "I've just finished my TEST...", 22, 4); // true |
boolean regionMatches(int toffset, String other, int ooffset, int len) | 测试两个字符串区域是否相等。 例如:"This is a test string...".regionMatches(10, "I've just finished my TEST...", 22, 4); // false |
String replace(char oldChar, char newChar) | 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。 例如:"Hello world".replace('l', 'm'); // Hemmo wormd |
String replaceAll(String regex, String replacement) | 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。 例如: String emailRegex = "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)"; "My email is daniel@outlook.com , you could send email to daniel@outlook.com if you want to contact me.".replaceAll(emailRegex, "daniel@gmail.com"); // "My email is daniel@gmail.com , you could send email to daniel@gmail.com if you want to contact me." |
String replaceFirst(String regex, String replacement) | 使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。机制类似于replaceAll,但是仅替换第一个。 |
String[] split(String regex) | 根据给定正则表达式的匹配拆分此字符串。 例如: String rawNames = "daniel#peter#steve,kyle,bruce#tom,bob"; String spliter = "[#,]"; String[] names = rawNames.split(spliter); for (String name : names) { System.out.println(name); } |
String[] split(String regex, int limit) | 根据匹配给定的正则表达式来拆分此字符串。limit参数指定分割的次数,如果达到这个次数,后面的子字符串就不再分割了。 |
boolean startsWith(String prefix) | 检查此字符串是否以指定的前缀开始。 例如:"Hello".startsWith("He"); // true |
boolean startsWith(String prefix, int toffset) | 检查此字符串从指定索引开始的子字符串是否以指定前缀开始。 例如:" Hello".startsWith("He", 2); // true |
CharSequence subSequence(int beginIndex, int endIndex) | 返回一个新的字符序列,它是此序列的一个子序列。 例如:CharSequence cs = "Hello world".subSequence(6, 11); // "world" |
String substring(int beginIndex) | 返回一个新的字符串,它是此字符串的一个子字符串。 例如:String subStr = "Hello world".substring(6); // world |
String substring(int beginIndex, int endIndex) | 返回一个新字符串,它是此字符串的一个子字符串。 例如:String subStr = "Hello world".substring(6, 10); // worl |
char[] toCharArray() | 将此字符串转换为一个新的字符数组。 例如:char[] a = "Hello".toCharArray(); |
String toLowerCase() | 使用默认语言环境的规则将此 String 中的所有字符都转换为小写。 例如:"Hello".toLowerCase(); // "hello" |
String toLowerCase(Locale locale) | 使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。 例如: Locale defloc = Locale.getDefault(); "Hello world".toLowerCase(defloc); // hello world |
String toString() | 返回此对象本身。 |
String toUpperCase() | 使用默认语言环境的规则将此 String 中的所有字符都转换为大写。用法同toLowerCase()。 |
String toUpperCase(Locale locale) | 使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。用法同toLowerCase()。 |
String trim() | 返回字符串的副本,忽略前导空白和尾部空白。 例如:" abc ".trim() // "abc" |
static String valueOf(primitiveType x) | 返回给定基本类型x参数的字符串表示形式。 例如:char[] a = {'a', 'b', 'c'}; String.valueOf(a); // "abc" |
StringBuffer 和StringBuilder类:
由于String对象的值在初始化后不能修改,所以为了避免堆栈中生成大量对象,可以使用StringBuffer和StringBuilder来生成字符串,StringBuilder相对速度较快,StringBuffer则是线程安全的。
常用方法:
方法 | 描述 |
StringBuffer/StringBuilder append(String s) | 将指定的字符串追加到此字符序列。 |
StringBuffer/StringBuilder reverse() | 将此字符序列反转。 |
delete(int start, int end) | 移除此序列的子字符串中的字符。 |
insert(int offset, <type> obj) | 将 offset 参数指定的位置插入obj的字符串表示(toString())。 |
replace(int start, int end, String str) | 使用给定 String 中的字符替换此序列的子字符串中的字符。 |
int capacity() | 返回当前容量。 |
void ensureCapacity(int minimumCapacity) | 确保容量至少等于指定的最小值。 |
除了上面的方法,下面的方法与String类相同:
方法 | 描述 |
char charAt(int index) | 返回此序列中指定索引处的 char 值。 |
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) | 将字符从此序列复制到目标字符数组 dst。 |
int indexOf(String str) | 返回第一次出现的指定子字符串在该字符串中的索引。 |
int indexOf(String str, int fromIndex) | 从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。 |
int lastIndexOf(String str) | 返回最右边出现的指定子字符串在此字符串中的索引。 |
int lastIndexOf(String str, int fromIndex) | 返回最后一次出现的指定子字符串在此字符串中的索引。 |
int length() | 返回长度。 |
void setCharAt(int index, char ch) | 将给定索引处的字符设置为 ch。 |
void setLength(int newLength) | 设置字符序列的长度。 |
CharSequence subSequence(int start, int end) | 返回一个新的字符序列,该字符序列是此序列的子序列。 |
String substring(int start) | 返回一个新的 String,它包含此字符序列当前所包含的字符子序列。 |
String substring(int start, int end) | 返回一个新的 String,它包含此序列当前所包含的字符子序列。 |
String toString() | 返回此序列中数据的字符串表示形式。 |