Java基礎03之資料型別
阿新 • • 發佈:2021-08-02
資料型別
- Java是一種強型別語言,即要求變數的使用要嚴格符合規範,所有變數都必須先定義後才能使用
基本型別(primitive type)
數值型別
整數型別
- byte
byte佔1個位元組,範圍為-128~127 - short
short佔2個位元組,範圍為-32768~32767 - int
int佔4個位元組,範圍為-2147483648~2147483647 - long
long佔8個位元組,範圍為-9223372036854775808~9223372036854775807
浮點型別
- float
float佔4個位元組
2。 double
double佔8個位元組
字串型別
- char
char佔2個位元組
boolean型別
boolean佔1位,其值只有true和false兩個
public class Demo{ public static void main(String[] args){ //八大資料型別 //整數 byte num1 = 10; short num2 = 20; int num3 = 30; //最常用的整數資料型別 long num4 = 500l;//long型別要在數字後面加個l //小數:浮點數 float num5 = 12.3f;//float型別要在數字後面加個f double num6 = 4.23552334; //字元 char c = 'A'; //布林值:是非 boolean b = true; } }
引用型別(reference type)
引用型別有類、介面、陣列,後面會展開,在這裡暫不表述