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 Type Default Value (for fields)
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
String (or any object)   null
boolean false

 

Reference Links:

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

 

你可能感兴趣的:(java,Primitive Types)