?
Solved

== and .equals clone()

Posted on 2002-05-09
2
Medium Priority
?
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 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