Java: Primitive Types

Primitive Types:

1) byte:

    size: 8-Bit = 1-Byte

    range: -128~+127

    usage:

        1> The byte data type can be useful for saving memory in large arrays, where memory saving actually matters.

        2> They can also be used in place of int where their limits help to clearify your code. The fact that a variable's range is limited can serve as a form of documentation.

 

2) short:

    size: 16-Bit = 2-Byte

    range: -32768~+32767

    usage:

        1> You can use short to save memory in large arrays, in situation memory where memory savings acutally matters.

 

3) int:

    size: 32-Bit = 4-Byte

    range: -(2^31)~+(2^31-1)

 

 

4) long:

    size: 64-Bit = 8-Byte

    range: -(2^63) ~ +(2^63-1)

 

5) float:

    size: single-precision 32-Bit IEEE 754 floating point

    range: Its range of value is beyond the scope of this discussion.

    usage: Use a float if you need to save memory in large arrays of floating point numbers.

                This data type should never be used for precise values, such as currency.

                For that, you need to use the java.math.BigDecimal class instead.

 

6) double:

    size: double-precision 64-Bit IEEE 754 floating point.

    range: Its range of values is beyond the scope of this discussion.

    usage: For decimal values, this data type is generally the default choice.

                As mentioned above, this data type should never be used for precise values.

 

7) boolean:

    size: represent one-bit of information, but its size isn't precisely defined. 

    range: true/false

    usage: Use this data type for simple flags that track true/false conditions.

 

8) char:

    size: 16-Bit = 2 Byte

    range: '\u0000' or 0 inclusive ~ '\uffff' or 65535 inclusive

 

Default Values:

 

Data TypeDefault Value (for fields)
byte0
short0
int0
long0L
float0.0f
double0.0d
char'\u0000'
String (or any object)  null
booleanfalse

 

Reference Links:

1) http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值