Java data type

Posted by Daen on Sat, 15 Jan 2022 01:37:54 +0100

  Java has two major data types: built-in data type and reference data type.

Built in data type

   the Java language provides eight basic types, namely six number types (four integer types and two floating-point types), one character type and one boolean type.


   byte data type is an 8-bit, signed integer represented by binary complement. The minimum value is - 128, the maximum value is 127, and the default value is 0.
   byte type is used in large arrays to save space, because byte variables occupy only a quarter of the space of int type:

byte a = 100;
byte b = -50:


   short data type is a 16 bit, signed integer represented by binary complement. The minimum value is - 32768, the maximum value is 32767, and the default value is 0.
   short data type can also save space like byte. A short variable is one half of the space occupied by int variables:

short s = 1000;
short r = -20000;


The     int data type is a 32-bit, signed integer represented by binary complement. The minimum value is - 2147483648, the maximum value is 2147483647, and the default value is 0. General integer variables are of type int by default:

int a = 100000;
int b = -200000;


   long data type is a 64 bit, signed integer represented by binary complement. The minimum value is - 9223372036854775808, the maximum value is 9223372036854775807, and the default value is 0L. L is not case sensitive in theory, but if it is written as l, it is easy to be confused with the number 1, so it is best to use uppercase. This type is mainly used on systems that require relatively large integers:

long a = 100000L;
long b = -200000L;

long data in Java must be followed by L, otherwise it will be parsed as an integer:

long g = (long)9223372036854775807;
long h = (long)-9223372036854775808;
/* perhaps */
long g = 9223372036854775807;
long h = -9223372036854775808;

The following error messages will appear:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The literal 9223372036854775807 of type int is out of range
The literal 9223372036854775808 of type int is out of range

Solution add L after the value:

long value = 9223372036854775807L;


   float data type is a single precision, 32-bit, IEEE 754 compliant floating-point number. The default value is 0.0f. Floating-point numbers cannot be used to represent exact values (such as currency). When float is used in large floating-point arrays, it can save memory space:

float f1 = 234.5f;


  double data type is a double precision, 64 bit, IEEE 754 compliant floating-point number. The default value is 0.0d. The default type of floating-point number is double. The double type also cannot represent an exact value (such as currency):

double d1 = 123.4;


   boolean data type represents one bit information. There are only two values (true and false). The default value is false. This type is only used as a flag to record true/false:

boolean one = true;


The char type is a single 16 bit Unicode character. The minimum value is \ u0000 (i.e. 0) and the maximum value is \ uFFFF (i.e. 65535). The char data type can store any character:

char letter = 'A';

For the value range of basic types of numeric types, we do not need to force to remember, because their values have been defined in the corresponding wrapper class in the form of constants:

public class PrimitiveTypeTest {
    public static void main(String[] args) {
        /* byte */
        System.out.println("Basic type: byte Binary digits:" + Byte.SIZE);
        System.out.println("Packaging: java.lang.Byte");
        System.out.println("Minimum: Byte.MIN_VALUE = " + Byte.MIN_VALUE);
        System.out.println("Maximum: Byte.MAX_VALUE = " + Byte.MAX_VALUE);

        /* short */
        System.out.println("Basic type: short Binary digits:" + Short.SIZE);
        System.out.println("Packaging: java.lang.Short");
        System.out.println("Minimum: Short.MIN_VALUE = " + Short.MIN_VALUE);
        System.out.println("Maximum: Short.MAX_VALUE = " + Short.MAX_VALUE);

        /* int */
        System.out.println("Basic type: int Binary digits:" + Integer.SIZE);
        System.out.println("Packaging: java.lang.Integer");
        System.out.println("Minimum: Integer.MIN_VALUE = " + Integer.MIN_VALUE);
        System.out.println("Maximum: Integer.MAX_VALUE = " + Integer.MAX_VALUE);

        /* long */
        System.out.println("Basic type: long Binary digits:" + Long.SIZE);
        System.out.println("Packaging: java.lang.Long");
        System.out.println("Minimum: Long.MIN_VALUE = " + Long.MIN_VALUE);
        System.out.println("Maximum: Long.MAX_VALUE = " + Long.MAX_VALUE);

        /* float */
        System.out.println("Basic type: float Binary digits:" + Float.SIZE);
        System.out.println("Packaging: java.lang.Float");
        System.out.println("Minimum: Float.MIN_VALUE = " + Float.MIN_VALUE);
        System.out.println("Maximum: Float.MAX_VALUE = " + Float.MAX_VALUE);

        /* double */
        System.out.println("Basic type: double Binary digits:" + Double.SIZE);
        System.out.println("Packaging: java.lang.Double");
        System.out.println("Minimum: Double.MIN_VALUE = " + Double.MIN_VALUE);
        System.out.println("Maximum: Double.MAX_VALUE = " + Double.MAX_VALUE);

        /* char */
        System.out.println("Basic type: char Binary digits:" + Character.SIZE);
        System.out.println("Packaging: java.lang.Character");
        /* Output Character.MIN_VALUE to the console as a number instead of a character */
        System.out.println("Minimum: Character.MIN_VALUE = " + (int) Character.MIN_VALUE);
        /* Output Character.MAX_VALUE to the console as a number instead of a character */
        System.out.println("Maximum: Character.MAX_VALUE = " + (int) Character.MAX_VALUE);

Execution results:

Basic type: byte Binary digits: 8
 Packaging: java.lang.Byte
 Minimum: Byte.MIN_VALUE = -128
 Maximum: Byte.MAX_VALUE = 127

Basic type: short Binary digits: 16
 Packaging: java.lang.Short
 Minimum: Short.MIN_VALUE = -32768
 Maximum: Short.MAX_VALUE = 32767

Basic type: int Binary digits: 32
 Packaging: java.lang.Integer
 Minimum: Integer.MIN_VALUE = -2147483648
 Maximum: Integer.MAX_VALUE = 2147483647

Basic type: long Binary digits: 64
 Packaging: java.lang.Long
 Minimum: Long.MIN_VALUE = -9223372036854775808
 Maximum: Long.MAX_VALUE = 9223372036854775807

Basic type: float Binary digits: 32
 Packaging: java.lang.Float
 Minimum: Float.MIN_VALUE = 1.4E-45
 Maximum: Float.MAX_VALUE = 3.4028235E38

Basic type: double Binary digits: 64
 Packaging: java.lang.Double
 Minimum: Double.MIN_VALUE = 4.9E-324
 Maximum: Double.MAX_VALUE = 1.7976931348623157E308

Basic type: char Binary digits: 16
 Packaging: java.lang.Character
 Minimum: Character.MIN_VALUE = 0
 Maximum: Character.MAX_VALUE = 65535

   the minimum and maximum values of Float and Double are output in the form of scientific notation. The ending e + number represents the power of 10 multiplied by the number before E. For example, 3.14E3 is 3.14 * 10 ^ 3 = 3140, and 3.14E-3 is 3.14 * 10 ^ (- 3) = 0.00314. In fact, there is another basic type void in Java, which also has a corresponding wrapper class java Lang. void, but we can't operate on them directly.

reference type

  in Java, variables of reference type are very similar to C/C + + pointers. The reference type points to an object, and the variable pointing to the object is the reference variable. These variables are specified as a specific type when declared, such as Employee, purchase, etc. Once a variable is declared, the type cannot be changed.
  objects and arrays are reference data types; The default value of all reference types is null; A reference variable can be used to reference any compatible type:

Site site = new Site("Runoob");

A reference type is an object type. Its value is a reference to the memory space (that is, the address). The memory pointed to holds a value or a group of values represented by the variable. Common built-in types are as follows:

int a;
a = 250; /* While declaring variable a, the system allocates space to a */

The reference type is not. Only the reference space is allocated to the variable, but the data space is not allocated, because we don't know what the data is. Examples of errors:

MyDate today; = 4; /* An error occurred because the data space of the today object is not allocated */

After a reference type variable is declared, it must be instantiated to open up the data space to access the object pointed to by the variable:

MyDate today; /* Allocate a space for variables to hold references */
today = new MyDate(); /* First, assign a value to the "myday()" variable, and then execute the "myday()" operation */

Reference variable assignment:

MyDate a, b; /* Open up two reference spaces in memory */
a = new MyDate(); /* Open up the data space of MyDate object and assign the first address of the space to a */
b = a; /* Write the address in the storage space of a to the storage space of b */

Java constants

  constants cannot be modified when the program is running. In Java, the final keyword is used to modify constants. The declaration method is similar to that of variables:

final double PI = 3.1415927;

Although constant names can also be lowercase, uppercase letters are usually used to represent constants for ease of identification.
  literal constants can be assigned to variables of any built-in type:

byte a = 68;
char a = 'A';

byte, int, long and short can be expressed in decimal, hexadecimal and octal. When using constants, the prefix 0 represents octal and the prefix 0x represents hexadecimal:

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

Like other languages, Java string constants are also character sequences contained between two quotation marks:

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

Both string constants and character constants can contain any Unicode character:

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

Java supports some special escape character sequences:

SymbolCharacter meaning
\nLine feed (0x0a)
\rEnter (0x0d)
\fPage feed (0x0c)
\bBackspace (0x08)
\0Null character (0x20)
\scharacter string
\"Double quotation mark
\'Single quotation mark
\dddOctal character (ddd)
\uxxxxHexadecimal Unicode characters (xxxx)

Automatic type conversion

   integer, real (constant) and character data can be mixed. In the operation, different types of data are first transformed into the same type, and then the operation is carried out. Transition from low level to high level:

low --------------------------------------> high
byte/short/char -> int -> long -> float -> double

Data type conversion must meet the following rules:

  • Cannot type convert boolean type.
  • You cannot convert an object type to an object of an unrelated class.
  • Cast must be used when converting a high-capacity type to a low-capacity type.
  • Overflow or loss of accuracy may occur during conversion:
int i = 128;
byte b = (byte)i;

Because the byte type is 8 bits and the maximum value is 127, when int is cast to byte type, the value 128 will cause overflow.

  • The conversion of floating-point numbers to integers is obtained by discarding decimals rather than rounding:
(int)23.7 == 23;
(int)-45.89f == -45;

    the number of bits of the data type before conversion must be lower than that after conversion. For example, if the number of bits of the short data type is 16, the int type with 32 bits can be automatically converted. Similarly, the number of bits of float data type is 32, which can be automatically converted to 64 bit double type:

public class ZiDongLeiZhuan {
    public static void main(String[] args) {
        char c1 = 'a'; /* Define a char type */
        int i1 = c1; /* char Automatic type conversion to int */
        System.out.println("char Automatic type conversion to int The value after is equal to" + i1);
        char c2 = 'A'; /* Define a char type */
        int i2 = c2 + 1; /* char Type and int type calculations */
        System.out.println("char Type and int The calculated value is equal to" + i2);

Execution results:

char Automatic type conversion to int The value after is equal to 97
char Type and int The calculated value is equal to 66

The value of c1 is character a, the corresponding int type value is 97, and the corresponding value of a is 65, so i2 = 65 + 1 = 66.

Cast type

   mandatory type conversion requires that the converted data types must be compatible:

public class QiangZhiZhuanHuan {
    public static void main(String[] args) {
        int i1 = 123;
        byte b = (byte) i1; /* Cast type to byte */
        System.out.println("int Cast type to byte The value after is equal to" + b);

Execution results:

int Cast type to byte The value after is equal to 123

Topics: Java