double不能derefferenced Java equals方法

时间:2021-09-05 16:23:58

I am trying to create an equals() method for a class and I am getting the following error for the last three lines of my "if statement": double cannot be derefferenced. modelName and VINumber are inherited properties. Can someone tell me what I am doing wrong here?

我正在尝试为类创建一个equals()方法,我在“if语句”的最后三行得到以下错误:double不能被解除引用。 modelName和VINumber是继承的属性。谁能告诉我这里我做错了什么?

public boolean equals(FourByFour f){
    boolean status = false;
    if (VINumber.equals(f.VINumber) && 
        modelName.equals(f.modelName) &&
        bayWidth.equals(f.bayWidth) &&
        bayHeight.equals(f.bayHeight) &&
        bayLength.equals(f.bayLength))
    {
        status = true;
    }

    return status;
}

3 个解决方案

#1


1  

Sample Code

double z=100,x=100;
System.out.println("z == x : "+ (z == x));
Double Z = new Double(z), X = new Double(x);
System.out.println("Z.equals(X) : "+ Z.equals(X)+"\n(Z==X) : "+(Z==X));

Output :

z == x : true
Z.equals(X) : true
(Z==X) : false

I hope this example makes things much clearer.

我希望这个例子让事情更加清晰。

You can't use .equals when using primitives (e.g. double) as the data type. The only operators that can be used are ==, !=, <, <=, >, >=. These will give you the boolean results of the comparisons they represent on the basis of the value of the primitive variable.

使用基元(例如double)作为数据类型时,不能使用.equals。唯一可以使用的运算符是==,!=,<,<=,>,> =。这些将根据原始变量的值为您提供它们所代表的比较的布尔结果。

However, if you use Autoboxing by using the java wrapper classes for primitive types, then .equals will give you the arithmetic comparison of your data types, whereas == will compare the variable addresses.

但是,如果您通过使用原始类型的java包装类来使用自动装箱,那么.equals将为您提供数据类型的算术比较,而==将比较变量地址。

#2


0  

Presumably bayWidth, bayHeight, and bayLength are doubles. doubles are primitives, not reference types, and have no methods. Use java.lang.Double.compare to compare double primitives for equals methods.

据推测,bayWidth,bayHeight和bayLength是双打的。双精度数是原语,不是引用类型,也没有方法。使用java.lang.Double.compare比较equals方法的双基元。

#3


0  

simply you can only compare object variable like String with .equals

只是你只能比较像String这样的对象变量和.equals

if you look on .equals method documentation:

如果你看一下.equals方法文档:

public boolean equals(Object anObject)

Parameters:

anObject − the object to compare this String against.

anObject - 要比较此String的对象。

so you can't use this method to compare other primitive datatype like double, float or int because they are not object variables.

所以你不能使用这个方法来比较其他原始数据类型,如double,float或int,因为它们不是对象变量。

you should compare them with == operator

你应该将它们与==运算符进行比较

or you can use Double class constructor to convert double primitive type to a Double object:

或者您可以使用Double类构造函数将双基元类型转换为Double对象:

Double bayWidthobj = new Double(bayWidth);

Double bayHeightobj = new Double(bayHeight);

Double bayLengthobj = new Double(bayLength);

#1


1  

Sample Code

double z=100,x=100;
System.out.println("z == x : "+ (z == x));
Double Z = new Double(z), X = new Double(x);
System.out.println("Z.equals(X) : "+ Z.equals(X)+"\n(Z==X) : "+(Z==X));

Output :

z == x : true
Z.equals(X) : true
(Z==X) : false

I hope this example makes things much clearer.

我希望这个例子让事情更加清晰。

You can't use .equals when using primitives (e.g. double) as the data type. The only operators that can be used are ==, !=, <, <=, >, >=. These will give you the boolean results of the comparisons they represent on the basis of the value of the primitive variable.

使用基元(例如double)作为数据类型时,不能使用.equals。唯一可以使用的运算符是==,!=,<,<=,>,> =。这些将根据原始变量的值为您提供它们所代表的比较的布尔结果。

However, if you use Autoboxing by using the java wrapper classes for primitive types, then .equals will give you the arithmetic comparison of your data types, whereas == will compare the variable addresses.

但是,如果您通过使用原始类型的java包装类来使用自动装箱,那么.equals将为您提供数据类型的算术比较,而==将比较变量地址。

#2


0  

Presumably bayWidth, bayHeight, and bayLength are doubles. doubles are primitives, not reference types, and have no methods. Use java.lang.Double.compare to compare double primitives for equals methods.

据推测,bayWidth,bayHeight和bayLength是双打的。双精度数是原语,不是引用类型,也没有方法。使用java.lang.Double.compare比较equals方法的双基元。

#3


0  

simply you can only compare object variable like String with .equals

只是你只能比较像String这样的对象变量和.equals

if you look on .equals method documentation:

如果你看一下.equals方法文档:

public boolean equals(Object anObject)

Parameters:

anObject − the object to compare this String against.

anObject - 要比较此String的对象。

so you can't use this method to compare other primitive datatype like double, float or int because they are not object variables.

所以你不能使用这个方法来比较其他原始数据类型,如double,float或int,因为它们不是对象变量。

you should compare them with == operator

你应该将它们与==运算符进行比较

or you can use Double class constructor to convert double primitive type to a Double object:

或者您可以使用Double类构造函数将双基元类型转换为Double对象:

Double bayWidthobj = new Double(bayWidth);

Double bayHeightobj = new Double(bayHeight);

Double bayLengthobj = new Double(bayLength);