Reason The float and double types are particularly ill-suited for monetary calculations because it is impossible to represent 0.1 (or any other negative power of ten) as a float or double exactly. // This will print 0.6100000000000001 System.out.prin…
java中int,float,long,double取值范围是多少? 写道 public class TestOutOfBound { public static void main(String[] args) { System.out.println(Integer.MAX_VALUE-(-Integer.MAX_VALUE)); //内存溢出System.out.println(Integer.MAX_VALUE); //2的31次方-1,10个数位,正的20亿左右,用在钱上面不一定够Sy…
java中的float.double计算存在精度问题,这不仅仅在java会出现,在其他语言中也会存在,其原因是出在IEEE 754标准上. 而java对此提供了一个用于浮点型计算的类——BigDecimal(java.math.BigDecimal),通过将double替换成BigDecimal进行计算可以获得较为精确的计算结果. BigDecimal的构造方法有许多,在此推荐使用BigDecimal(String val)的构造方法,通过String字符串进行构造.可能会有人直接使用BigDe…
原文网址:http://blog.sina.com.cn/s/blog_827d041701017ctm.html 问题提出:12.0f-11.9f=0.10000038,"减不尽"为什么? 来自MSDN的解释: http://msdn.microsoft.com/zh-cn/c151dt3s.aspx 为何浮点数可能丢失精度浮点十进制值通常没有完全相同的二进制表示形式. 这是 CPU 所采用的浮点数据表示形式的副作用.为此,可能会经历一些精度丢失,并且一些浮点运算可能会产生意外的结果…
Principle Strings are poor substitutes for other value types. Such as int, float or BigInteger. Strings are poor substitutes for enum types. As discussed in Item 30. Strings are poor substitutes for aggregate types. A better approach is simply to wri…
Principle To avoid liveness and safety failures, never cede control to the client within a synchronized method or block. Do as little work as possible inside synchronized regions. You should make a mutable class thread-safe (Item 70) if it is intende…
public static void main(String[] args) { double funds=1.00; ; // ; ;funds>=price;price+=.){ funds-=price; itemBought++; } //#解释1 // 第一次 price=0.1 funds=1.00 // #1 #2 #3 // for(double price=.1;funds>=price;price+=.10) //不经过#2,#3 price仍然为0.1 进入for循环 执…
NOTE Never do anything time-critical in a finalizer. Never depend on a finalizer to update critical persistent state. There is a severe performance penalty for using. "Finalizer chaining" is not performed automatically. The subclass finalizer mu…
Thread groups were originally envisioned as a mechanism for isolating applets for security purposes. They never really fulfilled this promise, and their security importance has waned to the extent that they aren't even mentioned in the standard work…
String s = new String("stringette"); // Don't do this. This will create an object each time Vs String s = "stringette"; class Person { private final Date birthDate; // Other fields, methods, and constructor omitted /** * The starting a…