在Java中比较两个值时,如何测试它们的类型和值是否相等?
我知道在JavaScript中可以使用===
来实现这一点,所以我尝试在Java中使用了它,但是没有成功。
我知道这是一个简单的问题,但我已经尝试过搜索,却没有找到答案。
简述
在Java中,不存在这样的比较运算符:===
,但可以使用==
或equals
。
更详细的解释
在JavaScript等弱类型语言中,您可以使用严格比较运算符(===
),因为该语言允许比较具有不同类型的变量。
例如,在JavaScript中,如果您这样做,就不会得到编译错误:
var x = 10;
var y = 'foo';
console.log(x == y); // false
当你想要比较可能持有“相等”但类型不同的值的变量时,它是有用的。
例如
var x = 10;
var y = '10';
console.log(x == y) // true
console.log(x === y) // false
在像Java这样的强类型语言中,您不需要使用严格比较运算符,因为该语言已经“处理”了类型比较。int x = 10;
String y = "10";
System.out.println("10" == y); // true
System.out.println(x == y); // compile error : Incompatible operand types int and String
总的来说,在Java中,使用===
进行严格检查是没有必要的(会报告语法错误)。
首先,当您使用==
运算符比较不同类型的值并且无法执行转换时,编译器会报错。
在上面Java代码的示例中,如果您想要比较x
和y
,可以使用equals
:
int x = 10;
String y = "10";
System.out.println(y.equals(x)); // compile warning: Unlikely argument type for equals(): int seems to be unrelated to String
值得一提的是,请注意,equals
方法不能在基本类型上调用。
一些有用的阅读材料:
我创建了一个函数,它在Java中复制了Javascript === 的功能
static boolean compareData(Object v1, Object v2)
{
if(v1 != null && v2 != null)
return (v1.getClass() == v2.getClass() && (v1.toString().equals(v2.toString())));
else
{
return (v1 == null ? v2 == null : v1.equals(v2));
}
}
float s1 = 0.f;
float s2 = 0.1f;
System.out.println(compareData(s1, s2)); //Returns false
float s1 = 0.0f;
float s2 = 0.0f;
System.out.println(compareData(s1, s2)); //Returns true
float s1 = 0.1f;
String s2 = "0.1f";
System.out.println(compareData(s1, s2)); //Returns false
String s1 = "sdf";
String s2 = null;
System.out.println(compareData(s1, s2)); //Returns false
String s1 = null;
String s2 = null;
System.out.println(compareData(s1, s2)); //Returns true
更新:我还成功地比较了数组,以下是代码片段,但我还没有对此代码进行全面测试,不过它已经适用于我进行的每个测试用例。
if(s1 != null && s2 != null)
if(s1.getClass().isArray() && s2.getClass().isArray())
compareDatab = s1.getClass().equals(s2.getClass()) && (Arrays.toString(s1).equals(Arrays.toString(s2)));
else
compareDatab = compareData(s1, s2);
else
compareDatab = compareData(s1, s2);
使用上述代码片段(在上述代码片段之前应完成以下初始化,smh: P):
//s1 and s2 can be anything including Arrays and non-Array...
int[] s1 = {1,2,3};
int[] s2 = {1,2,3};
//compareDatab gives true
int[] s1 = {1,2,4};
int[] s2 = {1,2,3};
//compareDatab gives false
float[] s1 = {1,2,3};
int[] s2 = {1,2,3};
//compareDatab gives false
compareData()是此答案中之前提到的相同函数。
希望这对你有用。 :)
Java没有真值和假值的概念,因此也没有严格比较运算符。
!(false == 0)
是真的。 - JavaCakes===
在弱类型语言中非常有用,比如Javascript,因为它可以验证被比较的对象是否为相同类型并避免隐式转换。
===
在强类型语言中完全没有用处,例如Java,因为您不能比较不同类型的变量,除非编写一个专门的方法来执行此操作。
public class MyClass {
private final int val;
private final String name;
public MyClass(int val, String name) {
this.val = val;
this.name = name;
}
public int getVal() { return val; }
public String getName() { return name; }
public boolean equals(Object o) {
if(o == null) return false;
if(this == o) return true;
if(!this.getClass().getSimpleName().equals(o.getClass().getSimpleName()) return false;
MyClass other = (MyClass) o;
return this.getVal() == other.getVal() && this.getName().equals(other.getName());
}
public int hashCode() { ... }
}
此外,请查看官方Java API以获取更多信息 https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html。
我认为自己编写比较器并没有什么好处,特别是如果已经有原生实现的话。
java.util.Objects是你的朋友。
它包含了许多小助手,例如
Objects.compare(object1, object2, comparator);
Objects.equals(object1, object2);
Objects.deepEquals(object1, object2);
Objects.hash(object1, object2, object3, ...);
...
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Customer)) {
return false;
}
Customer that = (Customer) o;
return Objects.equals(firstName, that.firstName)
&& Objects.equals(lastName, that.lastName)
&& Objects.equals(street, that.street)
&& Objects.equals(houseNumber, that.houseNumber)
&& Objects.equals(postalCode, that.postalCode)
&& Objects.equals(city, that.city)
&& Objects.equals(emailAddress, that.emailAddress);
}
@Override
public int hashCode() {
return Objects.hash(firstName,
lastName,
street,
houseNumber,
postalCode,
city,
emailAddress);
}
...
在比较两个引用时,没有“===”运算符可供使用。您应该检查以下内容:
原始答案:最初的回答。
if(firstreference==secondreference)
if (secondreference instanctof classoffirstreference)
firstreference.property1.equals(secondreference.property1)
//do this for all properties.
var x = 10;
var y = '10';
console.log(x == y) // true
console.log(x === y) // false
equals
方法进行比较a.equals(b)
。 - luk2302===
不能用于测试类型和值是否相等。var obj_a = {1: 2}; var obj_b = {1: 2}; console.log(obj_a === obj_b); // 结果为false
- LiuXiMin===
在 JavaScript 中表示严格相等
。你无法用它来比较对象、数组和函数。在这里查看更多细节。 - LiuXiMin===
的类比是.equals()
,如果你正在比较String
或数字(Integer
与Integer
或Double
与Double
),但对于原始类型和其他引用类型,它是==
,但如果比较Integer
和Double
(不同的装箱类型),则没有相应的等价物。总之,试着理解每个运算符在你使用的特定语言中是如何工作的。在两种不同的语言之间进行类比可能会更加混乱而不是有帮助。 - M A