java八大基本資料型別測試總結
public class DataType{
/*
java的資料型別:
1.8大基本資料型別;2.引用型別
八大基本資料型別:
1.整數 byte short int long
2.浮點型別(小數)float double
3.字元型別: char
4.布林型別:boolean(true,false)
1)位元組型:使用byte表示。(1位元組表示-128~127之間的256個整數)。
2)型短整:使用short表示。16位(2位元組)-32768-32767
3)整型:使用int表示。32位(4位元組)-2147483648-2147483647
4)長整型:使用long表示。64位(8位元組)-9233372036854477808-9233372036854477807
5)單精度浮點型:使用float表示。(4位元組)8個有效數字(四捨五入)
6)雙精度浮點型:使用double表示。(8位元組)17個有效數字(四捨五入)
7)字元型:使用char表示。(2位元組)
8)布林型:使用boolean表示。(1位)true false
拓展型別:
字串:String
當字串與其他資料相加的時候,會將其他資料也轉換為字串,並且首尾相連
注意點:
1.java中所有的整型資料,預設為int型別
2.當定義long型別的資料,超出int範圍的時候,需要在字面值的末尾加上L(建議寫大寫的L)
3.java中浮點型別預設為double型別,定義float型別的時候需要在末尾加上f
4.java中,浮點型別的運算與縮排都是不精確的,最好轉化成整型運算
*/
public static void main(String[] args){
byte by = 120;//超出資料型別的範圍,編譯不能通過
System.out.println("by="+by);
short sh = 30000;
System.out.println("sh="+sh);
int i = 5000;
System.out.println("i="+i);
long lo = 3000000000L;
System.out.println("lo="+lo);
float flo = 1.23333449f;
System.out.println("flo="+flo);
double dou = 2.4;
System.out.println("dou="+dou);
char ch = ';';
System.out.println("ch="+ch);
boolean boo = false;
System.out.println("boo="+boo);
String str = "我們都是好學生";
System.out.println("str="+str);
System.out.println(2+4+"=2+4");//"2+4="+2+4->"2+4=2"+4->"2+4=24"
}
}