英文:
How to compare 2 boolean objects in Java
问题
我正在尝试比较两个布尔值是否具有相同的逻辑值,但只有在比较相同对象时,代码才能正常工作。对于以下代码,输出将为false,我不明白为什么:
public class Logic {
    private boolean bo;
    
    public Logic(boolean bo) {
        this.bo = bo;
    }
    
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        else
            return false;
    }
    
    public static void main(String[] args) {
        Logic l1, l2;
        l1 = new Logic(true);
        l2 = new Logic(true);
        System.out.println(l1.equals(l2));
    }
}
英文:
I am trying to compare if two boolean values have the same logic value, but the code works only if I compare the same object,for the following code the output will be false and I don't understand why:
public class Logic {
	private boolean bo;
	public Logic(boolean bo) {
		this.bo=bo;
	}
	
	public boolean equals(Object obj) {
		if (this==obj)
			return true;
		else
			return false;
	}
	public static void main(String[] args) {
		Logic l1,l2;
		l1=new Logic(true);
		l2=new Logic (true);
		System.out.println(l1.equals(l2));
	}
}
答案1
得分: 1
你在你的 equals 方法中进行的是对象比较,而不是属性比较。正确的方式应该是:
public boolean equals(Logic obj) {
    return this.bo == obj.bo;
}
英文:
You are comparing the objects not the attributes in your equals method. The right way would be:
public boolean equals(Logic obj) {
     return this.bo==obj.bo;
}
答案2
得分: 0
以下是您要翻译的代码部分:
// Boolean类的compare()方法
class GeeksforGeeks {
    // 主方法
    public static void main(String[] args)
    {
        // 第一个值
        boolean a = true;
        // 第二个值
        boolean b = true;
        // 比较方法
        System.out.println(a + " 与 " + b
                           + " 比较结果 = " + Boolean.compare(a, b));
    }
}
或者查看此链接 链接说明在这里
英文:
you can to use this
// compare() method of Boolean class
class GeeksforGeeks {
// Driver method 
public static void main(String[] args) 
{ 
    // first value 
    boolean a = true; 
    // second value 
    boolean b = true; 
    // compare method 
    System.out.println(a + " comparing with " + b 
                       + " = " + Boolean.compare(a, b)); 
} 
}
or check this link enter link description here
答案3
得分: 0
- 当您使用new关键字创建对象时,例如l1,它在堆栈中会有一个引用地址,在堆中与l2引用的对象不同。
 - 如果您需要比较类似于Integer、Boolean等装箱对象,就不应该像那样进行比较。正确的方法是进行拆箱,如下所示:
 
Boolean b1 = new ..., Boolean b2 = new ...;
boolean bb1 = b1;
boolean bb2 = b2;
现在在bb1和bb2之间进行比较;
英文:
- 
When you create the object by using new, as an example l1 would have a reference address in stack which is different object in the heap than l2.
 - 
You should not compare Object like that if you need Boxed Object like Ingeter, Boolean and etc. Right approach to do that unbox it, like below:
Boolean b1 =new .., Boolean b2=new ...;
boolean bb1=b1;
boolean bb2=b2;
Now compare between bb1 and bb2; 
答案4
得分: 0
如你所知,新对象总是在堆空间中创建,并且对这些对象的引用存储在堆栈内存中。当你使用 == 操作符比较两个对象时,它会检查它们的引用是否指向相同的对象?它不会检查对象的内容。
使用你提供的 equal 方法实现(使用 == 比较对象),运行以下代码的输出将会是:
    public static void main(String[] args) {
        Logic l1, l2, l3;
        l1 = new Logic(true);
        l2 = new Logic(true);
        l3 = l1;
        System.out.println(l1.equals(l2));
        System.out.println(l1.equals(l3));
    }
将会是:
false
true
因为 l1 和 l2 指向不同的对象,但是 l1 和 l3 指向堆中的同一个对象。
我们应该重写 equals 方法以便比较内容的相等性。根据Java语言规范,equals(Object) 和 hashCode() 之间存在一个合约:
如果根据
equals(Object)方法两个对象是相等的,那么在这两个对象上调用hashCode方法必须产生相同的整数结果。
有了这些想法,我们可以像这样重写 equals(Object obj) 和 hashCode() 方法:
  @Override
  public int hashCode() {
    return Boolean.hashCode(bo);
  }
  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }
    if (this.getClass() == obj.getClass()) {
      return bo == ((Logic) obj).boValue();
    }
    return false;
  }
  private boolean boValue() {
    return bo;
  }
英文:
As you know new objects are always created in heap space and the references to these objects are stored in stack memory. When you compare two objects using == it checks if their reference are point to the same object or not? It doesn't check objects content.
Using your implementation of equal method (comparing object using ==), the output of running:
    public static void main(String[] args) {
        Logic l1, l2, l3;
        l1 = new Logic(true);
        l2 = new Logic(true);
        l3 = l1;
        System.out.println(l1.equals(l2));
        System.out.println(l1.equals(l3));
    }
would be:
false
true
Because l1 and l2 point to different objects but l1 and l3 point to a same object in heap.
We should override equals method so that it compares equality of contents. According to the Java Language Specification, there is a contract between equals(Object) and hashCode():
> If two objects are equal according to the equals(Object) method, then
> calling the hashCode method on each of the two objects must produce
> the same integer result.
With these in mind we can override equals(Object obj) and hashCode() like this:
  @Override
  public int hashCode() {
    return Boolean.hashCode(bo);
  }
  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }
    if (this.getClass() == obj.getClass()) {
      return bo == ((Logic) obj).boValue();
    }
    return false;
  }
  private boolean boValue() {
    return bo;
  }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论