0x0 基础知识
1. '==' 运算符
Java中的数据类型分为基本数据类型和引用数据类型:
- 基本类型:编程语言中内置的最小粒度的数据类型。它包括四大类八种类型
- 4种整数类型:
byte、short、int、long
- 2种浮点数类型:
float、double
- 1种字符类型:
char
- 1种布尔类型:
boolean
- 引用类型:引用也叫句柄,引用类型,是编程语言中定义的在句柄中存放着实际内容所在地址的地址值的一种数据形式,例如:
- 对于基本类型来说,
== 比较的是它们的值
- 对于引用类型来说,
== 比较的是它们在内存中存放的地址(堆内存地址)
举例说明:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public static void main(String[] args) {
//基本数据类型 int num1 = 100; int num2 = 100; System.out.println("num1 == num2 : " + (num1 == num2) + "\n"); //引用类型,其中'System.identityHashCode'可以理解为打印对象地址 String str1 = "mio4"; String str2 = "mio4"; System.out.println("str1 address : " + System.identityHashCode(str1)); System.out.println("str2 address : " + System.identityHashCode(str1)); System.out.println("str1 == str2 : " + (str1 == str2) + "\n"); String str3 = new String("mio4"); String str4 = new String("mio4"); System.out.println("str3 address : " + System.identityHashCode(str3)); System.out.println("str4 address : " + System.identityHashCode(str4)); System.out.println("str3 == str4 : " + (str3 == str4)); } |
运行上面的代码,可以得到以下结果:
| 1 2 3 4 5 6 7 8 9 |
num1 == num2 : true str1 address : 1639705018 str2 address : 1639705018 str1 == str2 :&n
|