Number类
装箱:将内置数据类型作为包装类对象使用;拆箱:相反
public class test{ public static void main(String args[]) { // box int into Integer Obeject Integer x = 5; // unbox Integer Object to int x = x + 10; System.out.println(x); } }
// functions: // convert to xxx type and return value xxxValue() compareTo() equals() // 返回Integer对象指定的内置数据类型 valueOf() toString() // 将字符串解析为int型 parseInt() // 对整型变量向左取整,返回类型为double ceil() // 对整型变量向右取整,返回类型为double floor() // 返回一个最接近int、long型的值 round() min() max() exp() log() abs() pow() sqrt() sin() cos() tan() asin() acos() atan() atan2() toDegrees() toRadians() // 返回一个随机数 random()
Character类
Character ch = 'a'; // functions: isLetter() isDigit() isWhitespace() isUpperCase() isLowerCase() toUpperCase() toLowerCase() toString()
String类
一旦创建,值就无法改变。
public class test{ public static void main(String args[]) { String str1 = "test"; char[] temp = {'t','e','s','t','2'}; // use char[] for initialization String str2 = new String(temp); System.out.println("the length of str2: " + str2.length()); // formated output. or use printf String fs; fs = String.format("The value of the float variable is " + "%f, while the value of the integer " + "variable is %d, and the string " + "is %s", 0.7f, 7, str2); System.out.println(fs); } }
StringBuffer类和StringBuilder类
由于StringBuilder相较于StringBuffer有速度优势,所以多数情况下建议使用StringBuilder类。然而在应用程序要求线程安全的情况下,则必须使用StringBuffer类。
public StringBuffer append(String s) //将指定的字符串追加到此字符序列。 public StringBuffer reverse() // 将此字符序列用其反转形式取代。 public delete(int start, int end) // 移除此序列的子字符串中的字符。 public insert(int offset, int i) //将 int 参数的字符串表示形式插入此序列中。 replace(int start, int end, String str) //使用给定 String 中的字符替换此序列的子字符串中的字符。
Array类
// for functions used below import java.util.Arrays; public class test{ public static void main(String args[]) { int[] arr = {1, 9, 3, 6, 4}; // sort by increasing order Arrays.sort(arr); // return index // not find, return -1*insertIndex-1. so it's -5 System.out.println(Arrays.binarySearch(arr, 8)); int[] arr2 = {0, 4}; // arr2 now is {6, 6}。but arr2 can't be blank before fill Arrays.fill(arr2, 6); for(int num:arr2) System.out.println("* " + num); // compare two arrays System.out.println(Arrays.equals(arr, arr2)); } }