Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Is .equals() redefined for primitive types?

  1. #1
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Is .equals() redefined for primitive types?

    Hello forums,

    The object is or cointains a primitive type either String, int, short, and boolean.

    I'm implementing this: object.equals(primitive type) hoping that is should be able to determing that object "Hello" is equal to String "Hello", or object "32" short is equal to Short "32", same for boolean, and int.

    Most of the type the equals compares identity and not state. So I am wondering if the equals() is overrided by the primitive types.

    Cheers.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Is .equals() redefined for primitive types?

    Java primitives are not objects and have no member methods associated with them. However, they can be auto-boxed into wrapper classes such as Integer, Short, etc. These are full objects and have equals() overridden to compare state. You'll have to check the definition if a Short with value 32 is equal() to an Integer with value 32, I don't remember off the top of my head if this is true or not.

    Strings are not primitives in Java, but full objects and have .equals() overridden correctly to compare state.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Andrew R (September 11th, 2013)

Similar Threads

  1. Why Java Data types called primitive?
    By pedong in forum Java Theory & Questions
    Replies: 11
    Last Post: October 3rd, 2013, 10:20 PM
  2. Tutorial: Primitive Data Types
    By newbie in forum Java Programming Tutorials
    Replies: 3
    Last Post: July 5th, 2012, 11:56 PM
  3. Primitive types
    By sudesh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 30th, 2012, 01:44 AM
  4. [SOLVED] Converting primitive data types causing NumberFormatException
    By Melawe in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 10th, 2011, 12:30 AM
  5. Double & Float primitive types
    By Zeek in forum Java Theory & Questions
    Replies: 10
    Last Post: September 12th, 2011, 07:31 AM