Java 基本問題

來源:互聯網
上載者:User

標籤:表示   test   double   錯誤   als   最大值   ref   ble   oat   

1、Java 基礎資料型別 (Elementary Data Type)與引用資料類型
https://www.cnblogs.com/Latiny/p/8099581.html

案例:

class test {    public static void main(String[] args){        # 引用資料類型 = 基本資料結構 + 方法        test t = new test();        # 基本資料結構         char c1 = ‘a‘;        byte b1 = 2;        short s1 = 13;        int i1 = 12;        long l1 = 123;        double d1 = 1234;        System.out.println(""+c1+i1);        System.out.println(t.judgeType(c1));    }public String judgeType(Object temp) {    if (temp instanceof Byte) {        return "是Byte類型";    } else if (temp instanceof Integer) {        return "是Integer類型";    } else if (temp instanceof Short) {        return "是Short類型";    } else if (temp instanceof String) {        return "是String類型";    } else if (temp instanceof Long) {        return "是Long類型";    } else if (temp instanceof Float) {        return "是Float類型";    } else if (temp instanceof Double) {        return "是Double類型";    } else if (temp instanceof Character) {        return "是Character類型";    } else {        return "是引用資料類型";    }}}

2、算術運算子

#錯誤: 不相容的類型: 從double轉換到int可能會有損失int i1 = 0.1;# 當“=”兩側資料類型不一致時,可以使用自動類型轉換或使用強制類型轉換原則進行處理int i1 = (int)0.1;int i1 *= 0.1;# 結果為 0System.out.println(i1);  

3、邏輯運算子

 “&”和“&&”的區別: 單&時,左邊無論真假,右邊都進行運算; 雙&時,如果左邊為真,右邊參與運算,如果左邊為假,那麼右邊不參與運算! 建議使用 &&“|”和“||”的區別同理,||表示:當左邊為真,右邊不參與運算!異或( ^ )與或( | )的不同之處是:當左右都為true時,結果為false。

樣本:

# 結果異常: Exception in thread "main" java.lang.ArithmeticException: / by zeroif ( false & (1/0 > 1) ){    System.out.println("true");}else{    System.out.println("false");}# 結果為false# 雙&時,如果左邊為假,那麼右邊不參與運算!if ( false && (1/0 > 1) ){    System.out.println("true");}else{    System.out.println("false");}

4、位元運算符

// 帶符號右移: 最高位用符號位補//  -8System.out.println(-31>>2);// 無符號右移: 最高位用0補//  1073741816System.out.println(-31>>>2);# 位元運算解決基礎資料型別 (Elementary Data Type)交換int m = 5;int n = 17;System.out.println("m:"+m+" n:"+n);m = m ^ n;n = m ^ n; // (5 ^ 17) ^ 17 = 5m = m ^ n; // (5 ^ 17) ^ 5 = 17System.out.println("m:"+m+" n:"+n);

5、三元運算子

 // 三元運算子與if-else的聯絡與區別 // 1、三元運算子可簡化if-else語句 // 2、三元運算子要求必須返回一個結果 // 3、if後的代碼塊可有多個語句樣本:    // 三個值尋找最大值    int m = -5;    int n = 17;    int k = 12;    System.out.println( (k > ((m > n)? m:n)) ? k : ((m > n)? m:n) );

Java 基本問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.