![java package tutorial](/java/lang/images/java.jpg)
- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java Object clone() Method
Description
The Java Object clone() method creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression −
x.clone() != x
will be true, and that the expression −
x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that −
x.clone().equals(x)
will be true, this is not an absolute requirement.
Declaration
Following is the declaration for java.lang.Object.clone() method
protected Object clone()
Parameters
NA
Return Value
This method returns a clone of this instance.
Exception
CloneNotSupportedException − if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.
Getting Clone of a GregorianCalendar Object Example
The following example shows the usage of java.lang.Object.clone() method. In this program, we've created an instance of GregorianCalendar and then using clone() method, we've created another object using earlier object. Time from both the object is printed.
package com.tutorialspoint; import java.util.GregorianCalendar; public class ObjectDemo { public static void main(String[] args) { // create a gregorian calendar, which is an object GregorianCalendar cal = new GregorianCalendar(); // clone object cal into object y GregorianCalendar y = (GregorianCalendar) cal.clone(); // print both cal and y System.out.println(cal.getTime()); System.out.println(y.getTime()); } }
Output
Let us compile and run the above program, this will produce the following result −
Fri May 31 13:50:19 IST 2024 Fri May 31 13:50:19 IST 2024
Getting Clone of a ArrayList Object Example
The following example shows the usage of java.lang.Object.clone() method. In this program, we've created an instance of ArrayList and then using clone() method, we've created another object using earlier object. Both the objects are printed.
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class ObjectDemo { public static void main(String[] args) { // create a ArrayList, which is an object ArrayList<String> list = new ArrayList<>(); // add value to the list list.add("Tutorialspoint"); // clone object list into object y List<String> y = (List) list.clone(); // print both list and y System.out.println(list); System.out.println(y); } }
Output
Let us compile and run the above program, this will produce the following result −
[Tutorialspoint] [Tutorialspoint]
Getting Clone of a HashMap Object Example
The following example shows the usage of java.lang.Object.clone() method. In this program, we've created an instance of HashMap and then using clone() method, we've created another object using earlier object. Both the objects are printed.
package com.tutorialspoint; import java.util.HashMap; public class ObjectDemo { public static void main(String[] args) { // create a HashMap, which is an object HashMap<String, String> map = new HashMap<>(); // add a value to the map map.put("t", "tutorialspoint"); // clone object map into object y HashMap<String, String> y = (HashMap)map.clone(); // print both map and y System.out.println(map); System.out.println(y); } }
Output
Let us compile and run the above program, this will produce the following result −
{t=tutorialspoint} {t=tutorialspoint}