欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java中基本數(shù)據(jù)類型與Object的關(guān)系說明

 更新時間:2022年03月12日 10:46:38   作者:向上的狼  
這篇文章主要介紹了java基本數(shù)據(jù)類型與Object的關(guān)系說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

基本數(shù)據(jù)類型與Object的關(guān)系

我知道大家是不是都聽說過Object是所有類型的基類,但是這句話其實并不是正確的,因為java中基本數(shù)據(jù)類型跟Object是沒有任何關(guān)系的.

這里舉一個例子

這里在進(jìn)行調(diào)用swap方法時,是不能直接將int類型傳給swap(Object obj)方法的,因為Object其實跟基本數(shù)據(jù)類型沒有一點關(guān)系,這個時候a就發(fā)現(xiàn)我們類型不匹配, 所以就自動包裝成了Integer類型了, 這個時候就能跟Object產(chǎn)生聯(lián)系了, 才能成功調(diào)用swap方法了.

Object、基本數(shù)據(jù)類型的包裝類

Object類

Object類的基本方法:

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

public final native Class<?> getClass()    //返回此 Object 運(yùn)行時的類
public native int hashCode()    //返回對象的哈希碼
public boolean equals(Object obj)    //判斷其他對象與此對象是否“相等”
protected native Object clone() throws CloneNotSupportedException    //創(chuàng)建并返回此對象的一個副本
public String toString()    //返回對象的字符串表示
protected void finalize() throws Throwable {}    //垃圾回收時調(diào)用該方法
public final native void notify()    //喚醒在此對象監(jiān)視器上等待的單個線程
public final native void notifyAll()    //喚醒在此對象監(jiān)視器上等待的所有線程
public final native void wait(long timeout) throws InterruptedException    //使當(dāng)前對象的線程等待 timeout 時長
public final void wait(long timeout, int nanos) throws InterruptedException    //使當(dāng)前對象的線程等待 timeout 時長,或其他線程中斷當(dāng)前線程
public final void wait() throws InterruptedException    //使當(dāng)前對象的線程等待

基本數(shù)據(jù)類型

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

對應(yīng)的包裝類型也有八種:

Byte、Integer、Short、Long、Double、Float、Character、Boolean;(已final,不可重寫)

將基本數(shù)據(jù)類型 轉(zhuǎn)成 對象包裝類型------裝箱,反之為拆箱

public static void main(String[] args) {
    int num1 = 1;
    //將基本數(shù)據(jù)類型裝箱成對象包裝類型,編譯器內(nèi)置方法
    Integer num2 = num1;
    Integer num3 = 3;
    //將對象數(shù)據(jù)類拆箱,該方法是java.lang.Number類中的
    int num4 = num3;
}

繼承關(guān)系:

Number類是 基本數(shù)據(jù)類型包裝類 的父類

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類常用方法:

  • parseInt(String s)將字符串轉(zhuǎn)換成Int
  • toString() 轉(zhuǎn)換成字符串
  • 還有一種方法,任何類型+“ ” 即變成String類型
  • max()、min()。兩個int的比較
  • 兩個靜態(tài)成員變量:MAX_VALUE;MIN_VALUE(在其他數(shù)值類型中也有相同變量)
  • compare方法,比較兩個數(shù)。返回-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+"");//+""的隱式轉(zhuǎn)換
        //查看int占用的位數(shù)
        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)

確定指定字符是否為數(shù)字。 

public static boolean isLetter(charch)

確定指定字符是否為字母。 

public static boolean isLowerCase(char ch)

確定是否是小寫字母字符 

public static boolean isUpperCase(char ch)

確定是否大寫字母字符    

兩個轉(zhuǎn)換功能

public static int toLowerCase(char ch)

使用取自 UnicodeData 文件的大小寫映射信息將字符參數(shù)轉(zhuǎn)換為小寫。 

public static int toUpperCase(char ch)

使用取自 UnicodeData 文件的大小寫映射信息將字符參數(shù)轉(zhuǎn)換為大寫。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。 

相關(guān)文章

最新評論