“===”;Java中的等效
在Java中比较两个值时,如何测试类型和值是否相等 我知道JavaScript中的“===”;Java中的等效,java,operators,Java,Operators,在Java中比较两个值时,如何测试类型和值是否相等 我知道JavaScript中的===可以用来实现这一点,所以我在Java中尝试了这一点,但没有成功 我知道这是一个简单的问题,但我试图查找它,却找不到它是什么。Java中没有truthy和falsy的概念,因此没有严格的比较运算符。如果两个变量不是同一类型的,则无法对它们进行比较,因此在本例中==就足够了。如果它们不可转换,则会引发编译器错误。===在弱类型语言(如Javascript)中很有用,因为它可以验证所比较的对象是否为相同类型,并避免
===
可以用来实现这一点,所以我在Java中尝试了这一点,但没有成功
我知道这是一个简单的问题,但我试图查找它,却找不到它是什么。Java中没有truthy和falsy的概念,因此没有严格的比较运算符。如果两个变量不是同一类型的,则无法对它们进行比较,因此在本例中==就足够了。如果它们不可转换,则会引发编译器错误。
===
在弱类型语言(如Javascript)中很有用,因为它可以验证所比较的对象是否为相同类型,并避免隐式转换
=
在Java等强类型语言中绝对没有用处,因为如果不编写具体的方法,就无法比较不同类型的变量;DR
在Java中没有这样的比较运算符:==
,而是=
或等于
更长的解释
在弱类型语言(如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中,基本上不需要使用==
检查严格性(报告了语法错误)
首先,当您使用=
运算符比较不同类型的值时,编译器会抱怨,并且无法执行
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);
在前面的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
请注意,不能对基元类型调用该方法
一些有用的阅读资料包括:
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以了解更多信息。没有用于比较的===运算符。当您想要比较两个参考时,您应该检查- 1.如果它们指向同一对象
if(firstreference==secondreference)
我制作了一个函数,它在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));
}
}
我能够将任何数据类型的值(数组除外)传递给此函数,并且只有当数据类型和值匹配时才能获得true,否则返回false。派生数据类型,如列表和HashMap也可以使用
对该函数的调用如下所示:
float s1 = 0.f;
float s2 = 0.1f;
System.out.println(compareData(s1, s2)); //Returns false
等等 更新:我也成功地比较了数组,下面是代码片段,但是,我没有深入测试这段代码,而是针对我执行的每个测试用例进行测试
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之前进行以下初始化):
其中,compareData()的函数与本答案前面所述的函数相同
希望这对您有用。:) 如果我们比较JS中的两个变量,我们可以同时使用“==”和“==”。 “==”比较值和“==”也比较类型
var x = 10;
var y = '10';
console.log(x == y) // true
console.log(x === y) // false
在Java中,这会给您带来编译错误,因为类型不一样。我看不出为此编写自己的比较器有什么好处。特别是如果已经有了本机实现 他是你的朋友 它包含很多小助手,比如
Objects.compare(object1, object2, comparator);
Objects.equals(object1, object2);
Objects.deepEquals(object1, object2);
Objects.hash(object1, object2, object3, ...);
我在equals中使用Objects.equals进行覆盖,在hashCode方法中使用Objects.hash。它还为您执行空检查,最终代码看起来非常干净可读
例如:
...
@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);
}
...
a.equals(b)
使用正确编写的equals
方法。在JavaScript中,==
不能用于测试类型和值是否相等<代码>var obj_a={1:2};var obj_b={1:2};console.log(对象a==对象b);//结果为假@LiuXiMin我认为它只对对象无效,但对所有对象都有效else@JavaCakes否。==
在JavaScript中表示严格相等。不能将其用于对象、数组和函数。寻找更多细节。下面的答案或多或少给出了一个解释,但我要说的是不要把苹果和橘子比较:)事实上,我能想到的Java中最接近的类比实际上是.equals()
,如果你在比较字符串或数字(Integer
至Integer
或Double
至<
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);
//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
var x = 10;
var y = '10';
console.log(x == y) // true
console.log(x === y) // false
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);
}
...