Solved

== and .equals clone()

Posted on 2002-05-09
2
207 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 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 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