Organized memo in the head (Java --Data type)
My Java environment
Java data type
- Primitive type
- Reference type
Primitive type (use the value itself, null cannot be assigned.)
- boolean (default is
false)
- char
- byte
- short
- int
- long (with
l.long value = 10l;)
- float (add
f.float value = 10f;)
- double
Reference type (handles a reference to an instance)
- Object type
- Array type
- Enum
n-ary
- Binary number (
0b starts. ʻint value = 0x0101`)
- Eighth number (
0 starts. ʻint value = 077;`)
- Decimal number (Unspecified. ʻint value = 10; `)
- Hexadecimal (
0x starts. ʻint value = 0x1B;`)
Numerical notation using underscore
- Use at the beginning and end NG (ʻint value = _123;
and ʻint value = 123 _; are NG)
- Use before and after the symbol is NG (ʻint value = 12_.3;
and float value = 123_fand ʻint value = 0x_0100;are NG)
int value = 123_456;
System.out.println(value);
The display result is
123456
Identifier naming convention
- Use of reserved words is NG
- Symbols are underscores (
_) and currency symbols are OK, hyphens (-) are NG
- Starting with a number is NG (ʻint v123; `is OK)
Type inference (var)
Type inference is done at compile time.
OK
var value = 123;
var value = method();
var value = new ArrayList<>();
NG
- Member variables
- Method argument declaration (
public method (var value) {...} is NG)
- Local variables such as
var value;
var value = null;
var value = {1, 2, 3};
var value =Lambda expression
- If the two classes are incompatible
var classA = new ClassA();
classA = new ClassB();
mutable and immutable
mutable
immutable
- String class
- LocalDate class
- Such
Type range
byte < short < int < long < float < double
Baishoiro Fuda
Type conversion
Explicitly cast from large to small.
int value1 = 123;
short value2 = (int)value1;
Identity and equivalence
- Identity (see same instance) ・ ・ ・ Judged by
==
- Equivalence (same value) ・ ・ ・ Judged by ʻequals`