Java 基本数据类型
八种基本类型、引用类型、常量与自动/强制类型转换。
内置数据类型
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.
| Type | Size | Range / Value |
|---|---|---|
| byte | 1 | -128 ~ 127 |
| short | 2 | -32768 ~ 32767 |
| int | 4 | -2^31 ~ 2^31-1 |
| long | 8 | -2^63 ~ 2^63-1,字面量加 L |
| float | 4 | 单精度,字面量加 f |
| double | 8 | 双精度,默认浮点类型 |
| char | 2 | 单个字符,用单引号 |
| boolean | 1 | true / false |
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);
}
}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.
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"));
}
}4
3
true
trueJava 常量
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.
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);
}
}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.
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);
}
}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.
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);
}
}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.内置数据类型
Java 有八种基本类型,它们不是对象、直接存值,因此比包装类更快。int 是默认整数类型,double 是默认浮点类型,char 是单个 UTF-16 字符。
| 类型 | 字节 | 范围 / 取值 |
|---|---|---|
| byte | 1 | -128 ~ 127 |
| short | 2 | -32768 ~ 32767 |
| int | 4 | -2^31 ~ 2^31-1 |
| long | 8 | -2^63 ~ 2^63-1,字面量加 L |
| float | 4 | 单精度,字面量加 f |
| double | 8 | 双精度,默认浮点类型 |
| char | 2 | 单个字符,用单引号 |
| boolean | 1 | true / false |
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);
}
}127
32000
2000000000
9000000000
3.14
3.1415926
A
true引用类型
引用类型包括类、接口、数组、枚举,以及 String。变量存的是对象地址而非对象本身,默认值是 null。String 虽然用起来像基本类型,本质是不可变对象。
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"));
}
}4
3
true
trueJava 常量
用 final 声明常量,赋值后不能再改。常量名惯例全大写、下划线分隔。static final 才是类级常量,所有对象共享一份。
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);
}
}100
3.14
100自动类型转换
小范围类型赋给大范围类型会自动提升,顺序是 byte → short → int → long → float → double,char 可提升为 int。这种转换不丢信息,编译器自动完成。
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);
}
}100
100.0
65强制类型转换
大范围转小范围必须显式加 (类型),可能丢失高位或小数部分。浮点转整数是直接截断,不是四舍五入。
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);
}
}9
44
B(byte) 300 得到 44 是因为 300 超出 byte 范围后按低位截断(300 - 256 = 44)。这类溢出编译器不会报错,需要自己注意。