常用链接

统计

最新评论

Special case: primitive types

 

Special case: primitive types

Java determines the size of each primitive type. These sizes don’t change from one machine

architecture to another as they do in most languages. This size invariance is one reason Java

programs are more portable than programs in most other languages.

 

 

All numeric types are signed, so don’t look for unsigned types.

The size of the boolean type is not explicitly specified; it is only defined to be able to take the literal values true or false.

The “wrapper” classes for the primitive data types allow you to make a non-primitive object on the heap to represent that primitive type. For example:

char c = 'x';

Character ch = new Character(c);

Or you could also use:

Character ch = new Character('x');

Java SE5 autoboxing will automatically convert from a primitive to a wrapper type:

Character ch = 'x';

and back:

char c = ch;

The reasons for wrapping primitives will be shown in a later chapter.

High-precision numbers

Java includes two classes for performing high-precision arithmetic: BigInteger and

BigDecimal. Although these approximately fit into the same category as the “wrapper” classes, neither one has a primitive analogue.

Both classes have methods that provide analogues for the operations that you perform on

primitive types. That is, you can do anything with a BigInteger or BigDecimal that you can with an int or float, it’s just that you must use method calls instead of operators. Also, since there’s more involved, the operations will be slower. You’re exchanging speed for accuracy.

BigInteger supports arbitrary-precision integers. This means that you can accurately represent integral values of any size without losing any information during operations.

BigDecimal is for arbitrary-precision fixed-point numbers; you can use these for accurate

monetary calculations, for example.

 

OX1231×1622×1613×160

posted on 2008-05-20 13:12 九宝 阅读(191) 评论(0)  编辑  收藏 所属分类: Java Study(JavaThinking4)


只有注册用户登录后才能发表评论。


网站导航: