May 10, 2021 Java
A variable is a request for memory to store values. That is, when you create a variable, you need to apply space in memory.
The memory management system allocates storage space for variables based on the type of variable, which can only be used to store that type of data.
Therefore, by defining different types of variables, you can store integers, numbers, or characters in memory.
Java's two big data types:
The Java language provides eight basic types. Six numeric types (four integers, two floating-point types), one character type, and one Boolean type.
byte:
short:
Int:
long:
float:
double:
boolean:
char:
For the base type range of numeric types, we don't need to force memory, because their values are already defined as constants in the corresponding wrapper class. Take a look at the following example:
public class PrimitiveTypeTest {
public static void main(String[] args) {
// byte
System.out.println("基本类型:byte 二进制位数:" + Byte.SIZE);
System.out.println("包装类:java.lang.Byte");
System.out.println("最小值:Byte.MIN_VALUE=" + Byte.MIN_VALUE);
System.out.println("最大值:Byte.MAX_VALUE=" + Byte.MAX_VALUE);
System.out.println();
// short
System.out.println("基本类型:short 二进制位数:" + Short.SIZE);
System.out.println("包装类:java.lang.Short");
System.out.println("最小值:Short.MIN_VALUE=" + Short.MIN_VALUE);
System.out.println("最大值:Short.MAX_VALUE=" + Short.MAX_VALUE);
System.out.println();
// int
System.out.println("基本类型:int 二进制位数:" + Integer.SIZE);
System.out.println("包装类:java.lang.Integer");
System.out.println("最小值:Integer.MIN_VALUE=" + Integer.MIN_VALUE);
System.out.println("最大值:Integer.MAX_VALUE=" + Integer.MAX_VALUE);
System.out.println();
// long
System.out.println("基本类型:long 二进制位数:" + Long.SIZE);
System.out.println("包装类:java.lang.Long");
System.out.println("最小值:Long.MIN_VALUE=" + Long.MIN_VALUE);
System.out.println("最大值:Long.MAX_VALUE=" + Long.MAX_VALUE);
System.out.println();
// float
System.out.println("基本类型:float 二进制位数:" + Float.SIZE);
System.out.println("包装类:java.lang.Float");
System.out.println("最小值:Float.MIN_VALUE=" + Float.MIN_VALUE);
System.out.println("最大值:Float.MAX_VALUE=" + Float.MAX_VALUE);
System.out.println();
// double
System.out.println("基本类型:double 二进制位数:" + Double.SIZE);
System.out.println("包装类:java.lang.Double");
System.out.println("最小值:Double.MIN_VALUE=" + Double.MIN_VALUE);
System.out.println("最大值:Double.MAX_VALUE=" + Double.MAX_VALUE);
System.out.println();
// char
System.out.println("基本类型:char 二进制位数:" + Character.SIZE);
System.out.println("包装类:java.lang.Character");
// 以数值形式而不是字符形式将Character.MIN_VALUE输出到控制台
System.out.println("最小值:Character.MIN_VALUE="
+ (int) Character.MIN_VALUE);
// 以数值形式而不是字符形式将Character.MAX_VALUE输出到控制台
System.out.println("最大值:Character.MAX_VALUE="
+ (int) Character.MAX_VALUE);
}
}
The result of compiling the above code output 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 the form of scientific notation, and the "E-number" at the end indicates that the number before E is multiplied by the "number" power of 10. For example, 3.14E3 is 3.14×1000-3140, 3.14E-3 is 3.14/1000-0.00314.
In fact, there is another basic type of void in JAVA, which also has the corresponding wrapper java.lang.Void, but we can't work on them directly.
A constant is a fixed value. They do not need to be calculated and directly represent the corresponding values.
Constants are quantities that cannot be changed. In Java, the final flag is declared in a manner similar to that of a variable:
final double PI = 3.1415927;
Although constant names can also be lowercase, capital letters are often used to indicate constants for ease of recognition.
Literal quantities 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 a hept, 16-step, and 8-step manner.
When constants are used, the prefix 0 indicates 8-step, while the prefix 0x represents 16-step. For example:
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
As with other languages, Java's string constants are a sequence of characters contained between two quotation marks. Here's an example of a literal amount of string type:
"Hello World"
"two\nlines"
"\"This is in quotes\""
String constants and character constants can contain any Unicode character. For example:
char a = '\u0001';
String a = "\u0001";
The Java language supports special escape character sequences.
Symbol | The meaning of the character |
---|---|
\n | Line-in (0x0a) |
\r | Return (0x0d) |
\f | Page break (0x0c) |
\b | Retired (0x08) |
\0 | Empty characters (0x0) |
\s |
String
|
\t | Tabs |
\" | Double quotes |
\' | Single quotes |
\\ | Backslash |
\ddd | Octal Characters (ddd) |
\uxxxx | 16-step Unicode character (xxxx) |
This section explains the basic data types of Java. The next section explores the different variable types and their use.