The best Java basic data types Tutorial In 2024, In this tutorial you can learn Built-in data types,Reference types,Java constants,
Variable is to apply memory to store values. That is, when the variable is created when the need to apply for space in memory.
Memory management system according to the types of variables to allocate storage space for variables, the allocated space can only be used to store this type of data.
Therefore, by the definition of the different types of variables that can store integer, decimal, or character in memory.
Java two data types:
Java language provides eight basic types. Six kinds of numeric types (four integers, two floating-point) A character type, there is a Boolean.
byte:
short:
int:
long:
float:
double:
boolean:
char:
For basic types of numeric types in the range, we do not need to remember to force, because their values have been defined as constants in the corresponding wrapper class. Consider 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); } }
Compile the above code output results are 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
Minimum and maximum values Float and Double are in the form of scientific notation output, ending with "E + number" represents a number before E is multiplied by the number of 10-th. For example 3.14E3 is 3.14 × 10 3 = 3140,3.14E-3 is 3.14 x 10 -3 = 0.00314.
In fact, there is additionally a basic JAVA type void, it also has a corresponding wrapper class java.lang.Void, but we can not directly manipulate them.
Constants in the program is running, the amount will not be modified.
With the final keyword in Java modified constants and variables manner similar statement:
final double PI = 3.1415927;
Although the constant name to be lowercase, but in order to facilitate the identification, usually uppercase letters constant.
Literals can be assigned to a variable of any built-in types. E.g:
byte a = 68; char a = 'A'
byte, int, long, short, and can be used in decimal, hexadecimal and octal way to represent.
When using a constant time, the prefix 0? Indicates octal, hexadecimal and prefix 0x representatives. E.g:
int decimal = 100; int octal = 0144; int hexa = 0x64;
And other languages, Java string constants are also included in the sequence of characters between the two quotes. The following are examples of string literals:
"Hello World" "two\nlines" "\"This is in quotes\""
String constants and character constants can contain any Unicode characters. E.g:
char a = '\u0001'; String a = "\u0001";
Java language support for some special escape sequences.
symbol | Character Meaning |
---|---|
\ N | Line feed (0x0a) |
\ R | Carriage return (0x0d) |
\ F | Formfeed (0x0c) |
\ B | Backspace (0x08) |
\ S | Space (0x20) |
\ T | Tabs |
\ " | Double quotes |
\ ' | apostrophe |
\\ | Backslash |
\ Ddd | Octal character (ddd) |
\ Uxxxx | Hexadecimal Unicode characters (xxxx) |
This section explains the basic Java data types. The next section explores the different types of variables and their usage.