Java basic data types


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 this type.

memorypic1.jpg

# 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 types


Built-in data types

The Java language provides eight basic types. Six numeric types (four integers, two floating point types), a character type, and a 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 the space occupied by byte variables is only one-quarter of the int type;

  •             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 is half the space occupied by an int type 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, integer variables default to int type;

  •           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 on systems that need to compare 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 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 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 double type;

  •             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:

  •             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’.


Instance

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);  
    }  
}

Run Instance»

Click the "Run Instance" button to view online Example

The output result of compiling 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 the form of scientific notation, and the "E+number" at the end represents the number before E The number should be multiplied to the power of 10. For example, 3.14E3 is 3.14 × 103 =3140, and 3.14E-3 is 3.14 x 10-3 =0.00314.

Actually, 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

  •           In Java, reference type variables are very similar to pointers in C/C++. A reference type points to an object, and a variable pointing to an object is a reference variable. 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: Site site = new Site("php").


Java Constant

Constant is an amount that will not be modified when the program is running.

Use the final keyword in Java to modify constants, and the declaration method is similar to variables:

final double PI = 3.1415927;

Although constant names can also be in lowercase, for ease of identification, capital 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 be represented in decimal, hexadecimal, and octal.

When using constants, the prefix 0 represents octal, and the prefix 0x represents hexadecimal. For example:

int decimal = 100;
int octal = 0144;
int hexa =  0x64;

Like other languages, Java's string constants are also character sequences contained between two quotation marks. The following is an example of a string literal:

"Hello World"
"two\nlines"
"\"This is in quotes\""

Both string constants and character constants can contain any Unicode characters. For example:

char a = '\u0001';
String a = "\u0001";

The Java language supports some special escape character sequences.

##               \n               Line feed (0x0a)             \r               Enter (0x0d)             \f               Form feed character (0x0c)               \b               Backspace (0x08)             \s               Space (0x20)             \t               Tabs \"             Double quotes \'             apostrophe \\               Backslash             \ddd             Octal characters (ddd)             \uxxxx               Hexadecimal Unicode characters (xxxx)
              Symbol             Character meaning

This section explains the basic data types of Java. The next section explores the different variable types and their uses.