whats the difference between == and .equal ?

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What type of variable is error flag?

587


What is meant by attribute?

595


How do I convert a string to an int in java?

553


What is Java Package and which package is imported by default?

597


How do you pass by reference?

539






Can we overload the constructors?

559


Can you inherit a constructor java?

577


What is finally block?

566


Write java program to reverse string without using api?

554


What is scope & storage allocation of static, local and register variables? Explain with an example.

568


How many types of exception can occur in a java program?

538


What is constructor chaining in java?

587


I want to print “hello” even before main is executed. How will you acheive that?

660


What is purpose of applet programming?

640


Why is string builder not thread safe?

591