Java Primitive Data Types
A variable is a request for memory to store values. That is, when a variable is created, space needs to be allocated in memory.
The memory management system allocates storage space for variables based on their 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.
Java's two main data types:
- Built-in Data Types
- Reference Data Types
Built-in Data Types
Java provides eight primitive types. Six numeric types (four integer types, two floating-point types), one character type, and one boolean type.
byte:
- 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 to save space in large arrays, mainly replacing integers because a byte variable occupies only one-fourth of the space of an int variable;
- Example:
byte a = 100
,byte b = -50
.
short:
- 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 short data type can also save space like byte. A short variable occupies half the space of an int variable;
- The default value is
0
; - Example:
short s = 1000
,short r = -20000
.
int:
- 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, the default integer type is int;
- The default value is
0
; - Example:
int a = 100000
,int b = -200000
.
long:
- 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 for systems that require large integers;
- The default value is
0L
; - Example:
long a = 100000L
,long b = -200000L
.
float:
- The float data type is a single-precision, 32-bit, IEEE 754 standard floating-point number;
- float saves 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, IEEE 754 standard floating-point number;
- The default type for floating-point numbers is double;
- double 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 possible values: true and false;
- This type is typically used as a flag to record true/false conditions;
- 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
(65535); - The char data type can store any character;
- Example:
char letter = 'A'
.
Example
For the range of values of the basic numeric types, we do not need to memorize them forcefully, as their values are already defined as constants in the corresponding wrapper classes. See the following example:
Example
public class PrimitiveTypeTest {
public static void main(String[] args) {
// byte
System.out.println("Basic type: byte Binary bit number: " + 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 binary bit count: " + 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 binary bit count: " + 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 binary bit count: " + 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 binary bit count: " + 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 binary bit count: " + 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 binary bit count: " + Character.SIZE);
System.out.println("Wrapper class: java.lang.Character");
// Output Character.MIN_VALUE to the console in numeric form instead of character form
System.out.println("Minimum value: Character.MIN_VALUE=" + (int) Character.MIN_VALUE);
// Output Character.MAX_VALUE to the console in numeric form instead of character form
System.out.println("Maximum value: Character.MAX_VALUE=" + (int) Character.MAX_VALUE);
}
}
Compiling the above code outputs the following results:
Primitive type: byte Binary bit count: 8
Wrapper class: java.lang.Byte
Minimum value: Byte.MIN_VALUE=-128
Maximum value: Byte.MAX_VALUE=127
Primitive type: short Binary bit count: 16
Wrapper class: java.lang.Short
Minimum value: Short.MIN_VALUE=-32768
Maximum value: Short.MAX_VALUE=32767
Primitive type: int Binary bit count: 32
Wrapper class: java.lang.Integer
Minimum value: Integer.MIN_VALUE=-2147483648
Maximum value: Integer.MAX_VALUE=2147483647
Primitive type: long Binary bit count: 64
Wrapper class: java.lang.Long
Minimum value: Long.MIN_VALUE=-9223372036854775808
Maximum value: Long.MAX_VALUE=9223372036854775807
Primitive type: float Binary bit count: 32
Wrapper class: java.lang.Float
Minimum value: Float.MIN_VALUE=1.4E-45
Maximum value: Float.MAX_VALUE=3.4028235E38
Primitive type: double Binary bit count: 64
Wrapper class: java.lang.Double
Minimum value: Double.MIN_VALUE=4.9E-324
Maximum value: Double.MAX_VALUE=1.7976931348623157E308
Primitive type: char Binary bit count: 16
Wrapper class: java.lang.Character
Minimum value: Character.MIN_VALUE=0
Maximum value: Character.MAX_VALUE=65535
The minimum and maximum values of Float and Double are output in scientific notation, where the "E+number" at the end indicates the number before E multiplied by 10 to the power of the number. For example, 3.14E3 is 3.14 × 10^3.
In fact, Java also has another basic type void, which also has a corresponding wrapper class java.lang.Void, but we cannot operate on them directly.
Default Values for Types
The following table lists the default values for various types in Java:
Data Type | Default Value |
---|---|
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
char | 'u0000' |
String (or any object) | null |
boolean | false |
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);
}
}
Example output:
Bool :false
Byte :0
Character:
Double :0.0
Float :0.0
Integer :0
Long :0
Short :0
String :null
Reference Types
In Java, reference type variables are very similar to pointers in C/C++. Reference types point to an object, and variables that point to objects are reference variables. These variables are specified with a particular type upon declaration, such as Employee, Puppy, etc. Once declared, the type cannot be changed.
Objects and arrays are reference data types.
All reference types have a default value of null.
A reference variable can be used to refer to any compatible type.
Example: Site site = new Site("tutorialpro").
---
## Java Constants
Constants cannot be modified during program execution.
In Java, the final keyword is used to define constants, similar to variable declaration:
final double PI = 3.1415927;
Although constant names can be in lowercase, they are typically written in uppercase for easier identification.
Literals can be assigned to variables of any built-in type. For example:
byte a = 68; char a = 'A'
byte, int, long, and short can be represented in decimal, hexadecimal, and octal forms.
When using literals, the prefix `0` denotes octal, and `0x` denotes hexadecimal, for example:
int decimal = 100; int octal = 0144; int hexa = 0x64;
Like other languages, Java string constants are sequences of characters enclosed in double quotes. Examples of string literals:
"Hello World" "two\nlines" "\"This is in quotes\""
String constants and character variables can include any Unicode characters. For example:
char a = '\u0001'; String a = "\u0001";
Java supports special escape sequences.
| Symbol | Character Meaning |
| --- | --- |
| \n | Newline (0x0a) |
| \r | Carriage Return (0x0d) |
| \f | Formfeed (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
Integral, floating-point (constants), and character data types can be mixed in operations. During operations, different types of data are converted to the same type before the operation is performed.
Conversion proceeds from lower to higher types.
Low ------------------------------------> High
byte, short, char —> int —> long —> float —> double
Type conversion must adhere to the following rules:
1. Conversion cannot be performed on boolean types.
2. Object types cannot be converted to unrelated class objects.
3. Casting is required when converting from a larger type to a smaller type.
4. Conversion may result in overflow or loss of precision, for example:
int i = 128;
byte b = (byte)i;
Since the byte type is 8 bits with a maximum value of 127, converting int to byte when the value is 128 causes overflow.
5. Conversion from floating-point to integer truncates the decimal, not rounding, for example:
(int)23.7 == 23;
(int)-45.89f == -45
### Automatic Type Conversion
Conversion must be from a lower bit count to a higher bit count. For example, a short type with 16 bits can be automatically converted to an int type with 32 bits, and a float type with 32 bits can be automatically converted to a double type with 64 bits.
## Example
public class ZiDongLeiZhuan{ public static void main(String[] args){ char c1='a';//Define a char type int i1 = c1;//char auto type conversion to int System.out.println("char auto type conversion to int value equals "+i1); char c2 = 'A';//Define a char type int i2 = c2+1;//char type and int type calculation System.out.println("char type and int calculation value equals "+i2); } }
Running result:
char auto type conversion to int value equals 97 char type and int calculation value equals 66
**Explanation:** The value of c1 is the character **a**, and according to the ASCII table, the corresponding int value is 97, while **A** corresponds to 65. Therefore, `i2 = 65 + 1 = 66`.
### Type Casting
-
1. The condition for conversion is that the data types must be compatible.
-
2. Format: (type)value where type is the data type to which you want to cast.
Example:
## Example
public class TypeCastingExample{ public static void main(String[] args){ int i1 = 123; byte b = (byte)i1; // Casting to byte System.out.println("The value of int after casting to byte is " + b); } }
Output:
The value of int after casting to byte is 123 ```
Implicit Type Casting
-
- The default type for integers is int.
-
- The default type for decimals is double. When defining a float type, you must follow the number with an F or f.
This section covered the basic data types in Java. The next section will discuss different variable types and their usage.