• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

== and equals()

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer i1=1000;
Integer i2=1000;
if(i1 != i2)System.out.println("different");
if(i1.equals(i2))System.out.println("equal");

Output::::
different
equal


Integer i3=10;
Integer i4=10;
if(i1 == i2)System.out.println("same");
if(i1.equals(i2))System.out.println("equal");

Output::::
same
equal



Could someone please explain the difference??? != and == really confusing me.!= tells they are different objects and == says they are the same..???
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from the Java Language Specification 5.1.7.

If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
 
Ranch Hand
Posts: 44
1
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 2 topics here, namely shallow vs. deep comparisons and autoboxing.

shallow vs. deep comparisons (== vs. equals):
!= and == represent shallow comparison. In order for == to return true, the 2 object references must reference the object found in the same memory location.
In order for != to return true, the 2 object references must reference objects found in different memory locations.

!object.equal() and !object.equal() represent deap comparison. These methods of comarison do not care about the the location of the objects involved in the comparison, but rather the contents there of.

Autoboxing:
To gain a better understanding of autoboxing read this: http://today.java.net/pub/a/today/2005/03/24/autoboxing.html. The supports what Keith Lynn was stating.


The following class might help you Run the following class
public class test {

public test() {
}

public static void main (String[] args)
{
// String comarisons included so you can see comparisons using String objects
String s1="hello";
String s2="hello";
String s3= new String("hello");
if(s1 != s2)System.out.println("s1 not referencing same string as s2");// returns false
if(s1 == s2)System.out.println("s1 referencing same string as s2");// returns true
if(s1.equals(s2))System.out.println("equal str content in s1 and s2");// returns true
if(s1 != s3)System.out.println("s1 not referencing same string as s3");// returns true
if(s1.equals(s3))System.out.println("equal str content in s1 and s3");// returns true

// mutable value comparison because value is greater than 127
Integer i1=1000;
Integer i2=1000;
if(i1 != i2)System.out.println("i1 and i2 reference different objects");// returns true because they are different objects
if(i1 == i2)System.out.println("i1 and i2 reference same object");// returns false
if(i1.equals(i2))System.out.println("i1 and i2 contents are the same");// returns true

// immutable value comparison because value is between -128 and 127
Integer i5 = 100;
Integer i6 = 100;
if(i5 != i6)System.out.println("i5 and i6 reference different objects");// returns false
if(i5 == i6)System.out.println("i5 and i6 reference same object");// returns true
if(i5.equals(i6))System.out.println("i5 and i6 contents are the same");// returns true


// immutable, but reference different objects
Integer i7 = 100;
Integer i8 = new Integer(100);
if (i7!=i8) System.out.println("i7 and i8 reference different objects");// returns true
if (i7==i8) System.out.println("i7 and i8 reference same object");// returns false
if (i7.equals(i8)) System.out.println("i7 and i8 contents are the same");// returns true

}
}
 
aditi mudholkar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnx Quintin...

but i dint understand
String s1="hello";
String s2="hello";
String s3= new String("hello");
if(s1 == s2)System.out.println("s1 referencing same string as s2");// returns true

why is the above code returning true?s1 and s2 are referencing different memory locations rite? and as you said == will return true only when they are referencing in the same memory locations.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
they are refering the same object (in the string constant pool).

with strings sometimes when you dont say 'new' a new string does not get created as it already exists in the string constant pool.
 
Quintin Stephenson
Ranch Hand
Posts: 44
1
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Louis, is correct they are refering the same object (in the string constant pool).

Taken from http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html

The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.

Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.



So when you say:
String s1="hello";
String s2="hello";

you are actually saying when you declare s2 use the string found in memory so s1==s2 will alway be true.

When you use:
String s3= new String("hello");

you are actually saying create a new object in memory. So s1 can never be == to s3, but its content as the same so equals() will do the job.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic