File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array Assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array Assignment" Watch "Array Assignment" New topic
Author

Array Assignment

R Kumar
Greenhorn

Joined: Jul 11, 2007
Posts: 6
hi all. I came across the following code in one of the mock tests

public class Testing123 {

public static void main(String... args)
{
Number[]na = new Integer[3];
System.out.println(na[0]);
}
}

a)0
b)some garbage
c)3
d)compilation fails

the correct answer is supoosed to be compilation fails but when i tried this code it simply works fine and prints null..

could anyone tell wots wrong?
B Misra
Greenhorn

Joined: Jul 27, 2007
Posts: 22
Hi,

As far as i know this looks completely fine & should print null.(I'm new here though & this is my first post )

When the array of objects is constructed the individual elements get default null.
[ July 27, 2007: Message edited by: B Misra ]
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

I don't see anything wrong with it either, the public static void main(String... args) looks a little funny having the ....

What version of JDK are you using. The exam is for 1.4 and if you are using 1.5 then some example may work when they are not supposed to. I heard on here somewhere to make sure and use the version JDK that you are testing for.


Visit my blog! http://jameshambrick.com
B Misra
Greenhorn

Joined: Jul 27, 2007
Posts: 22
The question should be of some scjp5 exam itself, cause otherwise the var-arg declaration would not have been there & even if the main declaration have been wrong (it is right here) it would not have had produced any compilation error.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
You're just right it will print null.

Normally the System.out.println(object) will print the value object.toString() which will go wrong because there is no object. It's a null-reference.

but when the value of the reference is null the method will just print null.

You will get an error if you change the code like this.



Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

Note that with Remko's change (adding toString()) you will not get a compiler error; a NullPointerException will be thrown at runtime because you are trying to call a method on a null reference.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
R Kumar
Greenhorn

Joined: Jul 11, 2007
Posts: 6
Thanks all. This question is from the John Meyer's mock exam for scjp5. I guess there is some mistake in the question itself because the null option has not even been specified
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Assignment