alfu
alfu

Reputation: 67

java object equality not working

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;}

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

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.

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));

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    
     }
}

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

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

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

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

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

Any idea why the comparision is failing?

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

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

But the object comparision always fails?

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

Upvotes: 0

Views: 851

Answers (2)

Esko Luontola
Esko Luontola

Reputation: 73655

I see two potential issues:

(1) rtype == acType is rarely ever what you want. Use equals. But since you told you already used it and it didn't help, here's the second issue:

(2) Either the definition of equals is not what you think it is, or the values are not what you think they are. First of all, print rtype.getClass() and acType.getClass() to find out the exact types of the objects. Then get the source code of those classes (assuming the libraries you use are open source) and see how their equals methods are defined. Then proceed into checking the values of the fields being compared by the equals method.

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234857

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).

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.

Upvotes: 3

Related Questions