n java.lang包包含了主要的Java语言支持类, 它是Java语言编程设计的基础。
n 作为Java语言中所有类的祖先Object类,就定义在Java.lang包中.
n 另外还包含了包装类、String类、多线程支持类、Math类、System类等。
Object 类
n Object 类是所有Java类的最终祖先,如果一个类在声明时没有包含extends关键词,那么这个类直接继承Object类。
n (1)equals(Object obj)
n (2)notify()
n (3)nofityAll()
n (4)wait()
n (5)toString():返回当前对象的字符串表示,格式为“类名@对象的16进制哈希码”。许多类,如String、StringBuffer和包装类都覆盖了toString()方法,返回具有实际意义的内容。
Object 类的toString()方法
n 当System.out.println()方法的参数为Object类型,println()方法会自动先调用Object对象的toString()方法,然后打印toString()方法返回的字符串。
System.out.println(new Object().toString()); //打印java.lang.Object@273d3c System.out.println(new Integer(100).toString()); //打印100 System.out.println(new String("123").toString()); //打印123 System.out.println(new StringBuffer("123456").toString()); //打印123456
以上代码等价于:
System.out.println(new Object()); System.out.println(new Integer(100)); System.out.println(new String("123")); System.out.println(new StringBuffer("123456"));
;
String类
n indexOf()和lastIndexOf():在字符串中检索特定字符或子字符串,indexOf()方法从字符串的首位开始查找,而lastIndexOf()方法从字符串的末尾开始查找。如果找到,则返回匹配成功的位置,如果没有找到,则返回-1。
String str="HelloHelloHello"; //查找字符'e'第一次在str中出现的位置 System.out.println(str.indexOf('e')); //打印1 //查找字符'e'从位置2开始第一次在str中出现的位置 System.out.println(str.indexOf('e',2)); //打印6 //查找字符'e'在str中最后一次出现的位置 System.out.println(str.lastIndexOf('e')); //打印11 //查找字符串"ello"从位置2开始第一次在str中出现的位置 System.out.println(str.indexOf("ello",2)); //打印6 //查找字符串"Ello"第一次在str中出现的位置 System.out.println(str.indexOf("Ello")); //打印-1
StringBuffer类
n length():返回字符串的字符个数,与String类的length()用法相同。
n append():向缓冲区内添加新的字符串,例如:
StringBuffer sb=new StringBuffer(); sb.append("Hello"); sb.append("World"); System.out.println(sb); //打印HelloWorld n substring():用法与String类的substring()方法相同。 n insert( int offset, String str):在字符串中的offset位置插入字符串str,例如: StringBuffer sb=new StringBuffer("0456"); sb.insert(1,"123"); System.out.println(sb); //打印0123456
比较String类和StringBuffer类
n String类不可编辑
n StringBuffer类可编辑
String s1=new String(“abc”); s1.concat(“def”); StringBuffer sb1=new StringBuffer(“abc”); sb1.append(“def”); System.out.println(s1); System.out.println(sb1);
基本类型的数据分别对应的包装类为:
boolean Boolean
byte Byte
char Charactor
short Short
int Integer
long Long
float Float
double Double
包装类的层次结构
包装类的构造方法
Integer i=new Integer(1); Float f=new Float( 1.0f); Double d=new Double(1.0); Integer i=new Integer("123"); Double d=new Double("123.45D"); Float f=new Float("123.45F");
包装类的常用方法
n 除Character类和Boolean类以外,包装类都有parseXXX(String str)静态方法,把字符串转变为相应的基本类型的数据(XXX表示基本类型的名称)。参数str不能为null,而且该字符串必须可以解析为相应的基本类型的数据,否则虽然编译会通过,运行时会抛出NumberFormatException。例如:
int i=Integer.parseInt("123"); //合法,i=123 double d=Double.parseDouble("abc"); //抛出NumberFormatException
包装类的用法举例
int a=Integer.parseInt(“123”); int a1=new Integer(“123”).intValue(); double d=Double.parseDouble(“234.567”); double d1=new Double (“234.567”).doubleValue();
Math类
n Math类是final的。
n Math的构造方法是private的。
n Math提供的方法都是静态的。
Math类的主要方法
n abs():返回绝对值。
n ceil():返回大于等于参数的最小整数。
n floor():返回小于等于参数的最大整数。
n max():返回两个参数的较大值。
n min():返回两个参数的较小值。
n random():返回0.0和1.0 之间的double类型的随机数,包括0.0,但不包括1.0。
n round():返回四舍五入的整数值。
n sin():正弦函数。
n cos():余弦函数。
n tan():正切函数。
n exp():返回自然对数的幂。
n sqrt():平方根函数。
n pow():幂运算。
Math类的常用方法
System.out.println(Math.round(3.3)); //打印3 System.out.println(Math.round(-3.3)); //打印-3 System.out.println(Math.ceil(3.3)); //打印4.0 System.out.println(Math.ceil(-3.3)); //打印-3.0 System.out.println(Math.floor(3.3)); //打印3.0 System.out.println(Math.floor(-3.3)); //打印-4.0