Home  >  Article  >  Java  >  A brief introduction to java basic data types

A brief introduction to java basic data types

不言
不言forward
2019-02-19 11:59:172761browse

This article brings you a brief introduction to the basic data types of Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Basic data types

There are eight basic data types in JAVA, they are

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

A brief introduction to java basic data types

Under normal circumstances, if an integer number such as 35 appears in JAVA, then this number is of type int. If we want it to be of byte type, we can add a capital B: 35B after the data to indicate that it is of byte type. Similarly, 35S represents short type, and 35L represents long type. If it represents int, we can add nothing, but if we want to represent long type, we must add "L" after the data.

The double type has a larger storage range and higher precision than the float type, so the usual floating point data is of the double type without declaration. If you want to indicate that a data is of the float type, You can add "F" after the data.
Floating point data is not completely accurate, so sometimes the last few decimal places may float during calculation. This is normal.

Automatic type conversion

1) The two types are compatible with each other

2) The space occupied by the converted target type must be larger than the converted source type

Automatic conversion from low byte to high byte (black line indicates automatic data conversion without data loss, red line indicates possible precision loss during conversion)

Forced data conversion

Convert the data type that holds more information into A data type with a smaller capacity may risk precision loss, and the compiler requires the programmer to perform a cast.

Data overflow may occur during forced conversion, so be vigilant.

int a=(int)3.14;

Data type automatic promotion

If one of the two operands is of double type, the other operation will be converted to double type.

Otherwise, if one of the operands is of type float, the other operand will be converted to type float.

Otherwise, if one of the operands is of type long, the other operand will be converted to type long.

Otherwise, both operands are converted to int type.

The above is the detailed content of A brief introduction to java basic data types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete