:
Puzzle 1:
當求余運算(remainder operation)符 % 返回一個非零余數時,余數的符號位和左邊操作數的符號位相同。例如
????????System.out.println((-53)%9);?//?-8
????????System.out.println(53%(-9));?//8
????????System.out.println((-53)%(-9));?//-8
Puzzle 2:

Change.java
import?java.math.BigDecimal;
public?class?Change
{
????public?static?void?main(String?args[])
????{
????????System.out.println(2.00?-?1.10);//0.8999999999999999
????????System.out.println(new?BigDecimal("2.00").subtract(new?BigDecimal("1.10")));//0.90
????????System.out.printf("%.2f%n",?2.00-1.10);//0.90
????}
}
關于浮點數的二進制表示~~
(1)二進制浮點數并不能精確表示所有的小數
(2)對計算精度要求比較準確(例如金融計算)時,不要使用float和double,盡量使用int, long,BigDecimal.
(3)推薦閱讀文章:What Every Computer Scientist Should Know About Floating-Point Arithmetic
網上很多地方都有的。另一本牛書 Computer Systems A Programmers's Perspective上也有講浮點數
(4)JLS 3.10.1由規范可知 0.1, .1, 1. 都是合法的浮點數。需要注意的是在java中,浮點數有兩種原生類型float,double,當浮點數的后綴是F或者f時,該浮點數為float類型,沒有后綴或者后綴是D或者d時,該浮點數是double類型的。注意下面的例子

FloatPoint.java
public?class?FloatPoint
{
????public?static?void?main(String?[]?args)
????{
????????double?x?=?.12345;
????????double?y?=?1234.;
????????double?z?=?1.123432343;
????????
????????//float?a?=?0.1;?--?可能損失精度
????????float?b?=?0.1f;
????????float?c?=?.1234f;
????????//float?d?=?.123;?--可能損失精度
????????System.out.println("x?=?"?+?x);
????????System.out.println("y?=?"?+?y);
????????System.out.println("z?=?"?+?z);
????????
????????System.out.println("b?=?"?+?b);
????????System.out.println("c?=?"?+?c);
????}
}結果:

結果
x?=?0.12345
y?=?1234.0
z?=?1.123432343
b?=?0.1
c?=?0.1234Puzzle 3:
需要注意java是如何處理整數溢出的,看下面的例子就一目了然了,別忘了long是 8 bytes,int是 4 bytes的~~

LongDividion.java
public?class?LongDivision
{
????public?static?void?main(String[]?args)
????{
????????final?long?MICROS_PER_DAY?=?24?*?60?*?60?*?1000?*?1000L;
????????final?long?MILLIS_PER_DAY?=?24?*?60?*?60?*?1000L;
????????final?int?micros_per_day?=?(int)?MICROS_PER_DAY;
????????final?int?millis_per_day?=?(int)MILLIS_PER_DAY;
????????
????????System.out.println(Long.toHexString(MICROS_PER_DAY));??//??????141dd76000
????????System.out.println(Integer.toHexString(micros_per_day));??//?????1dd76000
?????????System.out.println("*********************************");
????????System.out.println(micros_per_day);?//?500654080
????????System.out.println(millis_per_day);?//?86400000
????}
}
Puzzle 4:
添加long型整數的后綴時要使用L避免用l,同樣不要單獨使用小寫字母l作為變量名,理由是顯而易見的:l和1在大多數字體中太難區分。
Puzzle 5:(1)和十進制數不同,當十六進制、八進制數的最高位是1時,表示它是一個負數(在十進制數中,表示一個負數要顯式使用符號-)
(2)盡量避免混合類型運算,例如本例中的 long型和int型的加法,在java中,一個整數如果沒有后綴L或l,則它是一個int型而不是long型。

JoyOfHex.java
public?class?JoyOfHex
{
????public?static?void?main(String[]?args)
????{
????????System.out.println(Long.toHexString(0x100000000L?+?0xcafebabe));//cafebabe?instead?of?1cafebabe
????????System.out.println(Long.toHexString(0x100000000L?+?0xcafebabeL));?//?1cafebabe
????????System.out.println(0xffffffffL);?//?4294967295
????????System.out.println(0xffffffff);?//?-1
????}
}
Puzzle 6:
The rule "Sign
extension is performed if the type of the original value is signed;
zero extension if it is a char, regardless of the type to which it is
being converted" describes the sign extension behavior when converting from narrower integral types to wider.