问题描述
在Java中是否有类似“是”的东西?根据内存位置可以评估为真的东西吗?
a = 1
b = a
print(id(a))
print(id(b))
# if a is b condition will evaluate to true
# the above print statement gives the same memory ID. In my case: 14374435888
# above assignment b = a is not the same as below
a = 1
b = 1
解决方法
假设您有三个字符串:
String a = "first";
String b = "second";
String c = a;
// false,because they're in different memory locations
System.out.println(a == b);
// true,because they point to the same location in memory
System.out.println(a == c);