Java foundation - Java data type

This article is an original article of Joshua 317. Please note: reprinted from Joshua 317 blog

Java language is a strongly typed language. The values stored in variables by the compiler have appropriate data types. All variables must have their data types clearly defined before they can be used. All variables, expressions and values in Java must have their own types. There is no concept of "untyped" variables.

Learning any programming language needs to understand its data type. Almost all programming languages have data types. What is data type? The existence of software is mainly for data processing. There will be a lot of data in real life, so the programming language classifies them, and then the data of different data types will be allocated different sizes of space for storage. In other words, the function of data types in Java is to guide how much memory space the Java virtual machine should allocate to the variable at the program running stage.




1, Java data type

A variable is a request for memory to store values. That is, when creating variables, you need to apply for space in memory.

The memory management system allocates storage space for variables according to the type of variables, and the allocated space can only be used to store this type of data.

Therefore, you can store integers, decimals, or characters in memory by defining different types of variables.

The Java language supports two types of data: * * Primitive Type) * * and   Reference Type.




2, Basic data types of Java




The Java language provides eight basic data types. The basic data types include boolean, float, char, byte, short, int, long and double. Of which:

Integer type (number without decimal): byte,short,int,long

Floating point type (number with decimal): float,double

Character type (text, single character): char

boolean (true and false): boolean

Note: char represents character type. In fact, character type is also an integer type, which is equivalent to unsigned integer type.

Type namekeywordOccupied memoryValue rangeDefault value
Byte type byte 1 byte -128~127,(-2^7~2^7-1) 0
Short short 2 bytes -32768~32767,(-2^15~2^15-1) 0
integer int 4 bytes -2147483648~2147483647,(-2^31~2^31-1) 0
Long integer long 8 bytes -9223372036854775808L~9223372036854775807L,(-2^63~2^63-1) 0L
Single precision floating point float 4 bytes +/-3.4E+38F (6 ~ 7 significant bits) 0.0f
Double precision floating point double 8 bytes +/-1.8E+308 (15 significant bits) 0.0d
character char 2 bytes ISO single character set '\u0000'
Boolean boolean 1 byte true or false false

Note: one byte occupies 8 bits.

1.1 byte

byte type is the smallest integer type.

byte data type is an 8-bit, signed integer represented by binary complement;

This type is useful when a user processes a data stream from a network or file, or when dealing with raw binary data that may not be directly compatible with other built-in types of Java.

1.2 short

The short data type is a 16 bit, signed integer represented by a binary complement.

The short type restricts the storage of data to the high byte first and then the low byte, which will cause errors in some machines, so this type is rarely used

1.3 integer (int)

The int type is the most commonly used integer type.

The int data type is a 32-bit, signed integer represented by binary complement;

1.4 long

long data type is a 64 bit signed integer represented by binary complement;

Large programs often encounter large integers. When the range represented by int type is exceeded, long type should be used.

1.5 floating point types (float and double)

Floating point type is a data type with decimal part, also called real type. Floating point data includes single precision floating point (float) and double precision floating point (double), representing numbers with decimal precision requirements.

The main difference between single precision floating-point type (float) and double precision floating-point type (double) is the size of memory occupied. Float type occupies 4 bytes of memory space and double type occupies 8 bytes of memory space. Double precision type double has higher precision and larger representation range than single precision type float.

Java's default floating-point type is double. For example, 11.11 and 1.2345 are double values. If you want to specify a float type value, you need to append the letter F or F after it. For example, 11.11f and 1.2345F are constants of float type.

For example, you can declare a variable of type float and assign an initial value as follows.

float price = 10.2f; // Define float type and assign initial value

You can also use any of the following methods to declare a variable of type double and assign an initial value.

double price = 11.254d; // Define a variable of type double and assign an initial value

double price = 11.254; // Define a variable of type double and assign an initial value

Note: if a value can really be regarded as a float, it must end with F (or F) after buffer; otherwise, it will be regarded as double value. For double value, D (or D) after buffer is optional.

1.6 boolean type

Boolean type is used to determine whether the result is true or false through logical operation on two numeric values. In Java, the reserved words true and false are used to represent true and false in logical operation. Therefore, a boolean type variable or expression can only take one of true and false values.

In the Java language, boolean values cannot be converted to any data type. The true constant is not equal to 1, and the false constant is not equal to 0. These two values can only be assigned to variables declared as boolean or used in boolean expressions.

1.7 character type

The character type (char) in ava language is represented by two byte Unicode encoding. It supports all languages in the world. Char type can be assigned by single quotation mark characters or integers.

General computer languages use ASCII coding, with a byte representing a character. ASCII code is a subset of Unicode code. When Unicode is used to represent ASCII code, its high byte is 0, which is the first 255 characters.

Unicode characters are usually represented in hexadecimal. For example, "\ u0000"~"\u00ff" represents ASCII code set. "\ U" represents escape character, which is used to indicate that the next four hexadecimal digits are Unicode code.

The type of character type variable is char, which is used to represent a single character, for example:

char letter = 'A';
char numChar = '1';

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

package com.joshua317;

public class Main {

    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");
        // Set character.min as a numeric value instead of a character_ Value output to console
        System.out.println("Minimum: Character.MIN_VALUE="
                + (int) Character.MIN_VALUE);
        // Convert character.max to numeric instead of character_ Value output to console
        System.out.println("Maximum: Character.MAX_VALUE="
                + (int) Character.MAX_VALUE);

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

3, Java reference data type




Java reference data types are based on basic data types, including arrays, classes and interfaces. A reference data type is a reference to an object, a reference type variable is a pointer, and a variable pointing to an object is a reference variable, but the term pointer is no longer used in the Java language. In addition, pointer types, structure types, union types, and enumeration types in C + + are not supported in the Java language.

Reference types also have a special null type. Empty type (null type) is the type of null value. This type has no name. Because a null type has no name, it is impossible to declare a variable of null type or convert to null type. An empty reference (null) is the unique value of a null type variable. An empty reference (null) can be converted to any reference type. In actual development, null type can be ignored, assuming that null is only a special direct quantity of reference type.

Note: null references can only be converted into reference types, not basic types. Therefore, do not assign a null value to a variable of basic data type.


This article is an original article of Joshua 317. Please note: reprinted from Joshua 317 blog

Tags: Java

Posted on Mon, 01 Nov 2021 09:35:53 -0400 by steveclondon