java.lang中的基础类库

java.lang包中的类和接口

类名 功能
Object类 所有类的根,为子类提供对对象操作的基本方法
Math类 提供一组数学函数和常数
Comparable可比较接口 约定对象比较大小的方法
Byte、Short、Integer、Long、Float、Double、Character、Boolean 基本数据的包装类,封装基本数据类型
String、StringBuffer、StringBuilder字符串类 分别提供常量字符串与变量字符串的操作方法
System系统类 声明标准输入/输出常量,有终止程序运行、复制数组、获得当前时间、系统属性等方法
Class类操作类 提供类名、父类及类所在的包等信息
Error错误类和Exception异常类 Exception类处理异常,Error类处理错误
Cloneable克隆接口 支持类的克隆,标记接口
Runtime运行时类 提供访问系统运行时环境

注:StringBuffer和StringBuilder区别是StringBuilder的方法不是线程安全的,StringBuilder相对于StringBuffer有速度优势,不要求线程安全的情况下采用StringBuilder

Class操作类

@Test
    public void testClass() {
        Object obj = new Player();
        System.out.println(obj.getClass());
        System.out.println(obj.toString());
        System.out.println(obj.hashCode());
        System.out.println(obj.equals(obj));
    }
/* 
class testing.Player
testing.Player@4411d970
1142020464
true
*/

Math数学类

@Test
    public void testMath() {
        System.out.println(Math.E);
        System.out.println(Math.PI);
        System.out.println(Math.abs(Math.PI));
        System.out.println(Math.random());
        System.out.println(Math.pow(2.0d, 3.0d));
        System.out.println(Math.sqrt(9.00));
        System.out.println(Math.sin(Math.PI / 2.0d));
        System.out.println(Math.cos(Math.PI));
    }
/* 
2.718281828459045
3.141592653589793
3.141592653589793
0.17237703882930722
8.0
3.0
1.0
-1.0
*/

Comparable可比较接口

    @Test
    public void testComparable() {
        Player 伞兵一号 = new Player("卢本伟");
        Player 伞兵二号 = new Player("PDD");
        System.out.println(伞兵一号.compareTo(伞兵二号));
    }
/* 
21266(卢与P的code相差了21266,如果把卢改为Q,输出为1)
*/

Integer类

    @Test
    public void testInteger() {
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.parseInt("996"));
        System.out.println(Integer.parseInt("1000", 2));//radix是进制
        System.out.println(new Integer(996).toString());
        System.out.println(Integer.toBinaryString(8));
        System.out.println(Integer.toOctalString(8));
        System.out.println(Integer.toHexString(16));
        System.out.println(new Integer(996).equals(new Integer(007)));
        System.out.println(new Integer(996).compareTo(new Integer(007)));
    }
/* 
2147483647
-2147483648
996
8
996
1000
10
10
false
1
*/

Double浮点数类

    @Test
    public void testDouble() {
        System.out.println(new Double("666.0d").doubleValue());
        System.out.println(Double.parseDouble("666.0d"));
    }
/* 
666.0
666.0
*/

String类字符串

    @Test
    public void testString() {
        String s1 = new String("aaa");
        System.out.println(new String(s1));
        System.out.println(new String().isEmpty());
        String s2 = s1.toUpperCase();
        System.out.println(s2);
        System.out.println(s1);
        System.out.println(s1.equalsIgnoreCase(s2));
        System.out.println(s1.compareTo(s2));
        System.out.println(s1.startsWith("aa"));
        System.out.println(s2.endsWith("AA"));
        String s3 = new String("abcdabcd");
        System.out.println(s3.indexOf('a'));//indexOf(int ch),ch指某一个字符
        System.out.println(s3.indexOf('a', 3));
        //indexOf(int ch, int begin),返回的ch在String中的绝对下标而不是相对于fromindex的下标
        System.out.println(s3.indexOf("a"));
    }
/*output:
aaa
true
AAA
aaa
true
32
true
true
0
4
0
*/

Class类和Package类

    @Test
    public void testClass() {
        Player player = new Player();
        Class c1 = player.getClass();
        System.out.println(c1.getName());
        System.out.println(c1.getSuperclass());
        System.out.println(c1.getPackage());
    }
/* 
testing.Player
class testing.Person
package testing
*/

System类

@Test
    public void testSystem() {
        System.out.println(System.currentTimeMillis());
        System.out.println(System.getProperties());
        int a[] = {1, 2, 3, 4};
        int b[] = new int[10];
        System.arraycopy(a, 0, b, 0, 3);
        for (int i : b) {
            System.out.println(i);
        }
        System.exit(0);
    }
/*
1598771623193
{sun.desktop=windows, ...}
1
2
3
0
0
0
0
0
0
0

*/
原文地址:https://www.cnblogs.com/Glov/p/13589095.html