whats the difference between == and .equal ?
Answers were Sorted based on User's Feedback
Answer / manoj kumar sahu(secon pvt.ltd
In java if u use the .equal method it will compare the two
value if the values are match with each other the result
will give true otherwise false.
But if u use == it will compare the reference(address
of)two values.
s1 = new String("abc");
s2 = new String("abc");
Now, if you use the "equals()" method to check for their
equivalence as
if(s1.equals(s2))
System.out.println("s1.equals(s2) is TRUE");
else
System.out.println("s1.equals(s2) is FALSE");
it will give the output TRUE
let's try using '=='
if(s1==s2)
System.out.printlln("s1==s2 is TRUE");
else
System.out.println("s1==s2 is FALSE");
Now you will get the FALSE as output because both s1 and s2
are pointing to two different objects even though both of
them share the same string content. It is because of 'new
String()' everytime a new object is created.
if u try with out using new keyword the output will TRUE.
| Is This Answer Correct ? | 20 Yes | 1 No |
Answer / nagaraju
== it is relational operator to copares the values
.equals() is a method to compares the two strings whether
they are equal or not.
if(a==b)
System.out.println("both are equal");
object.equals(string1,string2)
System.out.println("strings are same");
| Is This Answer Correct ? | 24 Yes | 6 No |
Answer / tarun
equals() method compare the character inside the string object
== operator compare two object reference to see whether they
refer same instance
| Is This Answer Correct ? | 12 Yes | 1 No |
Answer / guest
== compares the objects but .equals compare the value of the
objects.
| Is This Answer Correct ? | 18 Yes | 9 No |
Answer / qim2010
The == returns true, if the variable reference points to the
same object in memory. This is a “shallow comparison”.
The equals() - returns the results of running the equals()
method of a user supplied class, which compares the
attribute values. The equals() method provides “deep
comparison” by checking if two objects are logically equal
as opposed to the shallow comparison provided by the
operator ==. If equals() method does not exist in a user
supplied class then the inherited Object class's equals()
method is run which evaluates if the references point to the
same object in memory. The object.equals() works
just like the "==" operator (i.e shallow comparison).
Overriding the Object class may seem simple but there are
many ways to get it wrong, and consequence can be
unpredictable behavior.
| Is This Answer Correct ? | 6 Yes | 0 No |
Answer / haneef
see, every object created by the new operator, it has it own
hash code.
so when you compare with equals(), checks only content. But
with ==, it also checks the hashcode.
try this example
package app;
public class Test {
public static void main(String[] args) {
String str2 = new String("Haneef");
String str3 = new String("Haneef");
if (str2.equals(str3))
System.out.println("ok");
else
System.out.println("Not OK");
if(str2==str3)
System.out.println("ok");
else
System.out.println("Not ok");
}
}
u get
OK
NOT OK
| Is This Answer Correct ? | 7 Yes | 2 No |
Answer / deepak sharma
== compares the object refrences to see if they refer to the
same object in memory.
where as
.equals compares the content of object to see if the object
content is same. Most of the times it is used to compare the
String Object. It can be used to compare other Objects too.
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / james rajesh
One database tale contain id and name...mostly id's are in int and name's are in String...if u want to compare with id and one number means u use ==
like, if(id==1)
if u want to compare name means u use .equals
like, if(name.equals("Bangalore"))
So point of view,
== used to compare integer
.equals used to compare String
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / surendra babu
== refers comparision of values and .eqals refers as
address of the variables.
| Is This Answer Correct ? | 1 Yes | 8 No |
Answer / jai
== this is equal to it mainly compares only the values of
the object if both are equal it returns true or else false
eg. a=10,b=20
if (a==b)
it will return false
for the same case it will return false for .equals since
.equals will check only that the objects refr to the same
reference (address) of the instance
clear
| Is This Answer Correct ? | 1 Yes | 10 No |
What is sleep method?
Why is inheritance used in java?
Does it matter in what order catch statements for filenotfoundexception and ioexception are written?
Difference between canvas class & graphics class?
Is there any way to find whether software installed in the system is registered by just providing the .exe file? I have tried the following code but its just displaying the directory structure in the registry. Here the code : package com.msi.intaller; import java.util.Iterator; import ca.beq.util.win32.registry.RegistryKey; import ca.beq.util.win32.registry.RootKey; public class RegistryFinder { public static void main(String... args) throws Exception { RegistryKey.initialize(RegistryFinder.class.getResource("jRe gistryKey.dll").getFile()); RegistryKey key = new RegistryKey(RootKey.HKLM, "Software\\ODBC"); for (Iterator<RegistryKey> subkeys = key.subkeys(); subkeys.hasNext();) { RegistryKey subkey = subkeys.next(); System.out.println(subkey.getName()); // You need to check here if there's anything which matches "Mozilla FireFox". } } }
what do you understand by the term string with respect to java?
How to sort an array from smallest to largest java?
What is difference between pointer and reference?
What is java util function?
Is ResultSet class?
5 Answers Bally Technologies, TCS,
Define how does a try statement determine which catch clause should be used to handle an exception?
what is mena by object block any what is the use of that