Java对象相等不起作用 [英] java object equality not working

查看:53
本文介绍了Java对象相等不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的试图在该论坛中搜索有关此类问题的答案,但到目前为止,似乎没有任何一项工作.

I really tried searching for answer in this forum for such a question but none seems to work so far.

我想输入检查方法声明,例如:

I want to type check a method declaration such as:

public int stackOverFlow() {int a; a = a + 1; return 0;}

返回表达式的类型必须与方法的返回类型匹配(在此示例中为true).

The type of the return expression must match the return type of the method(which in this example is true).

我使用Java Tree Builder为语法中的所有非终结符(以节点的形式)和默认的深度优先访问者生成语法树.

I use Java Tree Builder which generates syntax trees for all my non-terminals in my grammar(in the form of nodes) and a default depth-first visitor.

我有一个实现节点接口的MethodDeclaration类.节点接口具有以下形式的accept方法:

I have a MethodDeclaration class that implements a Node interface. The node interface has an accept method of the form:

public Node accept(TypeVisitor v){ return v.visit(v));

此accept方法使TypeVisitor可以访问MethodDeclaration.

This accept method makes it possible for a TypeVisitor to visit a MethodDeclaration.

现在要访问方法声明,我要做一个简单的类型检查

Now to visit a method declaration, I do one simple type checking

public Node visit(MethodDeclaration n){

    // this visits the f10 Node, which is the return expression, 
    // and returns a specific Node object
    Node rtype =  n.f10.accept(this);

    // this also does a similar thing by visitng the f1 Node,
    // the method's return type, and returns a  specific Node Object
    Node acType = n.f1.accept(this); 

    // Now if I compare the two specific Node objects, it always fails.

    if(rtype == acType){    
        //enter here    
     }
}

为什么不输入if-body?我还尝试了 rtype.equals(acType),它返回false.

Why does it not enter the if-body? I also tried rtype.equals(acType) and it returns false.

我尝试了 rtype.toString.equals(acType.toString()),它也返回false.

I tried rtype.toString.equals(acType.toString()) which also returns false.

我尝试使用eclipse调试器进入代码,这是输出:

I tried stepping into the code using the eclipse debugger and here is the output:

rtype   IntegerType  (id=67)    
acType  IntegerType  (id=69)    

从调试器的输出中可以看出,rtype和acType都是IntegerType对象.

As can be seen from the debugger output both rtype and acType are IntegerType objects.

知道为什么比较失败吗?

Any idea why the comparision is failing?

如果我使用if(rtype instanceof IntegerType),则返回true和

If i use if(rtype instanceof IntegerType) this returns true and

如果我使用if(acType instanceof IntegerType),它也会返回true.

If i use if(acType instanceof IntegerType) this also returns true.

但是对象比较总是失败吗?

But the object comparision always fails?

我正在使用JavaCC(用于生成解析器),JTB(AST和Visitors创建者),eclipse和Java 1.7

I am using JavaCC(for parser generation), JTB(AST and Visitors creator), eclipse and java 1.7

推荐答案

在Java中, == 测试对象身份-实际上,两个对象是相同的对象.您想使用 .equals()来测试对象是否相等.请注意,默认实现(在 equals() Object 中只是 == ;如果要逻辑相等,则类必须重写等于(对象).

In Java, == tests for object identity -- that two objects are, in fact, the same object. You want to use .equals() to test for object equality. Be aware that the default implementation (in Object of equals() is simply ==; if you want logical equality, your classes must override equals(Object).

有关如何执行此操作的资源很多.本文是一个很好的起点,Oracle的

There are lots of resources available on how to do this. This article is a pretty good starting place, as is Oracle's Java tutorial.

这篇关于Java对象相等不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆