Do you know the type of variable, literal value and type conversion? This chapter mainly talks about the type of variable, literal value and type conversion.
1. What Is a Variable?
The definition of a variable is naming a data identifier.
For example, 2020 is a number, representing a certain year. In java, in order to express the number 2020 with identifiers, we can introduce the concept of variables. The specific code can be expressed as int year=2020;

In this code, we can clearly see the following.
year is an identifier.
= is the assignment operator.
2020 is a numeric value.
; Indicates the end of the line.
In this code, the identifier year is a variable, which represents the value 2020.

2. Basic Variable Types
There are four basic types in Java, integer, character, floating point, and Boolean.
- Integer variables are used to store integers
According to the length of the integer data type, it can be divided into: Byte type with a length of eight bits, short type with a length of sixteen bits, lnt type with a length of thirty-two bits, Long type with a length of 64 bits.
- Character variables are used to store characters
When the char type stores a single character, the value is represented by single quotation marks. When the number of characters stored is more than one, the value is represented by double quotation marks. The specific code example is as follows:
char a1=’t’;//Single quotes can only store one character.
char a2=’tech’//error

- Floating-point variables are used to store floating-point numbers
There are two types of floating-point numbers, namely float type and double type. The float type has a length of 32 bits and the double type has a length of 64 bits. In the process of programming, the default decimal type is double type.
Therefore, float f = 2020.2222 will cause a compilation error, because the default type of 2020.2222 is double, and its type length is 64 bits, which exceeds the length of float by 32 bits. Add a letter f after the number and declare the number directly as a float type float f2 = 54.321f. So that you won’t go wrong.

- Boolean variables are used to store Boolean numbers
Boolean is used to represent true or false, and its length is 1 bit. The specific code is as follows: boolean b1=true; boolean b2=false;

Although the Boolean data actually stored is 0 or 1, 0 means false, 1 means true. However, you cannot directly use 0 or 1 for assignment.
- String type is used to store strings
The String type is actually not a basic type, but a class type.
The specific code is as follows: String str=”dailytechworld”;

3. Literal Value
Literal value is defined as a way to assign values to variables of basic types. The specific code is as follows:
String name;
double defensive;
double armor;
int moveSpeed;

- Integer denomination
When ending with l or L, an integer face value is of type long, otherwise it is of type int.
It is recommended to use uppercase L instead of lowercase l because it is easy to confuse with 1. The values of byte, short, int and long can all be created by int type literals. The literal value of an integer can be expressed in the following four bases:
Decimal: Base 10, containing numbers from 0-9, this is the usual one
Hexadecimal: Base 16, containing numbers from 0-9, and letters from A-F.
Octal: base 8, containing numbers from 0-7
Binary: base 2, contains 0 and 1. (Starting from JAVA7, binary literals can be created)
- Floating point value
When ending with f or F, it represents a floating-point number of type float, otherwise it is of type double (ending with d or D, you can write it or not). Floating point numbers can also be represented by E or e (scientific notation).
- Character and string literals
The literal value of the character is enclosed in single quotes.
The literal value of the string is enclosed in double quotes.
It should be noted that \ means escaping. For example, if you need to indicate a tab character, carriage return, line feed, double quotation marks, etc., you need to use “\t \r \n \”.
4. Type conversion
Data between different types can be converted to each other, but must comply with certain rules.
- Conversion rules
Low-precision data can be converted to high-precision data, but when some high-precision data is converted to low-precision data, data overflow will occur.
- Conversion of low-precision data to high-precision data

The specific code is as follows:
long L=100;
int l=100;
L is of type long, and its type length is 64 bits.
l is of type int, and its type length is 32 bits.
So, the accuracy of L is higher than the accuracy of l.
When this line of code is executed: l = i;
Assign the value of l to L. First, the types of L and l are different from each other, so whether they
can be converted depends on the accuracy of each other.
In this example, low-precision to high-precision conversion can be converted normally.
- Conversion of high-precision data to low-precision data
The specific code is as follows:
byte b = 2;
int i1 = 20;
int i2 = 200;
b = i1;
b=i2;
The type of b is byte, and its length is 8 bits.
The type of i1 is int, and its length is 32 bits.
Sometimes it can be converted, such as b = i1 (i1=10).
Sometimes it can’t be converted. For example, b = i2 (i2=200) ,
The maximum value that can be stored in b can only be 127, which shows that 200 has exceeded 127. At this time, conversion occurs, which will cause data overflow.
If you want to convert, you can only use forced conversion, but the forced conversion will cause data overflow in the future. The code for forced coercion is as follows:
b = (byte) i1;
System.out.println(b);
b =(byte) i2;
System.out.println(b);
Today’s tutorial is over! I believe that through the study of this chapter, you have basically mastered the concept of the type of variable, literal value and type conversion.