Java 基本数据类型

八种基本类型、引用类型、常量与自动/强制类型转换。

Beginner
Key Points八种基本类型:byte/short/int/long/float/double/char/boolean;小范围自动转大范围,反向必须强制转换且可能丢精度。

内置数据类型

Java has eight primitive types. They are not objects and store values directly, which makes them faster than wrapper classes. int is the default integer type, double the default floating type, and char a single UTF-16 character.

TypeSizeRange / Value
byte1-128 ~ 127
short2-32768 ~ 32767
int4-2^31 ~ 2^31-1
long8-2^63 ~ 2^63-1,字面量加 L
float4单精度,字面量加 f
double8双精度,默认浮点类型
char2单个字符,用单引号
boolean1true / false
Java
public class Primitives {
    public static void main(String[] args) {
        byte b = 127;
        short s = 32000;
        int i = 2_000_000_000;
        long l = 9_000_000_000L;
        float f = 3.14f;
        double d = 3.1415926;
        char c = 'A';
        boolean ok = true;

        System.out.println(b);
        System.out.println(s);
        System.out.println(i);
        System.out.println(l);
        System.out.println(f);
        System.out.println(d);
        System.out.println(c);
        System.out.println(ok);
    }
}
Output
127
32000
2000000000
9000000000
3.14
3.1415926
A
true

引用类型

Reference types cover classes, interfaces, arrays, enums and String. The variable holds an address rather than the object, and the default value is null. String feels primitive but is an immutable object.

Java
public class Refs {
    public static void main(String[] args) {
        String s = "Java";
        int[] arr = {1, 2, 3};
        Object o = s;

        System.out.println(s.length());
        System.out.println(arr.length);
        System.out.println(o instanceof String);
        System.out.println(s.equals("Java"));
    }
}
Output
4
3
true
true

Java 常量

Declare constants with final; once assigned they cannot change. Name them in UPPER_SNAKE. Only static final is a class-level constant shared by all instances.

Java
public class Consts {
    static final int MAX_SIZE = 100;
    final double PI_APPROX = 3.14;

    public static void main(String[] args) {
        Consts c = new Consts();

        System.out.println(MAX_SIZE);
        System.out.println(c.PI_APPROX);
        System.out.println(Consts.MAX_SIZE);
    }
}
Output
100
3.14
100

自动类型转换

Assigning a narrower type to a wider one promotes automatically: byte → short → int → long → float → double, and char promotes to int. No information is lost, so the compiler does it silently.

Java
public class AutoCast {
    public static void main(String[] args) {
        int i = 100;
        long l = i;
        double d = l;

        char c = 'A';
        int code = c;

        System.out.println(l);
        System.out.println(d);
        System.out.println(code);
    }
}
Output
100
100.0
65

强制类型转换

Narrowing requires an explicit (Type) cast and may lose high bits or the fractional part. Float to integer truncates; it does not round.

Java
public class Cast {
    public static void main(String[] args) {
        double d = 9.99;
        int i = (int) d;

        long big = 300;
        byte b = (byte) big;

        int code = 66;
        char c = (char) code;

        System.out.println(i);
        System.out.println(b);
        System.out.println(c);
    }
}
Output
9
44
B
(byte) 300 yields 44 because the value wraps by taking the low bits (300 - 256 = 44). The compiler does not warn, so overflow is your responsibility.

Friends & Partners