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:
- Built-in data types
The two major data types in Java:
Reference data types
byte:
- The Java language provides eight primitive types. Six numeric types (four integer types and two floating-point types), one character type, and one boolean type.
- The byte data type is an 8-bit, signed integer represented in two's complement;
- The minimum value is -128 (-2^7);
- The maximum value is 127 (2^7-1);
- The default value is 0;
- The byte type is used in large arrays to save space, mainly replacing integers, because a byte variable occupies only one quarter of the space of an int type;
short:
- Example: byte a = 100, byte b = -50.
- The short data type is a 16-bit, signed integer represented in two's complement
- The minimum value is -32768 (-2^15);
- The maximum value is 32767 (2^15 - 1);
- The maximum value is 127 (2^7-1);
- The short data type can also save space like byte. A short variable occupies half the space of an int variable;
int:
- Example: short s = 1000, short r = -20000.
- The int data type is a 32-bit, signed integer represented in two's complement;
- The minimum value is -2,147,483,648 (-2^31);
- The maximum value is 2,147,483,647 (2^31 - 1);
- Generally, integer variables default to the int type;
- The default value is 0;
long:
- Example: int a = 100000, int b = -200000.
- The long data type is a 64-bit, signed integer represented in two's complement;
- The minimum value is -9,223,372,036,854,775,808 (-2^63);
- The maximum value is 9,223,372,036,854,775,807 (2^63 -1);
- This type is mainly used in systems that need to handle relatively large integers;
- The default value is 0L;
"L" is theoretically case-insensitive, but if written as "l" it is easily confused with the digit "1" and hard to distinguish. So it is best to use uppercase.
float:
- The float data type is a single-precision, 32-bit floating-point number that complies with the IEEE 754 standard;
- float can save memory space when storing large floating-point arrays;
- The default value is 0.0f;
- Floating-point numbers cannot be used to represent exact values, such as currency;
- Example: float f1 = 234.5f.
double:
- The double data type is a double-precision, 64-bit floating-point number that complies with the IEEE 754 standard;
- The default type of floating-point numbers is the double type;
- The double type also cannot represent exact values, such as currency;
- The default value is 0.0d;
-
Example:
double d1 = 7D ;
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:
- The boolean data type represents one bit of information;
- It has only two values: true and false;
- This type is only used as a flag to record true/false situations;
- The default value is false;
- Example: boolean one = true.
char:
- The char type is a single 16-bit Unicode character;
- The minimum value is \u0000 (decimal equivalent is 0);
- The maximum value is \uffff (i.e., 65535);
- The char data type can store any character;
- Example: char letter = 'A';.
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:
int,short,long,bytehas a default value of 0.charhas a default value of\u0000(empty character).floathas a default value of0.0f.doublehas a default value of0.0d.booleanhas a default value offalse.- The default value of reference types (classes, interfaces, arrays) is
null.
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
- In Java, a reference-type variable is very similar to a pointer in C/C++. A reference type points to an object, and the variable pointing to the object is a reference variable. These variables are declared with a specific type, such as Employee, Puppy, etc. Once declared, the type of a variable cannot be changed.
- Objects and arrays are both reference data types.
- The default value of all reference types is null.
- A reference variable can be used to refer to any compatible type.
- Example: Site site = new Site("Runoob").
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:
-
1. Type conversion cannot be performed on the boolean type.
-
2. An object type cannot be converted into an object of an unrelated class.
-
3. When converting a type with a larger capacity to a type with a smaller capacity, a cast must be used.
-
4. The conversion process may cause overflow or loss of precision, for example:
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.
-
5. Conversion from floating-point to integer is done by discarding the fractional part, not by rounding, for example:
(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
-
1. The condition is that the data types being converted must be compatible.
-
2. Format: (type)value where type is the data type to be cast to. Example:
Example
Running result:
int强制类型转换为byte后的值等于123
Implicit Casting
-
1. The default type of an integer is int.
-
2. A decimal defaults to the double floating-point type; when defining a float type, you must append F or f after the number.
This section explained Java's primitive data types. The next section will explore different variable types and their usage.