[JavaSE column 5] Java basic data types and value range

Author homepage: Designer Xiao Zheng
About the author: A Java full-stack software engineer, from Ningbo, Zhejiang, responsible for the development and management of the company's OA projects, focusing on front-end and back-end software development (Vue, SpringBoot and WeChat applets), system customization, and remote technical guidance. Certified lecturer of CSDN Academy and Blue Bridge Cloud Class, high-quality creator in the full-stack field. Love technology, focus on business, open cooperation, willing to share, look forward to growing up with you and me!
Main direction: Vue, SpringBoot, WeChat applet

1. Data types in Java

In computer science and computer programming, a data type, or simply type, is an attribute of data that tells the compiler or interpreter how the programmer intends to use the data. Most programming languages ​​support the basic data types of integers (various sizes), floating point numbers (approximate real numbers), characters, and booleans. The value of a data type constraint expression, such as a variable or function, may be required. This data type defines the operations that can be performed on the data, what the data means, and how values ​​of that type are stored. A data type provides a set of values ​​from which an expression (that is, a variable, function, etc.) can obtain its value.

Data types are used in a type system, which provides various ways to define, implement, and use them. Different type systems ensure different degrees of type safety.

Almost all programming languages ​​explicitly include the concept of data types, although different languages ​​may use different terminology.

1.1 Storage unit

Before learning data types, you first need to learn what is a storage unit.

In a computer, each block of memory is divided into the smallest storage units.

Bit (bit) is the most basic unit in a computer, and a bit is the most basic concept. In a computer, since only logic 0 and logic 1 exist, that is, each logic 0 or 1 is a bit.

Each smallest storage unit can only store 0 or 1, which is called bit (bit). Eight bits equal one byte. The unit conversion is as follows.

  • 8 bit = 1 Byte one byte
  • 1024 B = 1 KB (KiloByte) Kilobytes
  • 1024 KB = 1 MB (MegaByte) megabyte
  • 1024 MB = 1 GB (GigaByte) gigabytes
  • 1024 GB = 1 TB (TeraByte) Terabyte
  • 1024 TB = 1 PB (PetaByte) petabyte
  • 1024 PB = 1 EB (ExaByte) Exabytes
  • 1024 EB = 1 ZB (ZetaByte) Zetabytes
  • 1024 ZB = 1 YB (YottaByte) YottaByte
  • 1024 YB = 1BB (Brontobyte) per byte
  • 1024 BB = 1 NB (NonaByte) Nona byte
  • 1024 NB = 1 DB (DoggaByte) knife byte

Smart friends will understand why the actual download speed of 100 M broadband is only 12.5 MB/S after reading the above formula.

A byte is a unit of measurement used by computer information technology to measure storage capacity. A byte represents the data type and language character in a computer programming language. In a computer, eight bits are regarded as a byte, and a byte is a small unit of information. , and participate in the operation as a whole. In a microcomputer, the storage capacity of the memory is expressed in bytes.

1.2 Java basic data types

There are three types of basic data types in Java, which are numeric, character, and Boolean.

Among them, the numerical type is divided into integer byte, short, int, long, and floating point number float, double, a total of 8 types, as shown in the figure below.

Tip: In addition to basic data types in Java, there are also reference data types, including classes, interfaces, and arrays, which will be explained in later lessons.

Second, the value range of Java

Before learning the value range of each data type in Java, you must first learn how to define basic variables.

2.1 Variable definition

first reference "JavaSE column 3] JDK installation, IntelliJ IDEA installation, configuration environment variables" Complete the creation of the basic project, and this paragraph will not be prompted in the following lessons.

Int eger

First define an integer type int, and assign a value of 1, the code is as follows.

public class Main {
    public static void main(String[] args) {

        int a = 1;
        System.out.println(a);
    }
}

integer short

Define an integer short and assign it a value of 2, the code is as follows.

public class Main {
    public static void main(String[] args) {

        short a = 2;
        System.out.println(a);
    }
}

integer byte

Define an integer byte and assign a value of 3, the code is as follows.

public class Main {
    public static void main(String[] args) {

        byte a = 3;
        System.out.println(a);
    }
}

integer long

Define an integer long and assign it a value of 4. Please note that the suffix L is required, otherwise it defaults to int. The code is as follows.

public class Main {
    public static void main(String[] args) {

        long a = 4L;
        System.out.println(a);
    }
}

floating point float

Define a floating-point type float and assign it a value of 5.0. Please note that the suffix f is required, otherwise it defaults to double. The code is as follows.

public class Main {
    public static void main(String[] args) {

        float a = 5.0f;
        System.out.println(a);
    }
}

floating point double

Define a floating-point double and assign it a value of 6.0, the code is as follows.

public class Main {
    public static void main(String[] args) {

        double a = 6.0;
        System.out.println(a);
    }
}

character type char

Define a character type char, and assign the value 'a', the code is as follows.

public class Main {
    public static void main(String[] args) {

        char a = 'a';
        System.out.println(a);
    }
}

Boolean boolean

Define a Boolean boolean and assign it as true, the code is as follows.

public class Main {
    public static void main(String[] args) {

        boolean a = true;
        System.out.println(a);
    }
}

There is also a way to define a wrapper class in Java, and the effect is basically the same. For example, int a = 1 can also be defined as Integer a = 1.
Wrapper classes have different storage locations, initial values, and usage methods.
The concept, usage, and automatic boxing and unboxing of packaging classes will be covered in subsequent lessons.

2.2 Value range verification

The results are displayed first before validation, as shown in the figure below.

The value range verification code is as follows:

public class Main {
    public static void main(String[] args) {

        System.out.println("Byte maximum value = " + Byte.MAX_VALUE);
        System.out.println("Byte maximum value = " + Byte.MIN_VALUE);

        System.out.println("Short maximum value = " + Short.MAX_VALUE);
        System.out.println("Short maximum value = " + Short.MIN_VALUE);

        System.out.println("Int maximum value = " + Integer.MAX_VALUE);
        System.out.println("Int maximum value = " + Integer.MIN_VALUE);

        System.out.println("Long maximum value = " + Long.MAX_VALUE);
        System.out.println("Long maximum value = " + Long.MIN_VALUE);

        System.out.println("Float maximum value = " + Float.MAX_VALUE);
        System.out.println("Float maximum value = " + Float.MIN_VALUE);

        System.out.println("Double maximum value = " + Double.MAX_VALUE);
        System.out.println("Double maximum value = " + Double.MIN_VALUE);

        System.out.println("Char maximum value = " + Character.MAX_VALUE);
        System.out.println("Char maximum value = " + Character.MIN_VALUE);

        System.out.println("boolean real = " + Boolean.TRUE);
        System.out.println("boolean Fake = " + Boolean.FALSE);
    }
}

output result

Byte maximum value = 127
Byte maximum value = -128
Short maximum value = 32767
Short maximum value = -32768
Int maximum value = 2147483647
Int maximum value = -2147483648
Long maximum value = 9223372036854775807
Long maximum value = -9223372036854775808
Float maximum value = 3.4028235E38
Float maximum value = 1.4E-45
Double maximum value = 1.7976931348623157E308
Double maximum value = 4.9E-324
Char maximum value = 
Char maximum value =  
boolean real = true
boolean Fake = false

process has ended,exit code 0

In addition, you can hold down Ctrl + left mouse button in IntelliJ IDEA to view the source code of the class.

If a variable beyond the value range is defined, IntelliJ IDEA will give a compilation prompt and fail to compile, as shown in the figure below.

3. Lesson summary

In this class, first learn the basic concepts of data types and storage units, and thus derive the eight basic data types of Java, and explain the definition methods and value ranges of these eight data types. In the next class, I will explain the basic type conversion of Java and the knowledge of automatic unboxing.

Tags: Java data structure JavaSE programming language

Posted by Grofit on Wed, 25 Jan 2023 14:12:53 +1030