Results 1 to 5 of 5
- 08-03-2012, 02:31 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 5
- Rep Power
- 0
can't figure out how jvm evaluates the code
hello,
i am new to java and now i can not figure out how generic types work in combination with polymorphic method dispath.
So this is the code:
everything worked fine but then i tried to work with lists of lists and that is when the equals method did brake down.Java Code:public interface IList<X> { public boolean equals(IList<X> other); } public class Nil<X> implements IList<X>{ public Nil(){} public boolean equals(IList<X> other){ if(other instanceof Nil<?>){ return true; }else{ return false; } } } public class Cons<X> implements IList<X>{ private X first; private IList<X> rest; public X getFirst() { return first; } public IList<X> getRest() { return rest; } public void setFirst(X first) { this.first = first; } public void setRest(IList<X> rest) { this.rest = rest; } public Cons(X first, IList<X> rest) { this.setFirst(first); this.setRest(rest); } public boolean equals(IList<X> other){ if(other instanceof Cons<?>){ Cons<X> o = (Cons<X>)other; return this.getFirst().equals(o.getFirst()) && this.getRest().equals(o.getRest()); }else{ return false; } } }
when i m debugging i think that what happens is that if the list contains only elements that are not of generic type themselfs the systems knowsJava Code:IList<IList<Integer>> l1 = new Cons<IList<Integer>>(new Cons<Integer>(1, new Nil<Integer>()), new Nil<IList<Integer>>()); IList<IList<Integer>> l2 = new Cons<IList<Integer>>(new Cons<Integer>(1, new Nil<Integer>()), new Nil<IList<Integer>>()); l1.equals(l2); // this here gives me false.
the type at runtime and can call the appropriate method. but in this case what i find is that insteat of calling the equals method in Cons<X>
again when comparing the first elements in the list the system seems to have forgotten about the type and just calls the equals method in
the Object class. now what i suspect is that during the process of converting the generic types back into the old fashion object system something bad happend.
so i do not need this for work or anything serious i was only curious about java and thought i could try it out.
so there is no hurry or anything but if someone would be willing to share his insights into the jvm workings it would be much appriciated.
ft
- 08-03-2012, 03:00 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: can't figure out how jvm evaluates the code
That's because your equals() method does not override the Object equals.
It should be declared:
Your equals method is just another method as far as the JVM is concerned.Java Code:public boolean equals(Object other) { }Please do not ask for code as refusal often offends.
- 08-03-2012, 04:05 PM #3
Re: can't figure out how jvm evaluates the code
Try debugging the code by adding a println to the method to see if it is being called and what value it returns.
If you don't understand my response, don't ignore it, ask a question.
- 08-03-2012, 07:41 PM #4
Member
- Join Date
- Aug 2012
- Posts
- 5
- Rep Power
- 0
Re: can't figure out how jvm evaluates the code
hello,
@Tolls i thought there wont be any reply at all, but you actually gave me exactly the right answer 30 min after i posted! incredible! thank you very much. so i changed the type IList<X> to Object and all the tests came out just fine. so then i spent the last 10 minutes thinking about what happend :D. but i think i got it now. so all the times before when i had different types in my list like Integer or Person or all that silly stuff i did put in those lists :D they actually had overwritten the class from Object with the right signature. (for self made class Person i used eclipse which actuall created also a hash method and an equals method that does not use instanceof and therfore is symmetric) so when at runtime all my generic type information for the argument of the equals function were gone and set to object whitch was not a problem since i had semantically working methods for it. BUT when i did put my own IList type as an element inside my list it did not override the equals method from Object so when the comparison was made and the system did not find an overwritten version of equals(Object o) he just passed it up to the real Object method which of course only performs an == check for addresses which did not make sense for comparing my lists. .... sorry if what i wrote just now was a little confusing but in any case at least you @Tolls knew what was the problem anyway :D......
so i guess i should have used that override annotation for the equal method like eclipse does........ that way i would have noticed that something was not right..... ANYWAY..... again Tolls thank you very much ...... super good answer...... you know in german toll means great.... so a guess that was just toll Tolls :D
ft
- 08-03-2012, 07:52 PM #5
Re: can't figure out how jvm evaluates the code
This is one of the reasons it's good to use the @Override annotation. If you think you're overriding a method but you accidentally mistyped its name or used the wrong argument type, the compiler will complain.
Get in the habit of using standard Java naming conventions!
Similar Threads
-
Breaking up code into methods. Can't figure out how to code
By Log1c in forum New To JavaReplies: 5Last Post: 04-14-2012, 12:18 PM -
Can't figure out
By beauti477 in forum New To JavaReplies: 3Last Post: 07-20-2011, 03:58 PM -
New to Java, cant figure out why im getting this error code.
By JBeese in forum New To JavaReplies: 5Last Post: 06-05-2011, 02:19 AM -
Can't seem to figure how to even get started on writing my code..
By theBurgh22 in forum New To JavaReplies: 7Last Post: 11-04-2010, 04:10 PM -
i have a small error but i can't figure out what is wrong [code provided]
By blueduiker in forum New To JavaReplies: 3Last Post: 01-11-2010, 06:48 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks