java基礎語法

注釋

單行注釋 //

多行注釋 /* content */

文檔注釋 /** this is javadoc comment */

標識符

關鍵字

Abstract assert boolean break byte case.....

不可作為變量名或方法名

數據類型

強類型語言:要求變量的使用嚴格符合規定,所有變量都必須先定義再使用。安全性較高,速度較慢。(java)

弱類型語言:javascript

java數據類型

  • 基本類型 primitive type

    數值類型:整數類型 浮點類型 字符類型char

    boolean類型

    //char
    char name = 'z';
    //String
    String name1 = "a string";

    Boolean: 1 bit, true or false


    Double: 8 bytes

    Float: 4 bytes


    Long: 8 bytes

    int: 4 bytes

    short:2 bytes

    byte:1字節

  • 引用類型 reference type

    接口

    數組

字節

位 bit:是計算機內部數據存儲的最小單位

字節 byte:是計算機中數據處理的基本單位

1B = 8 bit

拓展

  • 整數擴展

    進制:二進制0b;十進制;八進制0;十六進制0x (0-9 A-F)

    binary octal decimal hex

  int i = 10; // 二進制 -- 10
int i2 = 010; // 八進制 -- 8
int i3 = 0x10; // 十六進制 -- 16
  • 浮點數拓展

    float: 有限 離散 舍入誤差 結果是一個大約的數

    float f = 0.1f;
    double g = 1.0/10;
    System.out.println(f); // 0.1
    System.out.println(g); // 0.1
    System.out.println(f==g); // false
    ?
    float d1 = 23131231421412f;
    float d2 = d1+1;
    System.out.println(d1==d2); // true

    BigDecimal(數學工具類)

    q:銀行業務如何表示?

    最好完全避免使用浮點數進行比較

  • 字符拓展

    casts 強制轉換

    所有的字符本質還是數字

    char c1 = 'A';
    char c2 = '中';
    System.out.println(c1);
    System.out.println((int)c1); //casts強制類型轉換
    System.out.println(c2);
    System.out.println((int)c2);
    char c3 = '\u0061';
    System.out.println(c3); //a
    System.out.println("hello\tworld");
    System.out.println("hello\nworld");
    1. 八進制轉義序列:\ + 1到3位5數字;范圍'\000'~'\377' \0:空字符

    2. Unicode轉義字符:\u + 四個十六進制數字;0~65535 \u0000:空字符

    World

    hello

    hello world

    \n 換行符

    \t 制表符

    轉義字符

    2字節 65536 (早期的Excel表格的長度 2**16=65536)U0000-UFFFF

    編碼: unicode表

  • 布爾值擴展

    boolean flag = true;
    if (flag==true){}
    // equal to if(flag){}
    int i = 128;
    byte b = (byte)i; // -128
    //manipulate a large number, attention to overflow problem
    int money = 1_000_000_000; // _ can be used in numbers, it won't print
    int years = 20;
    int total = money * years; //-1474836480 memory overflow
    long total2 = money * years; // -1474836480
    // the type of money and years is int
    // so the default type of total2 is also int
    // before casts it to long, the result already got problem
    
    // First, cast one number to long
    long total3 = money * ((long)years); //20000000000

    memory overflow

    char c = 'a'; // a
    int v  = c+1; // 98
    System.out.println((char)v); // b
    1. 不能對布爾值進行轉換

    2. 不能把對象類型轉換為不相干的類型

    3. 在把高容量轉換為低容量的時候,強制轉換

    4. 轉換的時候可能存在內存溢出,或者精度問題

    注意點

    低 -> 高

    double d = i; //128.0
    • 自動轉換

    Problem2: 精度問題

    System.out.println((int)23.7); //23
    System.out.println((int)-45.89f); //-45
    problem1: 內存溢出 memory overflow
    the max value of byte is 127

    高 -> 低

    (類型)變量名

    • 強制轉換

    類型轉換

    • 強制轉換

    (類型)變量名

    高 -> 低

    int i = 128;
    byte b = (byte)i; // -128

    problem1: 內存溢出 memory overflow
    the max value of byte is 127

    System.out.println((int)23.7); //23
    System.out.println((int)-45.89f); //-45

  • Problem2: 精度問題

    • 自動轉換

    double d = i; //128.0

    低 -> 高

    注意點

    1. 不能對布爾值進行轉換

    2. 不能把對象類型轉換為不相干的類型

    3. 在把高容量轉換為低容量的時候,強制轉換

    4. 轉換的時候可能存在內存溢出,或者精度問題

memory overflow

char c = 'a'; // a

int v = c+1; // 98

System.out.println((char)v);

//manipulate a large number, attention to overflow problem
int money = 1_000_000_000; // _ can be used in numbers, it won't print
int years = 20;
int total = money * years; //-1474836480 memory overflow
long total2 = money * years; // -1474836480
// the type of money and years is int
// so the default type of total2 is also int
// before casts it to long, the result already got problem

// First, cast one number to long
long total3 = money * ((long)years); //20000000000