Results 1 to 5 of 5

Thread: Equals

  1. #1
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Equals

    hi all,

    i was going through the concept of .equals operator which according to me checks the objects content equivality. But when i tried to run my sample code mentioned below


    class Const

    {


    public Const(String n)

    {


    System.out.println(n);

    }




    public static void main(String[] args)

    {


    Const cst = new Const("sam");

    Const ts = new Const("sam");

    //ts= cst;


    if(cst.equals(ts))


    {


    System.out.println("equal");

    }


    else

    System.out.println("not equal");

    }


    }

    I get the out put as "not equal" which i guess shouldn't be. Can any one help me out here?


    Thank you

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Equals

    You should read some more about the equals( ... ) method and its compadre hashCode(); for one thing: you didn't implement them (according to the code you've shown us).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    java4amanda is offline Member
    Join Date
    Jan 2012
    Posts
    55
    Rep Power
    0

    Default Re: Equals

    ohh ok thanks

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Equals

    50 posts and you still haven't learned how to post code? Or are you just lazy?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: Equals

    Because you created a new class, you can't use equals() in this situation, you need override equals() to get desired result.


    Ksharp

Similar Threads

  1. using .equals
    By droidus in forum New To Java
    Replies: 11
    Last Post: 01-21-2012, 03:33 AM
  2. c always equals a*b
    By imorio in forum New To Java
    Replies: 3
    Last Post: 11-12-2010, 02:32 PM
  3. == and equals()
    By arefeh in forum New To Java
    Replies: 13
    Last Post: 01-05-2010, 04:56 PM
  4. == is same as .equals()??
    By DrMath in forum New To Java
    Replies: 1
    Last Post: 09-30-2009, 04:57 AM
  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
  •