Solved

== and .equals clone()

Posted on 2002-05-09
2
189 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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
count7 challenge 12 66
Books that can get me started on JAVA 2 52
for i loop in grovy 1 32
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:

763 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

7 Experts available now in Live!

Get 1:1 Help Now