what is the use of clone() in real time scenario?
Answers were Sorted based on User's Feedback
Answer / aravindareddy
Clone() method is used to create and return copy of the
object,The Cloneable interface defines a method called Clone
(), which can be used to clone an object in your java
program.The reason for making a copy of an object is if
you’re going to modify that object and you don’t want to
modify the caller’s object. If you decide that you want to
make a local copy, you simply use the clone() method to
perform the operation.
Is This Answer Correct ? | 28 Yes | 7 No |
Answer / aravinda reddy
Further to above a small update, clone will peform shallow
copy.Please execute below example and check the output.
public class CloneExamples {
public static void main(String[] args) {
ArrayList al = new ArrayList();
for(int i = 0; i < 10; i++ )
al.add(new Int(i));
System.out.println("al: " + al);
ArrayList al1 = (ArrayList)al.clone();
// Increment all al1's elements:
for(Iterator e = al1.iterator(); e.hasNext
(); )
((Int)e.next()).increment();
// See if it changed al's elements:
System.out.println("al: " + al);
System.out.println("al1: " + al1);
}
}
class Int {
private int i;
public Int(int ii) { i = ii; }
public void increment() { i++; }
public String toString() {
return Integer.toString(i);
}
}
o/p:
al: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
al: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
al1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
the old ArrayList and the cloned ArrayList are aliased to
the same objects
Is This Answer Correct ? | 6 Yes | 8 No |
what is the constructor and how many types of constructors are used in java?
What do you mean by singleton class in java?
What are reference variables in java?
How many types of string data types are there?
class A{ m2(){ } } class B extends A{ m2(){ } } class c extends B{ m2(){ } } class my_class extends c{ m2(){ } pulic static void main(){ ...My_class a = new my_class(); super.super.super.m2(); is this is leagal if not find what is the legal procedure in order to call A's version of m2(); }
Write down program for following scenario. Use java coding standard. You have array list with some words in it..we will call it as dictionary….and you have a arbitrary string containing some chars in it. You have to go through each word of dictionary and find out if that word can be constructed with the help of chars from arbitrary string given. If you find the word print it else print none.
Explain access specifiers?
0 Answers Thomson Reuters, Virtusa,
Explain 5 features introduced in jdk 1.7?
What are Advatages of Overloading and Overridding.
Why can't we make a class private in java?
What is UNICODE?
can u give one sinario when you use Abstract Class and When you use Interface.