0

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
alfu
  • 67
  • 7
  • 1
    It's not clear why you'd expect the nodes to *be* equal. We don't know enough about the `accept` method to know what the two nodes represent. – Jon Skeet Apr 14 '13 at 20:15
  • Additionally, please pay more attention to formatting your question in future. Please read http://tinyurl.com/so-list – Jon Skeet Apr 14 '13 at 20:16
  • 2
    How many duplicates does "my objects aren't equal when i use `==`" have...? – cHao Apr 14 '13 at 20:16
  • possible duplicate of [Equal strings aren't equal (==) in Java?](http://stackoverflow.com/questions/6178585/equal-strings-arent-equal-in-java) – cHao Apr 14 '13 at 20:18
  • Where is the source (or at least javadocs) for `IntegerType`? What does `rtype.getClass()` and `acType.getClass()` return? – Esko Luontola Apr 14 '13 at 20:29
  • @Esko Luontola rtype returns ast.syntaxtree.IntegerType and acType returns ast.syntaxtree.IntegerType. As can be seen, the are both from the same class – alfu Apr 14 '13 at 20:37
  • @Jon Skeet: Yes sorry about that. I was just making it short to save typing:( – alfu Apr 14 '13 at 20:39
  • Also a duplicate of http://stackoverflow.com/q/14261530 , http://stackoverflow.com/q/456575 , http://stackoverflow.com/q/9977236 ,... and no doubt countless others – cHao Apr 14 '13 at 20:51
  • @cHao: This is not a duplicate. I saw that thread which wasn't what I was looking for. I used both == and equals() to compare and both fails. From the thread you pointed out, the OP uses only ==. I needed to post this having tried both options unsuccessfully. That's the only way I can get Ted Hopp's answer. – alfu Apr 14 '13 at 20:51
  • 1
    @user1897459: Did you ever actually *implement* an `equals` method in your class? Java doesn't magically make one, you know -- and the default one (in `Object`) does pretty much exactly the same thing as `==`, except it breaks when the left side is null. – cHao Apr 14 '13 at 20:54
  • 1
    See that last post i linked -- that covers your question pretty near exactly. – cHao Apr 14 '13 at 20:56

2 Answers2

3

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 1
    He says he tried `equals`, too. This is not one of *those* questions :) – Marko Topolnik Apr 14 '13 at 20:30
  • tahnks, it works. I check the implementation for the override method from the answer by jjnguy: http://stackoverflow.com/questions/185937/overriding-the-java-equals-method-quirk – alfu Apr 14 '13 at 20:34
  • @MarkoTopolnik - It's not just a question of using `equals()`; as I point out in my answer, it's a question of implementing one's own `equals` logic. So this is, in fact, one of "_those_" question--OP even thinks that this solved his problem. :) – Ted Hopp Apr 14 '13 at 21:09
  • Not your downvoter, BTW (never have been, for that matter). I was only referring to your first sentence. – Marko Topolnik Apr 14 '13 at 21:12
1

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.

Esko Luontola
  • 73,184
  • 17
  • 117
  • 128
  • Thank you. rtype.getClass() and acType.getClass() are returning the same type of objects. As pointed out by "Ted Hopp", they are calling the equals() method of the Object class which is also simply ==. But if I override the equals() method in my IntegerType class, the comparision works. – alfu Apr 14 '13 at 20:46