• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

what is the difference between == and equals

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use == with a primitive (int, double, char, ...), you are checking whether the values are identical.
When you use == with an object, you are checking whether the 2 objects are stored at the same address, in other words whether the two references are both pointing to the same object.

.equals() changes. Unless it is overridden by the object class, it is the same as ==. Some classes (like String) have overridden this method so that the class to create objects, they can check whether the contents are equivalent.it will check the <u>contents</u> of the object at the two addresses instead of just the addresses themselves. If you write a class, you may want to override the equals() method in your class so that when people use

Marilyn

This was told by merlin ...

I have one doubt
[code]

public class EQUALSeqeq{

public static void main(String args[])
{

String s1=new String("5");
Long l1=new Long("5");
System.out.println(s1.equals(l1));//displays false
}
}
Here the content is 5 and both String class and Long class overrides equals method so when i use equlas() method they need to check the content

the content here is "5" so it needs to display true
but it is displaying false

can any one explain me about this?
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create the Long with that constructor, you are creating a object which at its core contains a long.

Even though both Long and String override equals, if you send an object to their equals method which is not the same type as the caller, then the equals method will return false.

That is to say that a String object can only be equal to another String object, and a Long object can only be equal to another Long object.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The values in those classes are different. One is a String the other is a wrapper for long, which is an integral type. How can 2 different types ever be equal?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The equlas( ) method is overridden in String class. But it compares only two String objects.If you are comparing a String object with any other object type, evne though they have same values, false will be returned.
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

If you see, the String.equals() source, code

public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}

-----

If the object you are passing is not of type String, then it wil return false.

Just have a look at the source, it will be very clear.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!