Solved

== and .equals clone()

Posted on 2002-05-09
2
190 Views
Last Modified: 2010-03-31
Can someone tell me precisely the difference between == and .equals(). Also, tell me what is clone() do.

Thanx
0
Comment
Question by:asukai
2 Comments
 
LVL 5

Expert Comment

by:jamoville
ID: 6999029
For strings I believe == checks if the object is the same and .equals checks for the same value.

For instanse
String one = "value";
String two = "value";

if (one == "value")    // false

if (one.equals("value") // true
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 50 total points
ID: 6999162
That is correct. When used with references, the '==' operator will return true only when the two object references are the same. That is to say, both references are to the same object on the heap. In contrast, the equals method permits individual classes to define what constitutes object equality at the value level.

The example cited in the previous post was Strings. Two string objects are equal per the equals operator (==) when they refer to the same actual String object in memory. Two separate String objects may be equal in value/contents and that can be determined by using the equals method.

It should be noted that the java.lang.Object default implementation of the equals method returns true for object references x and y only if x and y refer to the same object. So if a class or one of its superclasses does not override the equals method, the default implementation provided by Object will be used, thus making the equals operator and equals method functionally equivalent.

As for clone, this method, when implemented by a class, permits a copy of an object to be created and returned without calling any constructors. This typically requires creating a new instance of the class and copying of internal data structures. Depending on the network of contained objects, the clone can be either shallow or deep. A shallow clone results in two objects that refer to the same dependent objects. As for example if you were to clone a Vector, both Vectors would refer to the same collection of objects that existed at the time of the cloning. A deep copy would clone/copy the underlying objects as well resulting in separate and distinct object networks. For design guidance and implementation examples of clone, see:
    http://java.sun.com/docs/books/tutorial/java/javaOO/objectclass.html
    http://www.artima.com/designtechniques/canonicalP.html

Best regards,
Jim Cakalic
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java  and programming certification ? 4 86
Eclipse IDE - Cannot copy/paste from console output 8 126
Java array passed to SQL where clause 23 62
factorial example 4 34
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now