This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
String comparison and String interning in Java
What is the difference between .Equals and ==
Just a simple question about comparing strings. Why should i be using string.equals(string2)
and not string==string2 ? Thank you
equals tests if the strings' content is the same; == tests if both are the same object.
In case you have two different String objects that have the same value.
string==string2 is physical comparison and compares the references to objects. equals is logical comparison and the equality can be defined in equals() method which objects inherit this from Object (Parent of all types)
Related
This question already has answers here:
Comparing references in Java
(5 answers)
Closed 7 years ago.
I am looking for a Java equivalent of pointer-comparison in C/C++.
Suppose I am invoking a getSomething() method of an object of a class from a third-party. How do I know if the implementation of the getSomething() is just returning the same instance everytime or returning a different instance of the object?
I am not looking to check whether two objects are identical, but need to check if they are the same instance or not. The motivation is, if the 3rd party implementation is creating a new instance everytime, probably I can optimize my code by not invoking the method everytime. Assume getSomething() is invoked by me 1000s of times a second at run-time
From what I read from various articles, I shouldn't rely on hashCode(). In that case what is the way to do this?
Reference types are "pointing to" the same object, when they are equivalent according to the == (identity) operator.
The question says:
I am not looking to check whether two objects are identical, but need to check if they are the same instance or not (emphasis mine)
You actually mixed equality with identity (as I did initially in the answer). Being the same instance is identity. Being the same value is equality.
The == operator compares the values held by the two operands. If the operands are primitive, the actual values are checked for equality. If the operands are of object type, then == checks for equality of identity, functioning in this role just like Python's is keyword.
Note that there is bit of fudginess caused by autboxing and autounboxing between primitive and wrapper object types.
== double equal operator are use to check whether two reference variable hold the same object or not.
Eg.
String s1 = new String("hello");
String s2 = s1;
String s3 = new String("hello");
System.out.println(s1==s2); // it prints true
System.out.println(s1==s3); // it prints false
s1 and s2 pointing to the same object.
s3 holds newly created object and hence s1 == s3 prints false
I think you're not looking for the ==operator. To compare two instances of an object, for example, I use the equals() Object method. Here are two useful links about comparisons:
java ==, equals(), compare(), compareTo()
Javadocs equals() method
You can use == operator to compare two objects if they are having same reference or not.
This question already has answers here:
Why do I need to override the equals and hashCode methods in Java?
(31 answers)
Closed 9 years ago.
Why we should always override hashCode() method whenever we override equals() method? As per my understanding they both seem to fulfill different purpose. hashCode() method is used in hashtables to determine the equality of keys. However equals() method is used to determine the equality of two objects.
If two objects are equal according to the equals method, their hashcodes must be equal as well. Otherwise, lookup in the hash table would fail.
My question to java folks is, when I am comparing two strings
imageName=new String[20];
....
imageName[1]="img1";
imageName[2]="img1";
if(imageName[1]==imageName[2])
{
//// codes
}
it works perfectly, but when I am making the string through number concatenation it's not working
imageName=new String[20];
int j=1,k=1;
imageName[1]="img"+j;
imageName[2]="img"+k;
if(imageName[1].toString()==imageName[2].toString())
{
//// codes
}
it's not working though the values of j and k are the same
Thanks in advance for your solution
You should use String.equals when comparing two Strings:
if (imageName[1].equals(imageName[2])
You shouldn't compare strings with ==, but instead use the .equals method: imageName[1].equals(imageName[2]).
== compares the pointers for equality, so it'll be true if both variables represent the exact same instance in memory. In the first case, it's the case because Java pools String literals for performance. But in your second case, you're getting two distinct heap-allocated objects, which, despite their content is identical, are two distinct objects nonetheless.
You are comparing whether the two String are exactly the same object.
What you intended was to compare their contents. I suggest you use .equals instead.
Never, ever, use "==" to compare Strings in Java. Use the equals() method. The == operator checks to see if two String variables are referring to the same location in memory, while the equals() method checks whether two separate String objects contain the same characters. It's this second definition that makes sense here: your String concatenation is creating separate String objects, but we still want to consider them as "equal".
The correct way to compare strings is using equals() method
So, Please change your code as below,
if (imageName[1].equals(imageName[2])
And please consider to do a research in SO before posting, as the questions like this have been answered many times before.
== is a reference comparison. That is, you're determining if the two objects are, in fact, the same object. If you use equals() then that method comparses the contents of the string i.e. do those objects have the same contents (you'll appreciate there is a subtle difference here)
Your first scenario works since the compiler is clever enough to realise that you have the same string twice. i.e. it looks at:
imageName[1]="img1";
imageName[2]="img1";
and determines that your array elements can point to the same object. In your second scenario, that's no longer true.
imageName[1]="img"+j;
imageName[2]="img"+k;
The compiler can't reliably determine that these could be the same string object (quite correctly, too).
So (generally speaking) you should use equals() to compare Strings. You can use the reference equality (it's faster since it comparse the references rather than the string contents), but you have to be absolutely sure about what you're doing (perhaps you're using String.intern() - but there are disadvantages there)
To compare two Strings you better use equals() method
if(imageName[1].equals(imageName[2]))
{
//// codes
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference Between Equals and ==
in which cases equals() works exactly like == operator?
It seems that they both act similar for primitive data types. Are there any other cases in which both of them act equal?
== compares the bits of reference for Object type so if you have reference to same Object it would be the case
For example
Integer for value -128 and 127 (inclusive) it caches (while autoboxing) the instance so it would be the case here for the mentioned range of value of Integer
For primitive data types, there is no equals() (because they are not objects, and have no methods).
The default implementation (in class Object) for equals() just does object identity check (i.e. the same as ==). So if a class does not override it, it will have the same result as ==.
The operator == will always compare references for objects, and the actual value for primitive types.
Note that an array of primitives like int[] is still an object!
String test1 ="test";
String test2 = test1;
System.out.println(test1 == test2);
System.out.println(test1.equals(test2));
Both will print -
true
true
In addition to primitives (which are a special case) == and equals() behave similarly for every case in which reference equality is the same as actual equality:
Interned Strings
Certain Integer references (normally between -128 and +127, but this is configurable, and it depends on how the instance was constructed)
Singletons
Instances of Object (and any other class that doesn't override equals())
Obviously, when in doubt, use equals()
The equals() method evaluates on hashCode comparisons. While == compares objects by reference.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Equals method for objects
i have below code.
public class SomeClass{
OtherClass clas = new OtherClass();
OtherClass some;
some=this;
if(some != this) {
s.y.s.o("true");
}
my question is which one is correct?
some != this or some.equals(this) ?
To compare object references, you use == and !=, while equals is used to compare the values.
What do you want to compare?
== compares to see if the two references are to the EXACT SAME OBJECT.
equals compares to see if the two references are to objects which have matching properties, based on the class-specific criteria in the class's equals implementation.
You use operator!= or operator== when you want to check for identity of two objects [if they are actually the same object]
You use equals() when you want to check for equality. [if two object are equal, as the equals() method defined them].
It is hard to know what exactly you are trying to achieve, but usually when comparing two reference objects, we want to use equals().
In Java == compares references (i.e. addresses) while equals compares object equality (and you can override it to compare based the member variables you see fit)
In your post you compare to see if some is the same object as this. Is this what you want to do? If yes then the some!=this is correct (and is false since some refers to this)
== compares the references. That is, are these two objects located at the same place.
.Equals compares the things pointed to by those references. That is, are these two objects equivalent.