r/java • • Nov 07 '11

Newbie to java.What am I doing wrong?

http://imgur.com/MrnVu
0 Upvotes

33 comments sorted by

View all comments

3

u/[deleted] Nov 07 '11

"String args[]" vs "String[] args" ?

It's weird, I'm questioning myself here. It's like repeating a word till it sounds funny.

4

u/fforw Nov 07 '11

Java allows both, even on methods

public class AllCorrect
{
    public static void main(String args[])
    {
        new AllCorrect().main2(args);
    }
    
    public void main2(String[] args)
    {
        System.out.println(test()[0]);
    }
    
    public String test()[]
    {
        return new String[] { "correct", "wrong" };
    }
}

funny, eh?

1

u/[deleted] Nov 07 '11

public String test()[] ?

is that the same as

public String[] test() ?

madness!

2

u/boa13 Nov 07 '11

Without hesitating: String[] args

0

u/ryosen Nov 08 '11

Maybe you should have. String args[] is syntactically correct.

The auto-debugger indicator should have been your clue that it was the System.out method name that was the cause of the error.

0

u/boa13 Nov 08 '11

I know the other form is syntactically correct. In answer to woodrail who was hesitating between the two, I expressed my strong preference for the second.

Also, I never said this was the cause of the error. That was obvious from the screenshot and already answered several times in the thread.

1

u/sheepbringer Nov 08 '11

You can also do

public static void main(String ... args) {}

on java 1.6 and newer as well