1. 封装类、装箱、拆箱
封装类:基本类型对应的类类型
Number类的子类有Byte, Short, Integer, Long, Float, Double
基本类型->封装类: int i = 1; Integer intVal = new Integer(i), 自动装箱: Integer intVal = i;
封装类型—>基本类型: int i = intVal.intValue(); 自动拆箱: int i = intVal;
最大(小)值: Integer.MAX_VALUE, Integer.MIN_VALUE.
2. 数字与字符串
数字转字符串: int i = 1; String s = String.valueOf(i); 或 Integer it = i; String s = it.toString();
字符串转数字: String s = "10"; int i = Integer.parseInt(s);
3. 数学运算:
java.lang.Math提供常用数学运算方法(静态)
四舍五入: Math.round()
随机浮点数[0,1): Math.random()
开方: Math.sqrt()
幂: Math.pow(a, b);
pi: Math.PI
自然常数e: Math.E
4. 格式化输出
%s, %d, %n: String strFormat = "%s ... %d ... %n"; System.out.printf(strFormat, a, b, c);或 System.out.format(strFormat, a, b, c);
%,md, %,.nf, %-md。(","为千分位分隔符)
4. char
char -> Character
char c = 'a'; Character ch = c; c = ch;
Character.isLetter(), isDigiter(), isWhitespace(), isUpperCase(), isLowerCase(), toUpperCase(), toLowerCase(); toString();
char->String; String s = Character.toString();
不能直接把char转成String.
5. 字符串
String s = "";
String s = new String("");
char[] cArr = new char[] {};
String s = new String(cArr);
String s = s1 + s2;
String被final修饰,不能继承。
String对象内容不能改变。
String.format(strFormat, a, b, c)
字符串长度: s.length()
方法:
charAt(), toCharArray(), subString(), split(), trim(), toLowerCase(), toUpperCase(), indexOf(), lastIndexOf(), contains(), replaceAll(), replaceFirst();
是否是同一对象: ==
String s1 = "abc"; String s2 = new String(s1); s1 == s2->false;
String s1 = "abc"; String s2 = "abc"; s1 == s2->true;
s1.equals(s2); s1.equalsIgnoreCase(s2); 内容是否相同
s1.startsWith(), endsWith();
6. StringBuffer
可变长字符串
append(), delete();, insert(), reverse();
Stirng s = "abc"; StringBuffer sb = new StringBuffer(s); sb.append(" def");
sb.delete();
length:长度
capacity: 分配的空间。当第一次分配空间不够时,另分配空间,将原数据拷贝到新空间。

690

被折叠的 条评论
为什么被折叠?



