Hello, everyone. Last time we talked about the Java Hello World example, this time we will talk about the Java variable type. Stop gossiping and return to the truth. Let’s Talk Android!

We often use variable types when we write programs. This time we are going to talk about variable types in Java. Variable types in Java are similar to those in THE C language we used before. Here’s a look at some of these similar variable types.

  • 1. The integer

There are four types of integers in Java: int, Long, short, and byte. In contrast to C, Java does not have unsigned variables and has an additional byte type. The other types are the same as C, but their ranges are different. The C language does not specify the value range of each type, whereas Java does. Details are as follows:

Long: 64-bit int: 32-bit short: 16-bit byte: 8-bitCopy the code
  • 2. Floating point

There are two floating point types in Java: float and double. These two types are the same as the C language, but their value ranges are different. C does not specify the value range for each type, whereas Java does. Details are as follows:

Doub: 64-bit float: 32-bitCopy the code
  • 3. Character

There are two character types in Java: char and String. Char is the same type as C, while String is a new addition and is an object-oriented type. We’ll talk more about it later when we’re done with object orientation.

  • 4. Boolean type

Java also provides a Boolean type like C: Boolean. Variables of this type have only false and true values.

  • 5. Compound types

In C, there are four compound types: array, Union,struct, and ENmu. In Java, there are only two compound types: array and enum. In addition, classes in Java can also be seen as compound types. Classes are a big part of Java, and object-oriented content is all about classes, which we’ll cover in detail in a separate chapter.

When using variables of various types, we need to pay attention to the value range of variables of each type to avoid overflow. In addition, type conversions occur between variables of various types. There are two types of conversions:

Automatic conversion Forcible conversionCopy the code

Automatic conversion usually involves converting a small range of values to a large range of values. Automatic conversion There is a special case where any type that encounters a String is automatically converted to String.

The cast is not limited by the type. You only need to use parentheses, but you need to pay attention to the range of values of different types to avoid overflow.

You see, about the Java variable type examples we introduced here, to know what other examples, and listen to the next time decomposition!