1. 程式人生 > 其它 >java語法-數位進位制

java語法-數位進位制

數位進位制

整數拓展

二進位制、十進位制、八進位制、十六進位制表達“10”怎麼處理?

二進位制加0b

十進位制

八進位制加0

十六進位制加0x

舉例:

int i = 10;
int i1 = 010; //八進位制 0
int i2 = 0x10; // 十六進位制0x  0~9 A~F
int i3 = 0b10;
System.out.println(i);
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);

浮點數拓展

銀行業務怎麼表述?

浮點數是有限的、離散的,所以是個大約數,只是接近但不等於。

所以最好不要用浮點數進行比較:

所以最好不要用浮點數進行比較:

所以最好不要用浮點數進行比較:

舉例:

float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d); //false
System.out.println(f);
System.out.println(d);

float d1 = 232333334444443f;
float d2= d1 + 1;
System.out.println(d2);
System.out.println(d1==d2);

輸出結果看,f=d=0.1 f應該等於d

d2=d1+1 d2不等於d1;

但是程式判斷,f不等於d;d2等於d1,就是由這個造成的。

那對於精確資料的比較需要使用數學工具類:BigDecimal

字元拓展

所有的字元本質上還是數字,將字元強制轉換成整型數字。

舉例:

char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println(c2);
System.out.println((int)c1); //強制轉換行
System.out.println((int)c2); //強制轉換行
//編碼: Unicode表  2位元組 0 ~ 65536 Excel  2的16次方;

// Unicode表的範圍:U0000 ~ UFFFF

字元和數字轉換中有張Unicode表來作為“翻譯表”

可以用以下指令來檢視對應的輸出:

char c3 = '\u0061';//轉義字元
System.out.println(c3); //a

數位進位制

整數拓展

二進位制、十進位制、八進位制、十六進位制表達“10”怎麼處理?

二進位制加0b

十進位制

八進位制加0

十六進位制加0x

舉例:

int i = 10;
int i1 = 010; //八進位制 0
int i2 = 0x10; // 十六進位制0x  0~9 A~F
int i3 = 0b10;
System.out.println(i);
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);

浮點數拓展

銀行業務怎麼表述?

浮點數是有限的、離散的,所以是個大約數,只是接近但不等於。

所以最好不要用浮點數進行比較:

所以最好不要用浮點數進行比較:

所以最好不要用浮點數進行比較:

舉例:

float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d); //false
System.out.println(f);
System.out.println(d);

float d1 = 232333334444443f;
float d2= d1 + 1;
System.out.println(d2);
System.out.println(d1==d2);

輸出結果看,f=d=0.1 f應該等於d

d2=d1+1 d2不等於d1;

但是程式判斷,f不等於d;d2等於d1,就是由這個造成的。

那對於精確資料的比較需要使用數學工具類:BigDecimal

字元拓展

所有的字元本質上還是數字,將字元強制轉換成整型數字。

舉例:

char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println(c2);
System.out.println((int)c1); //強制轉換行
System.out.println((int)c2); //強制轉換行
//編碼: Unicode表  2位元組 0 ~ 65536 Excel  2的16次方;

// Unicode表的範圍:U0000 ~ UFFFF

字元和數字轉換中有張Unicode表來作為“翻譯表”

可以用以下指令來檢視對應的輸出:

char c3 = '\u0061';//轉義字元
System.out.println(c3); //a

常用的轉義字元還有:

//轉移字元
// \t 製表符
// \n 換行
// \r 回車
// \b 退格
System.out.println("Hello\tWorld");

布林值擴充套件

boolean flag = true;

if (flag==true) {}和 if (flag) {}是一樣的。
常用的轉義字元還有:

//轉移字元
// \t 製表符
// \n 換行
// \r 回車
// \b 退格
System.out.println("Hello\tWorld");

布林值擴充套件

boolean flag = true;

if (flag==true) {}和 if (flag) {}是一樣的。