Skip to main content

Hello world

1 reply [Last post]
Joined: 2009-08-17

* The HelloWorld2 example
public class HelloWorld2
public static void main(String[] args)
System.out.println("Hello, " + args[0]);

this short code pops out

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at HelloWorld2.main(

any idea why?
im new to java done C++ b4

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2009-08-25

Hi shong...

It is printing out an ArrayIndexOutOfBounds exception because you are referring to args[0] String array. By the time you are referring to the array's element 0, it doesn't contain any element. This array contains the command-line arguments you send to the java class' main could try to execute this class as below...

java HelloWorld2 shong

...and it should print out "Hello, shong".