java數據類型(進階篇)

  • 2021 年 2 月 18 日
  • 筆記

public class note03 {
public static void main(String[] args) {
//數據類型拓展

    //1.整數拓展

    //進位: 二進位0b   十進位  八進位0  十六進位0x

    int i1 = 10;
    int i2 = 015;//八進位 輸出為13
    int i3 = 0b101010;//二進位 輸出為42
    int i4 = 0x10f;//十六進位 輸出為271
    System.out.println(i1);
    System.out.println(i2);
    System.out.println(i3);
    System.out.println(i4);

    //2.浮點數擴展

    float f = 0.1f;//0.1
    double d = 1/10;//0.1
    System.out.println(f==d);//輸出為false

    float f1 = 2213151311131f;
    float f2 = f1 + 1 ;
    System.out.println(f1==f2);//輸出為true

    //float   有限 離散 舍入誤差 大約 接近但不等於
    //最好完全使用浮點數進行比較!!!
    //最好完全使用浮點數進行比較!!!
    //最好完全使用浮點數進行比較!!!

    //銀行業務如何表示呢?
    //運用類  BigDecimal 數學工具類

    //3.字元類拓展
    char c1 = 'a';
    char c2 = '中';
    System.out.println(c1);
    System.out.println((int)c1);//輸出為97(強制轉換)
    System.out.println(c2);
    System.out.println((int)c2);//輸出為20013(強制轉換)

    //所有的字元本質為數字
    //編碼 Unicode   97=a  65=A   2位元組 長度為65536
    //U0000  ---  UFFFF
    //excel表格  最大2的16次方,為65536

    char c3 = '\u0062';
    System.out.println(c3);//輸出為b

    //轉義字元  \t 製表符 \n 換行
    System.out.println("hello\tworld");//輸出為hello   world
    System.out.println("hello\nworld");//輸出為hello
    //                                        world

    //4.布爾值拓展
    boolean flag = true;
    if (flag==true){}
    if (flag){}
    //兩者相同
    //程式碼要精簡易懂

}

}