:
当求余运算(remainder operation)符 % 返回一个非零余数时,余数的符号位和左边操作数的符号位相同。例如
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.1234
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 1:
当求余运算(remainder operation)符 % 返回一个非零余数时,余数的符号位和左边操作数的符号位相同。例如
System.out.println((
-
53
)
%
9
);
//
-8
System.out.println( 53 % ( - 9 )); // 8
System.out.println(( - 53 ) % ( - 9 )); // -8
System.out.println( 53 % ( - 9 )); // 8
System.out.println(( - 53 ) % ( - 9 )); // -8
Puzzle 2:
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
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类型的。注意下面的例子
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
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);
}
}
结果:
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
x = 0.12345
y = 1234.0
z = 1.123432343
b = 0.1
c = 0.1234
Puzzle 3:
需要注意java是如何处理整数溢出的,看下面的例子就一目了然了,别忘了long是 8 bytes,int是 4 bytes的~~
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
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
}
}
(2)尽量避免混合类型运算,例如本例中的 long型和int型的加法,在java中,一个整数如果没有后缀L或l,则它是一个int型而不是long型。
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
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:
Multicast.java
public class Multicast
{
public static void main(String[] args)
{
/* int -> byte : 0xffffffff -> 0xff
* byte -> char: 0xff -> 0xffff
* char -> int : 0xffff -> 0x0000ffff
*/
System.out.println((int) (char) (byte) -1); //0x0000ffff = 65535
}
}
CleverSwap.java
public class CleverSwap
{
public static void main(String[] args)
{
int x = 1984;
int y = 2001;
x ^= y ^= x ^= y;
System.out.println("x = " + x + "; y = " + y); // x = 0; y = 1984
}
}
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.
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class Multicast
{
public static void main(String[] args)
{
/* int -> byte : 0xffffffff -> 0xff
* byte -> char: 0xff -> 0xffff
* char -> int : 0xffff -> 0x0000ffff
*/
System.out.println((int) (char) (byte) -1); //0x0000ffff = 65535
}
}
Puzzle 6:
JLS 15.7 Evaluation Order
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class CleverSwap
{
public static void main(String[] args)
{
int x = 1984;
int y = 2001;
x ^= y ^= x ^= y;
System.out.println("x = " + x + "; y = " + y); // x = 0; y = 1984
}
}