Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java basic data type


May 10, 2021 Java


Table of contents


Java basic data type

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:

  • Built-in data types
  • Refers to the data type

Built-in 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:

  • Byte data type is 8-bit, signed, integers represented by binary complements;
  • The minimum value is -128 (-2^7);
  • The maximum value is 127 (2-7-1);
  • The default is 0;
  • The byte type is used to save space in large arrays, mainly in place of integers, because the byte variable takes up only a quarter of the space of the int type;
  • Example: byte a s 100, byte b s -50.

short:

  • The short data type is a 16-bit, signed integer represented by a binary complement
  • The minimum value is -32768 (-2^15);
  • The maximum value is 32767 (2^15 - 1);
  • Short data types can also save space like byte. A short variable is one-half of the space occupied by the int variable;
  • The default is 0;
  • Example: short s s s 1000, short r s -20000.

Int:

  • The int data type is a 32-bit, signed integer represented by a binary complement;
  • The minimum value is -2,147,483,648 (-2^31);
  • The maximum value is 2,147,483,647 (2^31 - 1);
  • Generally integer variable defaults to int type;
  • The default is 0;
  • Example: int a s 100000, int b s -200000.

long:

  • The long data type is a 64-bit, signed integer represented by a binary 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 in systems that require large integers;
  • The default is 0L;
  • Example: long a s 100000L, long b s -200000L.

float:

  • Float data type is single-precision, 32-bit, IEEE 754 standard floating point;
  • Float saves memory space when storing large floating-point arrays;
  • The default is 0.0f;
  • Floating points cannot be used to represent precise values, such as currencies;
  • Example: float f1 s 234.5f.

double:

  • Double data type is double, 64-bit, IEEE 754 standard floating point;
  • The default type of floating point is double type;
  • The double type also does not represent an exact value, such as a currency;
  • The default is 0.0d;
  • Example: double d1 s 123.4.

boolean:

  • The boolean data type represents one person's information;
  • There are only two values: true and false;
  • This type records the true/false situation only as a flag;
  • The default is false;
  • Example: boolean one s true.

char:

  • The char type is a single 16-bit Unicode character;
  • The minimum value is 'u0000' (i.e. 0);
  • The maximum value is ''uffff' (i.e. 65,535);
  • char data types can store any character;
  • Example: char letter s 'A'.

Instance

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.


The reference type

  • Reference type variables are created by the constructors of the class and can be used to access referenced objects. T hese variables are specified as a specific type when declared, such as Employee, Pubby, and so on. Once a variable is declared, the 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 type that is compatible with it.
  • Example: Animal animal s new Animal ("giraffe").

Java constant

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.