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 7 of 7

Thread: Operation ==

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Operation ==

    Hi,

    I wrote the following code:
    Ingeter i1 = 10;
    Ingeter i2 = 10;
    if (i1 == i2)
        Stystem.out.println(" == Integer");
    Double d1 = 10.0;
    Double d2 = 10.0;
    if (d1 == d2)
        Stystem.out.println(" == Double");
    The output is:
    == Integer

    Question:
    I'm trying to understand why in one case (Integer, Boolean, String etc) the operation == is true and in oher case (Double,Float etc) the operation is false?

    Thanks.
    Last edited by helloworld922; January 4th, 2010 at 03:49 PM. Reason: Please use [code] tags!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Operation ==

    Using the == operator on primitives is fine, but on objects is generally a bad idea if you wish to check content equality....this technique checks that the objects refer to the SAME object (aka the same reference or memory). If this is not what one wishes to do, it is always best to use the equals() function, which checks values as opposed to references. The output is dependent upon the intricacies of compiler and JVM, in the case of the Integer object, the JVM uses the same address in memory to store the 10, and therefore i1 and i2 refer to that same address. For doubles that may not be the case.
    As an example, see the following outputs
    == Integer 
    == Double (d1 == d3)
    == Double (d1 == d2 via equals)

    		Integer i1 = 10;
    		Integer i2 = 10;
    		if (i1 == i2)
    		System.out.println(" == Integer");
    		Double d1 = 10.0;
    		Double d2 = 10.0;
                    Double d3 = d1;
    		if (d1 == d2)
    		System.out.println(" == Double (d1 == d2)");
    		if (d1 == d3)
    		System.out.println(" == Double (d1 == d3)");
    		if (d1.equals( d2 ))
    		System.out.println(" == Double (d1 == d2  via equals)");

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Operation ==

    Thanks for the answer.

    Is there any way to know which object returns true when I'm using the operation == (without test it)?
    (this question was part of my job interview and I could not test it)

    Thanks.

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

    Default Re: Operation ==

    Low-down, object variables hold an address that points to the location of the object. Using the == operator on any such variables will try to compare the addresses of the two objects rather than comparing the objects themselves. Obviously, if both objects point to the same object == will return true. However, if they point to different objects there is no way to tell if the objects are equal, only that they are not the same object. Instead, there is an equals method that actually compares the contents of objects according to the way the method is defined.

    The reason primitive values can be compared using the == operator is they don't contain an address to an object, but rather contain the value directly.

    Integer a = new Integer(5);
    Integer b = new Integer(5);
    if(a == b)
    {
         System.out.println("This code won't get executed because they point to different objects");
    }
    if (a.equals(b))
    {
         System.out.println("This code will get executed because their contents are the same");

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Operation ==

    helloworlds response is much more eloquent than mine

    Quote Originally Posted by meytalg View Post
    Thanks for the answer.

    Is there any way to know which object returns true when I'm using the operation == (without test it)?
    (this question was part of my job interview and I could not test it)

    Thanks.
    Basically, if you ever assign an object to another using '=' instead of new, the == will evaluate to true for both assignees. In this particular case with the Integer object, one may think i1 and i2 are NOT equal based upon this assumption:
    s
    Integer i1 = 10;
    Integer i2 = 10;
    if (i1 == i2)//will return true

    but you need to dig deep to know why. In the manner in which you assigned the values of i1 and i2 (i1 = 10), you let the compiler do it for you (as opposed to using new Integer(10) explicitely). To do the asignment, the compiler uses the static function Integer.valueOf(10) to get the Integer of value 10 for you. However, the Integer object has a cache of static Integer objects for the values -128 to 127 and returns one of these if it be within this range. Therefore in this case of 10, it retrieves the cached object of value 10. After all is said and done, each Integer you end up with refers to the same object/address/memory space: they are equal both in value and in memory space: == will return true.
    Try the same and instead of 10, use 128, you will see it will not evaluate to true.
    Integer i1 = 10;//assigns i1 to a cached static Integer
    Integer i2 = 10;//assigns i1 to a cached static Integer (the same as above)
    if (i1 == i2)//will return true
     
    Integer i1 = 128;//no cached Integer, returns a new Object
    Integer i2 = 128;//no cached Integer, returns a new Object
    if (i1 == i2)//will return false
    Last edited by copeg; January 4th, 2010 at 04:31 PM.

  6. The Following User Says Thank You to copeg For This Useful Post:

    helloworld922 (January 4th, 2010)

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

    Default Re: Operation ==

    Certain Strings also get cached. I believe it only caches Strings that are guarenteed constant and static/local.

    public static void main(String[] args)
    {
    	final String c = " world";
    	String a = "Hello " + c;
    	String b = "Hello world";
    	System.out.println(a == b); // will print out true
     
    	String d = " world";
    	String a = "Hello " + d;
    	System.out.println(a == b); // will print out false
    }

  8. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Operation ==

    Quote Originally Posted by helloworld922 View Post
    Certain Strings also get cached. I believe it only caches Strings that are guarenteed constant and static/local.]
    The compiler does a pretty darn good job of re-using Strings from memory, say for example within functions and objects (not to the extent that I'd trust == though). You can also 'force caching' using the intern() function of String, although I've never found the need to do this.
    Last edited by copeg; January 4th, 2010 at 07:48 PM.