In the java book im reading (Java: a beginner's guide by Herbert Schildt) it usually displays the main as such:

public static void main(String args[]) {

but often in this website I see code written as this:

public static void main(String[] args) {

Is there a functional difference between the two? Or is it really just preference?

Recommended Answers

All 6 Replies

It's prefence. As long a you have an String array as the argument, it can have any name, and be "formatted" anyway.

public static void main(String[] args)
public static void main(String args[])
public static void main(String[] s)
public static void main(String s[])


All of those are perfectly legal. Actually as long as it's public, static, void, named main, and as a String[] as a parameter, This is even legal:

static public void main(String[] args)


So basicly it comes down to personal preference.

For good practice I think it's best to put the braces after the data type:

String[]


That will save some confusion and readability down the road.

There is no difference in Java (in other languages there may be).

Just be consistent in your use in order to avoid confusing people.

The second syntax is preferred by many people because it more accurately describes what Java does under the hood (in Java an array of whatever is actually an object of an array type) but the first more closely describes what the user of the array expects, which is to see an array of objects and is preferred by a considerable minority of people.

Sun themselves uses both in different publications :)
In the JLS James Gossling talks about String[] arr, in the Java coding style guidelines I find Object arr[].
In fact, in that same document the authors state explicitly that that is the preferred way (section 8.1.3).
http://www.sun.com/software/sundev/whitepapers/java-style.pdf
But I've seen both used in coding style guides for different projects (and there are other things in the Sun guidelines I don't agree with).

8.1.3 Array declarations
        The brackets “[] in array declarations should immediately follow the array name, not the type. The exception
        is for method return values, where there is no separate name; in this case the brackets immediately
        follow the type:
               char[] buf; // WRONG
               char buf[]; // RIGHT
               String[] getNames() { // RIGHT, method return value
        There should never be a space before the opening bracket “[.

hi
it can even be

static public void main(String args[])
{
            //your code..........
}

<access modifiers><access specifiers><return type><method name> is the correct convention.Although you can interchange the first two as I have done.

Following are java access specifiers. There are 10 of them

1. public
2. private
3. protected

4. abstract
5. final
6. native
7. transient
8. volatile
9. synchronized
10. static

The first three are catagorized as access modifiers. Although the next seven does not have any sub catagory. You can interchange the access specifiers when you are declaring a variable or method as I had done.
:p
Srinivas

wow java rocks! there's so much depth, yet alot of freedom!

Hi everyone,

wow java rocks! there's so much depth, yet alot of freedom!

Well said

Richard West

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.