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 A rather silly one Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A rather silly one " Watch "A rather silly one " New topic

A rather silly one

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327

public class test
public static void main( String[] args )
int i = 3;
System.out.println(" From command line " + args.length); // works fine and prints 0
//int[] iArr;
//System.out.println(" Length of array " + iArr.length); // compile error - Array needs to be //initialized

Why does printing the length of the argument String[] args
not give any compile time error ?
While this is not the case with the local declared array iArr.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Anjela,
The String[] args of the main is initialized by the JVM.However, locally defined array needs to be initialized before you calculate its length.
Alternatively, you could declare the array as a class variable for compilation.However, still you will get RuntimeException, unless you initialize the array.
So the following code will compile but not run correctly :

The following code will compile and run properly :

The following code will not compile :

The following code will compile and run correctly

Hope this helps,
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)

I agree. Here's the link:
subject: A rather silly one
jQuery in Action, 3rd edition