File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Array Assignment

R Kumar

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];

b)some garbage
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

Joined: Jul 27, 2007
Posts: 24

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!
B Misra

Joined: Jul 27, 2007
Posts: 24
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: 15081

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 8 API documentation
R Kumar

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:
subject: Array Assignment
It's not a secret anymore!