「Java」 第三課: 型態Type

型態有兩大類:
1. 基本型態
2. 類別型態


基本型態又分成以下:
1. 整數:     short ,int , long
2. 位元:     byte
3. 浮點數:  float
4. 字元:     char
5. 布林:     boolean

解析:
short :   占用記憶體長度 2 位元組
int :       占用記憶體長度 4 位元組
long :    占用記憶體長度 8 位元組
byte:     占用記憶體長度 1 位元組
float:     占用記憶體長度 4 位元組
double: 占用記憶體長度 8 位元組
char:     採用字元符號來儲存,占用記憶體長度 2 位元組
boolean: 採用 true 與 false 來分別表示

備註: 當儲存值超出各自的範圍稱之為『溢值 Overflow』

public class PrimitiveType{

    //有static 是 類別型態的變數

    public static float aa1a;

    public static double aa2;

   

public static void main(String[] args){

    //static 是類別成員,只要透過類別名稱呼叫即可

    //區域變數 : 型別  變數名稱[值];

   

    int intMax = Integer.MAX_VALUE;

    //int是基本型別  Interger是類別成員 

    //Interger.MAX_VALUE =>呼叫    類別名稱 的 最大值

    //MAX_VALUE 類別屬性

    System.out.println("1. Max= "+intMax); //Max= 2147483647

   

   

    int intMin = Integer.MIN_VALUE;

    System.out.println("2. MIN= "+intMin); //MIN= -2147483648

    /*

    + 可分兩種:

    1. 加號

    2. 串接

    */

    System.out.println(2147483647); //2147483647

    //System.out.println(2147483648); // error integer number too large

    System.out.println(2147483648L);//2147483648  因數值 升轉 int -> long

    System.out.println(2147483647+1);//-2147483648  溢位 變成負數 -2147483648 (因二進位)

   

    System.out.println(aa1a); //public static float aa1a; => 0.0

    System.out.println(aa2); //public static double aa2; => 0.0

   

    int a1 = 1;

    int a2 = intMax+a1;

    System.out.println("3. a2= "+a2); //a2= -2147483648


   

    //為了避免溢位,所以使用大水桶long

    long a3 = 5;

    long a4 = intMax+a3;

    System.out.println("4. a4 ="+a4);//a4 =2147483652

   

   

    System.out.println(2147483647+2147483646);//溢位 變成負數 -3

    //有如一個時鐘,在12點鐘 0 右為正(累加),向左為負(累加),

    //在六點鐘-2147483648 向右為正(累減),向左為負(累減),


    System.out.println("2147483647+1");//2147483647+1  字串 串接

    System.out.println("Test"+3+3+4);//Test334  字串 串接

    System.out.println(3+3+4);//10  數值 相加

   

    //-------整數: default primitive = int ---------------------------

    //十進位 原本數值

    System.out.println("十進位 558: "+558); //十進位 558: 558

   

    //二進位 0b_0-1

    System.out.println("二進位 0b_1000101110: "+0b1_000101110);//二進位 0b_1000101110: 558

   

    //八進位 0_0-9

    System.out.println("八進位 0_1056: "+0_1056);//八進位 0_1056: 558

   

    //十六進位 0x_0-9_(A(11)/B(12)/C(13)/D(14)/E(14))

    System.out.println("十六進位 0x_22E: "+0x22E);//十六進位 0x_22E: 558

   

    //-------小數: default primitive = double---------------------------

   

    //float a1=1.23; //Reason 1.23 default is double type,

    float b1 = 1.23F; //強制 double降轉 為float

    double b2=1.23;

    System.out.println(b1+b2); //2.4600000190734863

   

    float b3 = 1.23F;//強制 double降轉 為float

    System.out.println(b3);//1.23

   

    double b4 = 1.23;

    System.out.println(b4);//1.23

   

    float a5 = 0.3F; //大概的小數值

    System.out.println(a5+a5+a5);//0.90000004

   

    double a6 = 0.3;//精確的小數值

    System.out.println(a6+a6+a6);//0.8999999999999999

   

    //-------字元: default primitive = char---------------------------

   

    char c1 = 'A';

    char c2 = '\u0041';

    System.out.println(c1); // A

    System.out.println(c2);// A

   

    System.out.println(c1+5);//char(16進位)可轉換成int(10進位)=>65 + 5 = 70

   

    char c3 = '熱';

    char c4 = '\u5496';

    char c5 = '\u5561';

    System.out.println("-->"+c3+c4+c5);//字串  -->熱咖啡

    System.out.println(c3+c4+c5);//int  72616

   

    //-------布林: default primitive = boolean---------------------------

   

    boolean B = true;

    System.out.print(B); //true

    System.out.print(5>3); //true

    System.out.print(3==3); //true

}

}

留言

這個網誌中的熱門文章

[Excel]國曆轉農曆VBA

「CSS」「div區塊介紹」 三欄式網頁排版設計

「Java」「For迴圈」 三角形