Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Operators and Assignments

 
John Mathew
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Green {
public static void main (String args[]) {
int[] i = null; // 1
Cloneable c = i; // 2
i = (int [])c; // 3
}}

What is the result of attempting to compile and run the program?

a. Compile-time error at line 1.
b. Run-time error at line 1.
c. Compile-time error at line 2.
d. Run-time error at line 2.
e. Compile-time error at line 3.
f. Run-time error at line 3.
g. None of the above


the answer is none None of the above.Can anyone elaborate on this?How is it possible to assign null reference to an integer array.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rahul,

Welcome to JavaRanch;

Is this SCJP question?

Cloneable is not on the exam objective list;




-------
cmbhatt
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can set any object reference equal to null.
 
Joshua Smith
Ranch Hand
Posts: 193
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul-

The fact that the question uses Comparable is not all that important. The key thing to understand about it is that arrays of primitives are objects and can be manipulated as such. That means that you can cast them, assign them to Object references, call toString(), hashCode() and equals() on them, etc.

While some of the code below looks weird, it's totally valid:


Hope that helps,
Joshua Smith
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic