Results 1 to 6 of 6
  1. #1
    gamilah is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Question a little problem with (equals)

    hello all
    I have a very little problem ,the outputs after running test class are both false. it supposed to be false for Animal and true for Vegetable i think there is something wrong with equals lines**.
    i'll be thankfull if you figure out the mistake



    public class Thing{}

    public class Animal extends Thing{

    public int weight;

    }

    public class Vegetable extends Thing{

    public int Length;

    }

    public class ToGo{
    public void go(){


    Thing x=new Thing();
    Thing y = oo(x);
    **System.out.println("x is Animal : "+ x.equals(y));

    **System.out.println("x is Vegetble : "+ x.equals(y));
    }

    public Thing oo (Thing pValue){
    pValue= new Vegetable();
    return pValue;
    }
    }


    public class test{
    public static void main(String[]args){
    ToGo f = new ToGo();
    f.go();


    }


    }

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    cleaned up your code a little so its easier to read.
    Java Code:
    public class Thing{
    }
    
    public class Animal extends Thing{
    	public int weight;
    }
    public class Vegetable extends Thing{
    	public int Length;
    }
    
    public class ToGo{
    	public void go(){
    		Thing x=new Thing();
    		Thing y = oo(x);
    		System.out.println("x is Animal : "+ x.equals(y));
    
    		System.out.println("x is Vegetble : "+ x.equals(y));
    	}
    
    	public Thing oo (Thing pValue){
    		pValue= new Vegetable();
    		return pValue;
    	}
    }
    
    
    public class test{
    	public static void main(String[]args){
    		ToGo f = new ToGo();
    		f.go();
    	}
    }

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    I think one issue might be you have no constructor within Thing but since you said it outputs false, false that's probably not it.

    I think I remember something about the equals method comparing the object ID so the only way they would equal was if you were comparing an object against itself. I'll look at this some more when I get home from work and get back to you

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    If x.equals(y) is false the first time you try it, why would you expect it to be true the second time?

  5. #5
    gamilah is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    If x.equals(y) is false the first time you try it, why would you expect it to be true the second time?
    oooh you 're right .i wasn't thinking

  6. #6
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    You should look at the rules for how equals() is defined.

    Among the rules are that a.equals(b) requres that b.equals(a)

Similar Threads

  1. number equals astrisks ????
    By nanna in forum New To Java
    Replies: 6
    Last Post: 10-06-2008, 09:47 AM
  2. Creating a new equals() method help
    By Dave0703 in forum New To Java
    Replies: 2
    Last Post: 09-21-2008, 05:32 PM
  3. Object class's equals() method behavior????
    By skyineyes in forum New To Java
    Replies: 4
    Last Post: 07-19-2008, 11:58 PM
  4. Why Equals method should be over ridden in Hashcode?
    By skyineyes in forum New To Java
    Replies: 1
    Last Post: 05-26-2008, 04:13 PM
  5. Replies: 0
    Last Post: 01-25-2008, 11:13 PM

Posting Permissions

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