Forums Register Login

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

+Pie Number of slices to send: Send
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!!!
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
Hi Ragu,
Shouldn't ia1.equals(ia2) in your example code give true?
Rashmi
+Pie Number of slices to send: Send
Rashmi
I believe clone() makes a shallow copy not a deep one
Ragu
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
 

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

The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1098 times.
Similar Threads
Reference Conversion
Conversion of Objects
casting
Aren't Collection and AbstractMap peers? what's the common subclass?
a question about Array
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 23:51:55.