
本文共 4832 字,大约阅读时间需要 16 分钟。
������������������������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
1. ���������������������
���������������������������double
���������������������������������������������������������������������double
������������������float
������������������������������������������������������arithmetics���������������������������������������������
������������
public static void main(String[] args) { double num1 = 1; //double��������� double num2 = 31.2; //double��������� double num3 = 323.03; //double��������� System.out.println(num1 + num2 + num3); //���������������355.22999999999996}
������������
- ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������ floating-point precision limit���
- ���������������������������������������������������������������������
BigDecimal
���������������������������������������������DECIMAL
������������������float
���double
���
������������������������������������������������������������������������������������������������
2. ���������������������������
��� Java ���������������������������������������������������������������������������������������������equals
���compareTo
������������������������
������������
public static void main(String[] args) { Integer i1 = 100; //autoboxed���Integer������ Integer i2 = 100; //autoboxed���Integer������ Integer i3 = 200; //autoboxed���Integer������ Integer i4 = 200; //autoboxed���Integer������ System.out.println(i1.equals(i2)); //������true���i3���i4���������true��� System.out.println(i3.equals(i4)); //������false System.out.println(i1.equals(100)); //������false System.out.println(i1.equals("100")); //������false}
������������
- Integer���
valueOf
���������Integer.valueOf(int)
������������������IntegerCache
, ���������[-128, 128]
������������������������������������������������������������������������Integer
��������� - Double���
valueOf
���������Double.valueOf(String s)
���������������������������������Double
���������������������������������������������������
���������������
- ������������������
Double i1 = 100d; i1.equals("100")
���������false������������������������ - ���������������
i1.equals(100d)
������������true���
������������������������������������������compareTo
���������������������Objects.equals
������������������������������������
������������������������������
public static void main(String[] args) { Double d1 = 100d; Double d2 = 100d; System.out.println(d1.equals(d2)); //������true System.out.println(d1.equals("100")); //������false}
3. ������0���������
������������������������������0������������������������������������������
1. 1 ������ 0 ���������
public static void main(String[] args) { double d = 1d; double v = d / 0d; System.out.println(v); //���������Infinity}
2. 0 ������ 0 ���������
public static void main(String[] args) { double d = 0d; double v = d / 0d; System.out.println(v); //���������NaN}
������������
- Infinity������������������������������������
- NaN���������������������������������Not a Number������������������
������������������������������������������0���������������������������������������������������������������
4. Float ��� Double ���������������
���������������������������Float
���������Double
���������������������������������������������������������������
������������
public static void main(String[] args) { Float f = 12312.12f; //������������Float������ Double d = f.doubleValue(); //Float->Double������ System.out.println(d); //12312.1201171875 Double d2 = Double.parseDouble(f.toString()); //��������������� System.out.println(d2); //12312.12}
������������
- Float ��� Double���
Float.doubleValue()
������������������������������������������������������������������������ - ������������������
f.toString()
������������������������������������������������������������������������������
���������������
- ���������������������������������
Float
������Double
��������������������������������������������������� IEEE 754������
���������������
- ������������
Float
���������������������������������������������������Double
��������� - ���������������������������������������������������������������������������
BigDecimal
���������
������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
