954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

What's the difference?

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?

asatrujesus
Newbie Poster
12 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

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.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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 “[.
jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

hi
it can even be
[HTML]
static public void main(String args[])
{
//your code..........
}
[/HTML]

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

cheenu78
Light Poster
45 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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

asatrujesus
Newbie Poster
12 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Hi everyone,

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

Well said

Richard West

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You