whats the difference between == and .equal ?

Answers were Sorted based on User's Feedback



whats the difference between == and .equal ?..

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

whats the difference between == and .equal ?..

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

whats the difference between == and .equal ?..

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

whats the difference between == and .equal ?..

Answer / guest

== compares the objects but .equals compare the value of the
objects.

Is This Answer Correct ?    18 Yes 9 No

whats the difference between == and .equal ?..

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

whats the difference between == and .equal ?..

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

whats the difference between == and .equal ?..

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

whats the difference between == and .equal ?..

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

whats the difference between == and .equal ?..

Answer / surendra babu

== refers comparision of values and .eqals refers as
address of the variables.

Is This Answer Correct ?    1 Yes 8 No

whats the difference between == and .equal ?..

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

Post New Answer

More Core Java Interview Questions

What is java class writing rules?

1 Answers   Oracle,


Which is bigger double or float?

0 Answers  


What are use cases?

0 Answers  


Is java util regex pattern thread safe?

0 Answers  


What are the new features in java 8?

0 Answers  






Why is string class considered immutable?

0 Answers  


What is stack example?

0 Answers  


what is main difference b/w abstract class and interface

4 Answers  


What is an eror in java?

0 Answers  


What is "Java Native Interface" and how to use it?

1 Answers   IBM,


Is empty .java file name a valid source file name?

0 Answers  


Can we use different return types for methods when overridden?

0 Answers  


Categories