• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need your help about a cast about cloneable.!!!(jcjp07-Q119)

 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q119 Which of the following are true?
A A reference to an array can be cast to a reference to an Object.
B A reference to an array can be cast to a reference to a Cloneable.
C A reference to an array can be cast to a reference to a String.
D None of the above.

the given ans is:A,B.
My ans is:A.
Am I correct ?If not ,correct me Pls!!
You are always to the item!!!
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gong James:
Q119 Which of the following are true?
A A reference to an array can be cast to a reference to an Object.
B A reference to an array can be cast to a reference to a Cloneable.
C A reference to an array can be cast to a reference to a String.
D None of the above.

the given ans is:A,B.
My ans is:A.
Am I correct ?If not ,correct me Pls!!
You are always to the item!!!

Ans: A,B
How its becoz Every array implements the interfaces Cloneable and java.io.Serializable.
class Test {
public static void main(String[] args) {
int ia1[] = { 1, 2 };
int ia2[] = (int[])ia1.clone();
System.out.print((ia1 == ia2) + " ");
ia1[1]++;
System.out.println(ia2[1]);
}
}
Pls refer JLS for further expln
Enjoy
Ragu
 
Rashmi Hosalli
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ragu,
Shouldn't ia1.equals(ia2) in your example code give true?
Rashmi
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rashmi
I believe clone() makes a shallow copy not a deep one
Ragu
 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You meant that java.lang.reflect.array is subclass of the interface java.lang.cloneable.

But i cant find the description in the jdk_doc(v1.3).
Where the conclusion "array class implements interface cloneable" comes from?
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this about arrays in the JLS http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#61637
 
Jim Petersen
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gong James:
You meant that java.lang.reflect.array is subclass of the interface java.lang.cloneable.
But i cant find the description in the jdk_doc(v1.3).
Where the conclusion "array class implements interface cloneable" comes from?

Hi Gong:
Hope this clears up the confusion... java.lang.reflect.array inherits (is a subclass of) from java.lang.Object, and directly inherits the clone() method from java.lang.Object which to quote the API states:
The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.
- all the best Jim

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic