whats the difference between == and .equal ?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are autoboxing and unboxing? When does it occur?

532


What is the difference between inner class and nested class?

548


Why put method is used?

519


What is the function of http?

527


Which methods cannot be overridden in java?

532






Write a program to search a number in the given list of numbers.

625


What are advantages and disadvantages of OOPs?

623


What is mean by encoding?

625


What is the difference between dom and sax parser in java?

526


What is difference between arraylist and list in java?

576


What are the loops in java?

518


Why is static used?

545


What do you mean by static variable?

567


Which class should you use to obtain design information about an object in java programming?

641


Is Constructor possible in abstract class in java ?

582