Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Primitive Data Types · Private Archive

Java Primitive Data Types · Private Archive

Java Primitive Data Types

A variable is an application for memory to store a value. That is, when a variable is created, space must be requested in memory.

The memory management system allocates storage space for the variable according to its type, and the allocated space can only be used to store data of that type.

Therefore, by defining variables of different types, integers, decimals, or characters can be stored in memory.


The two major data types in Java:

Reference data types

byte:

short:

int:

long:

float:

double:

double d2 = 7.; double d3 = 8.0; double d4 = 8.D; double d5 = 12.9867;

7 is an int literal, while 7D, 7., and 8.0 are double literals.

boolean:

char:

Example

For the value ranges of the primitive numeric types, we do not need to memorize them forcibly, because their values are already defined as constants in the corresponding wrapper classes. Please see the following example:

Example

public

class

PrimitiveTypeTest

{

public

static

void

main

(

String

[

]

args

)

{

//

byte

System

.

out

.

println

(

"

Primitive type: byte Number of bits:

"

Byte

.

SIZE

)

;

System

.

out

.

println

(

"

Wrapper class: java.lang.Byte

"

)

;

System

.

out

.

println

(

"

Minimum value: Byte.MIN_VALUE=

"

Byte

.

MIN_VALUE

)

;

System

.

out

.

println

(

"

Maximum value: Byte.MAX_VALUE=

"

Byte

.

MAX_VALUE

)

;

System

.

out

.

println

(

)

;

//

short

System

.

out

.

println

(

"

Primitive type: short Number of bits:

"

Short

.

SIZE

)

;

System

.

out

.

println

(

"

Wrapper class: java.lang.Short

"

)

;

System

.

out

.

println

(

"

Minimum value: Short.MIN_VALUE=

"

Short

.

MIN_VALUE

)

;

System

.

out

.

println

(

"

Maximum value: Short.MAX_VALUE=

"

Short

.

MAX_VALUE

)

;

System

.

out

.

println

(

)

;

//

int

System

.

out

.

println

(

"

Primitive type: int Number of bits:

"

Integer

.

SIZE

)

;

System

.

out

.

println

(

"

Wrapper class: java.lang.Integer

"

)

;

System

.

out

.

println

(

"

Minimum value: Integer.MIN_VALUE=

"

Integer

.

MIN_VALUE

)

;

System

.

out

.

println

(

"

Maximum value: Integer.MAX_VALUE=

"

Integer

.

MAX_VALUE

)

;

System

.

out

.

println

(

)

;

//

long

System

.

out

.

println

(

"

Primitive type: long Number of bits:

"

Long

.

SIZE

)

;

System

.

out

.

println

(

"

Wrapper class: java.lang.Long

"

)

;

System

.

out

.

println

(

"

Minimum value: Long.MIN_VALUE=

"

Long

.

MIN_VALUE

)

;

System

.

out

.

println

(

"

Maximum value: Long.MAX_VALUE=

"

Long

.

MAX_VALUE

)

;

System

.

out

.

println

(

)

;

//

float

System

.

out

.

println

(

"

Primitive type: float Number of bits:

"

Float

.

SIZE

)

;

System

.

out

.

println

(

"

Wrapper class: java.lang.Float

"

)

;

System

.

out

.

println

(

"

Minimum value: Float.MIN_VALUE=

"

Float

.

MIN_VALUE

)

;

System

.

out

.

println

(

"

Maximum value: Float.MAX_VALUE=

"

Float

.

MAX_VALUE

)

;

System

.

out

.

println

(

)

;

//

double

System

.

out

.

println

(

"

Primitive type: double Number of bits:

"

Double

.

SIZE

)

;

System

.

out

.

println

(

"

Wrapper class: java.lang.Double

"

)

;

System

.

out

.

println

(

"

Minimum value: Double.MIN_VALUE=

"

Double

.

MIN_VALUE

)

;

System

.

out

.

println

(

"

Maximum value: Double.MAX_VALUE=

"

Double

.

MAX_VALUE

)

;

System

.

out

.

println

(

)

;

//

char

System

.

out

.

println

(

"

Primitive type: char Number of bits:

"

Character

.

SIZE

)

;

System

.

out

.

println

(

"

Wrapper class: java.lang.Character

"

)

;

//

Output Character.MIN_VALUE to the console as a numeric value rather than a character

System

.

out

.

println

(

"

Minimum value: Character.MIN_VALUE=

"

(

int

)

Character

.

MIN_VALUE

)

;

//

Output Character.MAX_VALUE to the console as a numeric value rather than a character

System

.

out

.

println

(

"

Maximum value: Character.MAX_VALUE=

"

(

int

)

Character

.

MAX_VALUE

)

;

}

}

Run Example »

The compilation output of the above code is as follows:


基本类型:byte 二进制位数:8
包装类:java.lang.Byte
最小值:Byte.MIN_VALUE=-128
最大值:Byte.MAX_VALUE=127

基本类型:short 二进制位数:16
包装类:java.lang.Short
最小值:Short.MIN_VALUE=-32768
最大值:Short.MAX_VALUE=32767

基本类型:int 二进制位数:32
包装类:java.lang.Integer
最小值:Integer.MIN_VALUE=-2147483648
最大值:Integer.MAX_VALUE=2147483647

基本类型:long 二进制位数:64
包装类:java.lang.Long
最小值:Long.MIN_VALUE=-9223372036854775808
最大值:Long.MAX_VALUE=9223372036854775807

基本类型:float 二进制位数:32
包装类:java.lang.Float
最小值:Float.MIN_VALUE=1.4E-45
最大值:Float.MAX_VALUE=3.4028235E38

基本类型:double 二进制位数:64
包装类:java.lang.Double
最小值:Double.MIN_VALUE=4.9E-324
最大值:Double.MAX_VALUE=1.7976931348623157E308

基本类型:char 二进制位数:16
包装类:java.lang.Character
最小值:Character.MIN_VALUE=0
最大值:Character.MAX_VALUE=65535

The minimum and maximum values of Float and Double are output in scientific notation; the trailing "E+number" means the number before E is multiplied by 10 to the power of that number. For example, 3.14E3 is 3.14 x 10^3 = 3140, and 3.14E-3 is 3.14 x 10^-3 = 0.00314.

In fact, there is another primitive type void in JAVA, which also has a corresponding wrapper class java.lang.Void, but we cannot directly operate on them.

Type Default Values

The following table lists the default values of each Java type:

Data Type Default value
int 0
long 0L
short 0
char '\u0000'
byte 0
float 0.0f
double 0.0d
boolean false
Reference types (classes, interfaces, arrays) null

Explanation:

Example

public

class

Test

{

static

boolean

bool

;

static

byte

by

;

static

char

ch

;

static

double

d

;

static

float

f

;

static

int

i

;

static

long

l

;

static

short

sh

;

static

String

str

;

public

static

void

main

(

String

[

]

args

)

{

System

.

out

.

println

(

"

Bool :

"

bool

)

;

System

.

out

.

println

(

"

Byte :

"

by

)

;

System

.

out

.

println

(

"

Character:

"

ch

)

;

System

.

out

.

println

(

"

Double :

"

d

)

;

System

.

out

.

println

(

"

Float :

"

f

)

;

System

.

out

.

println

(

"

Integer :

"

i

)

;

System

.

out

.

println

(

"

Long :

"

l

)

;

System

.

out

.

println

(

"

Short :

"

sh

)

;

System

.

out

.

println

(

"

String :

"

str

)

;

}

}

The example output is:

Bool     :false
Byte     :0
Character:
Double   :0.0
Float    :0.0
Integer  :0
Long     :0
Short    :0
String   :null

Reference type


Java Constants

A constant cannot be modified while the program is running.

In Java, the final keyword is used to modify a constant; the declaration method is similar to that of a variable:


final double PI = 3.1415927;

Although constant names can also be lowercase, for ease of identification uppercase letters are usually used to represent constants.

A literal can be assigned to a variable of any built-in type. For example:


byte a = 68;
char a = 'A'

byte, int, long, and short can all be represented in decimal, hexadecimal, and octal forms.

When using literals, the prefix 0 indicates octal, and the prefix 0x represents hexadecimal, for example:


int decimal = 100;
int octal = 0144;
int hexa =  0x64;

Like other languages, Java string constants are also character sequences enclosed between two quotation marks. The following are examples of string literals:


"Hello World"
"two\nlines"
"\"This is in quotes\""

Both string constants and character variables can contain any Unicode character. For example:


char a = '\u0001';
String a = "\u0001";

The Java language supports some special escape character sequences.

sign before positive numbers Character meaning
\n Newline (0x0a)
\r Carriage return (0x0d)
\f Form feed (0x0c)
\b Backspace (0x08)
\0 Null character (0x0)
\s Space (0x20)
\t Tab
" Double quote
' Single quote
\ Backslash
\ddd Octal character (ddd)
\uxxxx Hexadecimal Unicode character (xxxx)

Automatic Type Conversion

Integer, real (constant), and character data can be mixed in operations. During the operation, data of different types are first converted to the same type, then the operation is performed.

Conversion is from lower level to higher level.


低  ------------------------------------>  高

byte,short,char—> int —> long—> float —> double 

Data type conversion must satisfy the following rules:

int i =128;
byte b = (byte)i;

Because the byte type is 8 bits with a maximum value of 127, when an int is cast to byte, the value 128 will cause overflow.

(int)23.7 == 23;         (int)-45.89f == -45

Automatic Type Conversion

The number of bits of the data type before conversion must be lower than that of the data type after conversion. For example: the short data type has 16 bits and can be automatically converted to the 32-bit int type; similarly, the float data type has 32 bits and can be automatically converted to the 64-bit double type.

Example

public

class

ZiDongLeiZhuan

{

public

static

void

main

(

String

[

]

args

)

{

char

c1

=

'

a

'

;

//

Define a char type

int

i1

=

c1

;

//

char automatically converted to int

System

.

out

.

println

(

"

The value after char is automatically converted to int is

"

i1

)

;

char

c2

=

'

A

'

;

//

Define a char type

int

i2

=

c2

1

;

//

char type and int type calculation

System

.

out

.

println

(

"

The value after char and int calculation is

"

i2

)

;

}

}

The running result is:


char自动类型转换为int后的值等于97
char类型和int计算后的值等于66

**Analysis:** The value of c1 is the character a , by looking up the ASCII table we know the corresponding int value is 97, A corresponds to 65, so i2 = 65 + 1 = 66.

Explicit Type Casting

Example

public class ForceTransform { public static void main(String[] args){ int i1 = 123; byte b = (byte)i1;//Cast to byte System.out.println("The value after int is cast to byte is"+b); } }

Running result:

int强制类型转换为byte后的值等于123

Implicit Casting

This section explained Java's primitive data types. The next section will explore different variable types and their usage.