The difficulty

primary

Learning time

30 minutes

Suits the crowd

Zero basis

Development of language

Java

The development environment

  • JDK v11
  • IntelliJ IDEA v2018.3

1. What is the data type?

What is the data? Like 123, 3.14, “Do you play games?” These are all data:

So, what are their data types? For example, 123 is an integer, 3.14 is a decimal, “Do you play games?” Is a sentence. Their corresponding data types are:

In computers, a data type is defined in a data structure as a set of values and a set of operations defined on that set of values. For example, if you define a variable of type int, you can store either 1 or 2.

Variables are places where values are stored. They have names and data types.

The data type of a variable determines how the bits representing these values are stored in the computer’s memory.

When declaring a variable, you can also specify its data type. All variables have data types to determine what kind of data can be stored.

Before we learned about variables, we have to declare them before we use them. To declare them, we have to specify the type of variables. So what types of variables can there be?

2. Eight basic data types

There are eight basic data types in Java:

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char

Classify the eight basic data types as follows:

Integers: byte, short, int, and long

Decimal: float, double

True or false: a Boolean

Character: char

Let’s go through them one by one.

3.byte

The byte data type is an 8-bit signed binary complement integer, consisting of 1 byte.

Its minimum value is -128 and its maximum value is 127 inclusive.

Byte data applications. You are advised to use byte data when storing weeks and months.

4.short

The short data type is a 16-bit signed binary complement integer, comprising two bytes.

Its minimum value is -32,768 and its maximum value is 32,767 inclusive.

Short data type application, we suggest that you can use the short type when storing the age of people or animals.

5.int

By default, the int data type is a 32-bit signed binary complement integer, accounting for 4 bytes.

The minimum is -2 to the 31st, and the maximum is 2 to the 31st minus 1.

Int is the default data type for integers.

Int data type application, we recommend that you can use int when storing the age of dinosaur fossils.

6.long

The long data type is a 64-bit binary complement integer of 8 bytes.

The minimum value of signed long is -2 to the 63rd power, and the maximum value is 2 to the 63rd power -1.

Long data type application, it is recommended that we store the age of the sun can use long type.

When declaring a variable of type long, you can add a lowercase “L” or an uppercase “L” after the value to represent a number of type long. The uppercase “L” is recommended because the lowercase “L” is too close to the number 1 to confuse it.

7.float

The float data type is a single-precision floating point number of four bytes.

Float minimum and maximum values in the following range:

Other requirements for high precision, such as currency. To do this, you need to use the java.math.BigDecimal class.

Float data type application, we recommend that you can use float when storing PI.

When declaring a variable of type float, follow the value with a lowercase “f” or an uppercase “f” to indicate that you want your decimals to be stored as float.

8.double

The double data type is a double-precision floating point number of 8 bytes.

The minimum and maximum values of type double range as follows:

Double is the default type for decimals, so when you write decimals, it defaults to double.

The double data type can be used when storing more precise data.

When declaring a variable of type double, we can add either a lowercase “D” or an uppercase “D” to the value to represent a number of type double.

9.boolean

Boolean Data types have only two values: true and false. Use this data type as a simple flag to track true/false conditions.

Boolean data type application. It is recommended that you use Boolean when storing true or false data.

10.char

Char data type is a single 16-bit Unicode character, consisting of two bytes.

Its minimum value is ‘\u0000’ (or 0) and its maximum value is ‘\ uFFFF ‘(or 65,535 inclusive).

It is recommended that you use the char type when storing data of one character, such as gender.

11.String

In addition to the eight primitive data types listed above, the Java programming language provides special support for strings through the java.lang.String class.

Enclosing a String in double quotes automatically creates a new String; For example, String name = “Tom “;

Strings are immutable, which means that their values cannot be changed once they are created.

String is not a basic data type. See the following sections for more on strings.

12. The default value

What is a default value?

If you don’t initialize the field, the system will give it an initial value.

One thing to note here is that only fields get the treatment that the system gives them a default value.

What do you mean?

Fields have default values, variables do not.

What is a field?

Only the fields in the class, the class has no contact, do not understand, it does not matter, the subsequent article will explain. To create a New Class, right-click the main folder, select “New”, and click “Java Class” :

Enter the Class name, select “Class”, and click “OK” when done:

Write “Student. Class” :

Notice that the “name” is the field. Ok, let’s go back to main. class and see what a variable is again:

You see, age is a variable, and it’s a local variable, because it’s declared in the method body, and local variables don’t have default values. As we all know, variables need to be initialized before they can be used, and if we only declare the age variable at this point, it can’t be used.

Create a new student. class object that does not initialize the fields.

The student object is created using the age variable in student:

Running results:

We can see:

  1. The age in the Student object has been initialized by the system.
  2. The default value of the age variable of type int is 0.

Int default = 0 int default = 0 int default = 0

Note that the figure above lists the default values for the data types in the fields.

What is a local variable? Local variables are variables declared in methods.

Local variables are slightly different; The compiler never assigns default values to uninitialized local variables. If you cannot initialize the local variable that declares it, be sure to assign a value to it before trying to use it. Accessing an uninitialized local variable causes a compile-time error.

With that said, the basic Data types in Java are over, but stay tuned for more.

Answering questions

If you have questions or want to learn more about cutting-edge technology, please leave them in the comments below, and I’ll answer them for you.

The previous chapter

Full Stack 2019 Java Chapter 12: Variables

The next chapter

Full Stack 2019 Java Chapter 14: Binary, Octal, hexadecimal

A study group

Join a synchronous learning group for mutual communication and progress.

  • Method 1: Follow the headline number gorhaf, private message “Java study Group”.
  • Method 2: follow the public account gorhaf, reply “Java learning group”.

Full stack engineer learning program

Follow us and join the “Full stack Engineer Learning Program”.

Copyright statement

Original is not easy, shall not be reproduced without permission!