• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is the diff. b/w "==" and ".equals( )" Operators

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q, What is the basic difference b/w == and .equals()operators..
e.g:
public class Kamran{
public static void main(String[]args){
String aa=("Kamran");//it is in pool
String bb=new String ("Kamran");//it is in heep
System.out.println(aa==bb);
System.out.println(aa.equals(bb));
}
}
Output:
false
true
what does the "==" and ".equals()" operators do in this code ?
Note:Please try to explain with examples...
Thanx in advance
Kamran(Badarian)

[This message has been edited by Kamran Mughni Siddiqui (edited November 16, 2001).]
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kamran Mughni Siddiqui:
Q, What is the basic difference b/w == and .equals()operators..
e.g:
public class Kamran{
public static void main(String[]args){
String aa=("Kamran");//it is in pool
String bb=new String ("Kamran");//it is in heep
System.out.println(aa==bb);
System.out.println(aa.equals(bb));
}
}
Output:
false
true
what does the "==" and ".equals()" operators do in this code ?
Note:Please try to explain with examples...
Thanx in advance
Kamran(Badarian)

[This message has been edited by Kamran Mughni Siddiqui (edited November 16, 2001).]


== //Shallow comparison
equals() //Deep comparison
if aa = bb; //then both the reference refer to single litreal Kamran and == will be true
But "Kamran" and new String("Kamran") are different and therefore == will be false
HIH
Ragu
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
== is for comparing primitive values and object references.
int i=1;
int j=1;
int k=2;
i==j yields true, while i==k yields false.
Integer i1 = new Integer(1);
Integer i2 = new Integer(1);
Integer i3 = i2;
i1 == i2 yields false because i1 and i2 reference different Integer instances. i1.equals(i2) yields true because the integer value held by i1 and i2 are the same.
i3 == i2 yields true since i2 and i3 reference the same object and the operator == compares the references of variables.
equals() is only for comparing equality of objects. See example above.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kamran,
== is the coditional operator.It's basic function is to checks the two operands(one on it's left & one on it's right ) for equality.If they are equal then it prints true & vice versa.This opeartor is used in two cases.
1: With variables(Primitives)//As Valentin Crettaz mentioned
2: With reference variable(Non primitive)
Case:1
When used with simple variables i.e primitives it checks the values present in variables.
Example:

Out Put will be true
Since here it matches the values presnt in the variables a & b.

Case:2
When used with reference variables i.e non primitive, it checks there references.
Example:

equals()[/B].It is very simple one
It's function is to do a [b]deep matching
.Deep matching means to match the contents refer by refernce variable.

Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
[This message has been edited by Vikrama Sanjeeva (edited November 17, 2001).]
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic