熟悉java.lang包

 

java.lang包作为java语言的核心语言包,是唯一一个可以不通过import语句导入而可以直接使用的java包。

 

几个关键的类包括:

一-基本数据类型对应的接口和类

Comparable接口:以下8个基本数据类型都实现了Comparable接口,此借口就一个方法public int compareTo(T o)

Number类:public abstract class Number implements java.io.Serializable

Byte,Short,Integer,Long,Float,Double6个类都是该类的子类

提供了6个方法:public abstract int intValue();

                            public abstract long longValue();

                            public abstract float floatValue();

                            public abstract double doubleValue();

                            public byte byteValue(){}

                            public short shortValue(){}

-------------------------------------------------------------------------------------------------------------------

Boolean
Byte
Character
Double
Float
Integer
Long
Short

以上8个基本类型的对象类:

1、都是public final 的

2、都重写了Object类的equals()方法,可以直接用来比较对象的值

    由于重写了equals()方法,所以必须重写hashCode()方法

3、由于都实现了Comparable接口,所以都有compareTo()方法

    compareTo()方法实现两个字符串对象的关系,大于,等于,小于,返回值为int类型的。

4、都有类似于public static long parseLong(String s)这样的将String转换为对应基本数据类型的静态方法

5、都有类似于public static Long valueOf(long l)这样的将基本数据类型转化为对应对象类型的静态方法

-------------------------------------------------------------------------------------------------------------------

二、类加载器(抽象类)

ClassLoader:定义如下

public Class<?> loadClass(String name) throws ClassNotFoundException

 

三、主线程接口和类
接口:Runnable

只有一个虚函数

public interface Runnable {

    public abstract void run();
}

类:Thread

public class Thread implements Runnable {...}

 

四、异常处理常用类和接口
Throwable

Error
Exception

RuntimeException

ClassNotFoundException
NullPointerException
IndexOutOfBoundsException

 

1 Throwable类

   是所有Error类和Exception类的基类。

   提供的方法有:

getMessage()

getLocalizedMessage()

getCause()

printStackTrace():抵达异常掷出点之前的一连串函数调用过程。

printStackTrace(PrintStream s):抵达异常掷出点之前的一连串函数调用过程。

printStackTrace(PrintWriter s):抵达异常掷出点之前的一连串函数调用过程。

fillInStackTrace()

getStackTrace()

setStackTrace(StackTraceElement[] stackTrace)

 

2 Error类

继承自Throwable,不重写Throwable类的方法,重载了多个构造方法。

 

3 Exception类

继承自Throwable,不重写Throwable类的方法,重载了多个构造方法。

 

4 RuntimeException类

继承自Exception,不重写Exception类的方法,重载了多个构造方法。

 

五、算术计算的工具类

Math(final类型)

提供了关于算术计算的诸多静态方法,如

1、三角函数

2、反三角函数

3、弧度转角度,角度转弧度(toRadians,toDegrees)

4、e的几次方(exp)

5、对数计算(log,log10)

6、取平方根(sqrt)

7、取三次方根(cbrt)

8、小数取整:floor,ceil

       这两个宝贝函数的主要任务是截掉小数以后的小数位。
        区别是: floor总是把数字变得越来越小,而ceil总是把数字变大。
        其实名字可以理解floor是地板,ceil是天花板。

        如:

  1. public class CeilAndFloor {
  2.     public static void main(String[] args) {
  3.         System.out.println("==============Math.floor()==============");
  4.         System.out.println("Math.floor(99.1) = " + Math.floor(99.1));
  5.         System.out.println("Math.floor(-99.1) = " + Math.floor(-99.1));
  6.         System.out.println("Math.floor(99.9) = " + Math.floor(99.9));
  7.         System.out.println("Math.floor(99.9) = " + Math.floor(-99.9));
  8.         
  9.         System.out.println("/n/n==============Math.ceil()==============");
  10.         System.out.println("Math.ceil(99.1) = " + Math.ceil(99.1));
  11.         System.out.println("Math.ceil(-99.1) = " + Math.ceil(-99.1));
  12.         System.out.println("Math.ceil(99.9) = " + Math.ceil(99.9));
  13.         System.out.println("Math.ceil(99.9) = " + Math.ceil(-99.9));
  14.         
  15.     }
  16. }
  17. output:
  18. ==============Math.floor()==============
  19. Math.floor(99.1) = 99.0
  20. Math.floor(-99.1) = -100.0
  21. Math.floor(99.9) = 99.0
  22. Math.floor(99.9) = -100.0
  23. ==============Math.ceil()==============
  24. Math.ceil(99.1) = 100.0
  25. Math.ceil(-99.1) = -99.0
  26. Math.ceil(99.9) = 100.0
  27. Math.ceil(99.9) = -99.0

9、两个数比较取大,取小(max,min)

10、随机数(random)

11、四舍五入(round)

12、a的b次方计算(pow)

等等......

注意:java.lang包下还有一个与之类似的类StrictMath(final类型)

 

六、系统相关工具类
System

提供了3个常用的静态数据成员和若干静态方法。如:

System.in     final static

System.out   final static

System.err    final static

并提供了与这3个静态数据成员对象的静态set方法

System.setIn()

System.setOut()

System.setErr()

 

其他静态方法

System.arraycopy()    static native

System.getProperties()  若干重载方法

System.setProperties()  若干重载方法

System.clearProperty(String key)

System.exit(int status)

System.gc()

System.load(String filename)

System.loadLibrary(String libname):加载动态链接库(.dll/.os),libname是库名,不带有.dll后缀名

 

 

七、基类

Object

 

  1. package java.lang;
  2. public class Object {
  3.     private static native void registerNatives();
  4.     static {
  5.         registerNatives();
  6.     }
  7.     public final native Class<?> getClass();
  8.     public native int hashCode();
  9.     public boolean equals(Object obj) {
  10.     return (this == obj);
  11.     }
  12.     protected native Object clone() throws CloneNotSupportedException;
  13.     public String toString() {
  14.     return getClass().getName() + "@" + Integer.toHexString(hashCode());
  15.     }
  16.     public final native void notify();
  17.     public final native void notifyAll();
  18.     public final native void wait(long timeout) throws InterruptedException;
  19.     public final void wait(long timeout, int nanos) throws InterruptedException {
  20.         if (timeout < 0) {
  21.             throw new IllegalArgumentException("timeout value is negative");
  22.         }
  23.         if (nanos < 0 || nanos > 999999) {
  24.             throw new IllegalArgumentException(
  25.                 "nanosecond timeout value out of range");
  26.         }
  27.     if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
  28.         timeout++;
  29.     }
  30.     wait(timeout);
  31.     }
  32.     public final void wait() throws InterruptedException {
  33.     wait(0);
  34.     }
  35.     protected void finalize() throws Throwable { }
  36. }
  37.  

wait()方法:public final/public final native

notify()方法:public final native

notifyAll()方法:public final native

以上3个方法:wait,notify,notifyAll给线程使用的,wait() 是让一个线程处于等待状态,notify()是唤醒一个线程

wait 就是让当前运行的代码暂停,等待通知
notofy 就是发出一个通知,不过不知道谁能拿到这个通知哦!
notifyAll()  就是发出一个广播,所有 wait 的都会听到这个广播通知。

 

getClass()方法:public final native

hashCode()方法:public native int

在Hashtable,HashMap这类使用散列值的类里,查找的时候可以直接使用hashCode,而不必要一个的遍历,这样一来效率提高很多。
equals(),hashCode()(如果你重载了.equals(),hashCode()必须随之更改)。
a.equal(b)==true   =>   a.hashcode()==b.hashcode(),反之则未必。

toString()()方法:

 

八、字符串处理相关类
String
StringBuffer

请参照另外一篇博客《熟悉java.lang包-2(八、字符串处理相关类)》

你可能感兴趣的:(熟悉java.lang包)