Variables apply for memory to store values. In other words, when creating a variable, you need to apply for space in memory.
The memory management system allocates storage space to variables according to 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 major data types:
Built-in data types
Reference data type
The Java language provides eight basic types. Six numeric types (four integers, two floating point types), a character type, and a Boolean type.
byte 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 to save space in large arrays, mainly replacing integers, because the byte variable occupies only one-quarter of the space of the int type;
Example: byte a = 100, byte b = -50.
short type (short integer type):
The short data type is a 16-bit, signed two's complement integer.
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 is half the space occupied by an int type variable;
The default value is 0;
Example: short s = 1000, short r = -20000.
int type (integer type):
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 type int;
The default value is 0;
Example: int a = 100000, int b = -200000.
long (long integer type):
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 on systems that need to compare large integers;
The default value is 0L;
Example: long a = 100000L, long b = -200000L.
float (single precision floating point type):
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 precise values such as currencies;
Example: float f1 = 234.5f.
double (double precision floating point type):
The double data type is a double-precision, 64-bit, IEEE 754-compliant floating point number;
The default type of floating point numbers is double;
The double type also cannot represent precise values, such as currency;
The default value is 0.0d;
Example: double d1 = 123.4.
boolean:
The boolean data type represents one bit of information;
There are 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 (character type):
The char type is a single 16-bit Unicode character;
The minimum value is 'u0000' (that is, 0);
The maximum value is 'uffff' (that is, 65,535);
The char data type can store any character;
Example: char letter = 'A'.
For the value range of the basic types of numeric types, we do not need to be forced to remember, because their values have already been defined in the corresponding packaging class in the form of constants. Please see the following example:
public class PrimitiveTypeTest {
public static void main(String[] args) {
// byte
System.out.println("Basic type: byte Binary digits: " + Byte.SIZE);
System.out.println("Packaging 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("Basic type: short binary digits: " + Short.SIZE);
System.out.println("Packaging 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("Basic type: int Binary digits: " + Integer.SIZE);
System.out.println("Packaging 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("Basic type: long Number of binary digits: " + Long.SIZE);
System.out.println("Packaging 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("Basic type: float Number of binary digits: " + Float.SIZE);
System.out.println("Packaging 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("Basic type: double Binary digits: " + Double.SIZE);
System.out.println("Packaging 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("Basic type: char Binary digits: " + Character.SIZE);
System.out.println("Packaging class: java.lang.Character");
// Output Character.MIN_VALUE to the console in numerical 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 numerical form instead of character form System.out.println("Maximum value: Character.MAX_VALUE="
+ (int) Character.MAX_VALUE);
}
}
The output of compiling the above code is as follows:
Basic type: byte Binary digits: 8
Wrapping class: java.lang.Byte
Minimum value: Byte.MIN_VALUE=-128
Maximum value: Byte.MAX_VALUE=127
Basic type: short Binary digits: 16
Wrapping class: java.lang.Short
Minimum value: Short.MIN_VALUE=-32768
Maximum value: Short.MAX_VALUE=32767
Basic type: int Binary digits: 32
Wrapping class: java.lang.Integer
Minimum value: Integer.MIN_VALUE=-2147483648
Maximum value: Integer.MAX_VALUE=2147483647
Basic type: long Number of binary digits: 64
Wrapping class: java.lang.Long
Minimum value: Long.MIN_VALUE=-9223372036854775808
Maximum value: Long.MAX_VALUE=9223372036854775807
Basic type: float Number of binary digits: 32
Wrapping class: java.lang.Float
Minimum value: Float.MIN_VALUE=1.4E-45
Maximum value: Float.MAX_VALUE=3.4028235E38
Basic type: double Binary digits: 64
Wrapping class: java.lang.Double
Minimum value: Double.MIN_VALUE=4.9E-324
Maximum value: Double.MAX_VALUE=1.7976931348623157E308
Basic type: char Binary digits: 16
Wrapping 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 the form of scientific notation. The "E+number" at the end means that the number before E should be multiplied by the "number" power of 10. For example, 3.14E3 is 3.14×1000=3140, and 3.14E-3 is 3.14/1000=0.00314.
In fact, there is another basic type void in JAVA, which also has a corresponding packaging class java.lang.Void, but we cannot directly operate on them.
Reference type variables are created by the constructor of a class and you can use them to access the referenced object. These variables are specified as a specific type when declared, such as Employee, Pubby, etc. Once a variable is declared, its type cannot be changed.
Objects and arrays are reference data types.
The default value for all reference types is null.
A reference variable can be used to refer to any compatible type.
Example: Animal animal = new Animal("giraffe").
A constant is a fixed value. They do not require calculation and directly represent the corresponding values.
A constant refers to a quantity that cannot be changed. The final flag is used in Java, and the declaration method is similar to that of variables:
final double PI = 3.1415927;
Although constant names can also be lowercase, for ease of identification, uppercase letters are usually used to represent constants.
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 all be represented in decimal, hexadecimal, and octal notation.
When using constants, the prefix 0 indicates octal, and the prefix 0x indicates hexadecimal. For example:
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
Like other languages, Java's string constants are sequences of characters enclosed between two quotation marks. The following is an example of a string literal:
"Hello World"
"twonlines"
""This is in quotes""
Both string constants and character constants can contain any Unicode character. For example:
char a = 'u0001';
String a = "u0001";
The Java language supports some special escape character sequences.
symbol | Character meaning |
---|---|
n | Line feed (0x0a) |
r | Enter (0x0d) |
f | Form feed character (0x0c) |
b | Backspace (0x08) |
Null character (0x0) | |
s | string |
t | tab character |
" | double quotes |
' | single quote |
\ | backslash |
ddd | Octal characters (ddd) |
uxxxx | Hexadecimal Unicode characters (xxxx) |
This section explains the basic data types of Java. The next section explores the different variable types and their uses.