java: data type

Basic data type

byte byte type [-128, 127]
short short integer type [-32728, 32767]
int integer type [about 2.1 billion, most commonly used]
long long integer type [need to end with l or L, such as 23L]

float single-precision floating point [need to end with f or F, such as 23.3f]
double double-precision floating point [decimals are most commonly used]

char character
type boolean boolean

Reference data type

Except for basic data types and null, such as String, array, class, interface..., the default value of reference data type is null

byte short int long float double boolean char

Definition method:

数据类型 变量名 = new 数据类型();

Note: The String type is special because it is optimized by the commonly used JVM String str="hello world";

constant

Use the final keyword to decorate constants in Java, usually using uppercase letters to represent constants

final double PI = 3.1415927;

Type conversion

Automatic type conversion (implicit conversion)

condition:

  • The data types must be compatible, such as numbers (int and long)
  • The target type is greater than the source type (int -> long, not vice versa)

Forced type conversion (explicit conversion)

double e = 12.6;
int a = (int)e;
a // 12

But if the string is converted to a number, it will become the ascll value corresponding to the character

Implicit coercion

  1. The default type of integer is int.
  2. This situation does not exist for floating-point types, because the number must be followed by F or f when defining the float type.

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/113484352