• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic