1. 程式人生 > 實用技巧 >Java Object、基本資料型別的包裝類

Java Object、基本資料型別的包裝類

1、Object類

Object類的基本方法:

getClass()、hashcode()、equals()、clone()、finalize()、toString()

public final native Class<?> getClass()    //返回此 Object 執行時的類

public native int hashCode()    //返回物件的雜湊碼

public boolean equals(Object obj)    //判斷其他物件與此物件是否“相等”

protected native Object clone() throws CloneNotSupportedException    //
建立並返回此物件的一個副本 public String toString() //返回物件的字串表示 protected void finalize() throws Throwable {} //垃圾回收時呼叫該方法

public final native void notify() //喚醒在此物件監視器上等待的單個執行緒 public final native void notifyAll() //喚醒在此物件監視器上等待的所有執行緒 public final native void wait(long timeout) throws InterruptedException //
使當前物件的執行緒等待 timeout 時長 public final void wait(long timeout, int nanos) throws InterruptedException //使當前物件的執行緒等待 timeout 時長,或其他執行緒中斷當前執行緒 public final void wait() throws InterruptedException //使當前物件的執行緒等待

2、基本資料型別:

byte、int、short、long、double、float、boolean、char;

對應的包裝型別也有八種:Byte、Integer、Short、Long、Double、Float、Character、Boolean;(已final,不可重寫)

將基本資料型別 轉成 物件包裝型別------裝箱,反之為拆箱

public static void main(String[] args) {
    int num1 = 1;
    //將基本資料型別裝箱成物件包裝型別,編譯器內建方法
    Integer num2 = num1;
    Integer num3 = 3;
    //將物件資料類拆箱,該方法是java.lang.Number類中的
    int num4 = num3;
}

繼承關係:Number類是 基本資料型別包裝類 的父類

Number類:

package java.lang;

public abstract class Number implements java.io.Serializable {

    public abstract int intValue();  \\拆箱方法

    public abstract long longValue();

    public abstract float floatValue();

    public abstract double doubleValue();

    public byte byteValue() {
        return (byte)intValue();
    }

    public short shortValue() {
        return (short)intValue();
    }

    private static final long serialVersionUID = -8742448824652078965L;
}

Integer類常用方法:

1  parseInt(String s)將字串轉換成Int

2  toString() 轉換成字串

3  還有一種方法,任何型別+“ ” 即變成String型別

4  max()、min()。兩個int的比較

兩個靜態成員變數:MAX_VALUE;MIN_VALUE(在其他數值型別中也有相同變數)

5  compare方法,比較兩個數。返回-1、0、1

public class BasicNumber {
    public static void main(String args[]) {
        //最大最小值
        int intmax=Integer.MAX_VALUE;
        int intmin=Integer.MIN_VALUE;
        System.out.println(intmax);System.out.println(intmin);
        //String to Int
        String string="55";
        int testInt=100;
        System.out.println(Integer.parseInt(string)+12);//parseInt方法
        System.out.println(Integer.toString(testInt));//toString方法
        System.out.println(String.valueOf(testInt));//valueOf方法
        System.out.println(testInt+"");//+""的隱式轉換
        //檢視int佔用的位數
        System.out.println(Integer.SIZE);
        //compare方法
        int intbig=17;
        int intsmall=6;
        System.out.println(Integer.compare(intsmall, intbig));
    }
}

character類

    Character類的判斷功能:

public static boolean isDigit(char ch)
  確定指定字元是否為數字。

public static boolean isLetter(charch)
  確定指定字元是否為字母。

public static boolean isLowerCase(char ch)
確定是否是小寫字母字元

public static boolean isUpperCase(char ch)
確定是否大寫字母字元

    兩個轉換功能:

public static int toLowerCase(char ch)
使用取自 UnicodeData 檔案的大小寫對映資訊將字元引數轉換為小寫。

public static int toUpperCase(char ch)
使用取自 UnicodeData 檔案的大小寫對映資訊將字元引數轉換為大寫。