Java data types

There are two types of Java data types, basic data types and reference types

Basic data types

The basic data types are integer, floating point, character, and Boolean

Integer types

There are four types of integers: byte, short, int, and long

type The number of bytes Statements range Wrapping classes (reference types)
byte 1 – 127-128 Byte
short 2 – 32768-32767 Short
int 4 – 2147483648-2147483647 Integer
long 8 – 9223372036854775808-9223372036854775807 Long

Floating point types

Floating point types come in two types, float and double

type The number of bytes Statements range Wrapping classes (reference types)
float 4 IEEE 754 Float
double 8 IEEE 754 Double
Single and double?

A single-precision floating-point number occupies four bytes (32 bits) in computer memory and can represent a wide range of values using a “floating point” (floating decimal point) method.

Instead of single-precision floating-point numbers, double-precision floating-point numbers use 64 bits (8 bytes) to store a floating-point number.

Why can’t you use floating point to represent amounts

Because the decimal saved in the computer is actually the decimal approximation, not the exact value, so, do not use floating point numbers in code to represent the amount of important indicators.

It is recommended to use BigDecimal or Long (in minutes) to represent amounts.

The Boolean

type The number of bytes Statements range Wrapping classes (reference types)
boolen 1 false/true Boolen
How to correctly define the interface return value (Boolean /Boolean) type and name (SUCCESS /isSuccess)

When defining a Boolean variable, especially a return value for an externally provided interface, use the name SUCCESS. The Alibaba Java Development Manual recommends using encapsulated classes to define variables in POJO and RPC return values. However, this does not mean that null can be used arbitrarily, and we should avoid null handling as much as possible

Hollischuang. Gitee. IO/tobetopjava…

character

type The number of bytes Statements range Wrapping classes (reference types)
char 2 16-bit Unicode characters Character

Unpacking and packing

The conversion between basic data types and packaging classes is called unboxing or boxing. Wrapping numeric comparisons of objects cannot be done simply with ==, although numbers between -128 and 127 can be used, but outside of that range equals is still required.

Java plastic caching mechanism www.hollischuang.com/archives/11…