Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By kjkrum

Thread: can't figure out how jvm evaluates the code

  1. #1
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default 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:

    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;
    		}				
    	}
    }
    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:
    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.
    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 knows
    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

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,800
    Rep Power
    19

    Default 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:
    Java Code:
    public boolean equals(Object other) {
    }
    Your equals method is just another method as far as the JVM is concerned.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

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

  4. #4
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default 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

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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.
    Norm likes this.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 5
    Last Post: 04-14-2012, 12:18 PM
  2. Can't figure out
    By beauti477 in forum New To Java
    Replies: 3
    Last Post: 07-20-2011, 03:58 PM
  3. Replies: 5
    Last Post: 06-05-2011, 02:19 AM
  4. Replies: 7
    Last Post: 11-04-2010, 04:10 PM
  5. Replies: 3
    Last Post: 01-11-2010, 06:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •