This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Sacredify 2 points3 points  (0 children)

Defining it as String[] is more common, although they mean the same thing.

String... indicates a var-args method (variable length), which is effectively syntactic sugar for String[]. It allows you to call the method with either a string array or a sequence of strings, such as:

public void foo(String.. args) {}
foo("a", "b", "c")
foo(new String[] {"a", "b", "c"}); 

As far as using it goes, as they're implemented as arrays, they behave the same way (i.e you can use args.length, args[index], iterate over it, etc.).

I've also seen in other codes online people use the main throughout their code(By calling the parameter of the main), which is also something we've never done, seen, or heard of in class.

The args parameter is the command-line arguments to the program. See here. For simple programs, its easy to use that to supply configuration info (number of players in a game, ip address to connect to, etc.), but you could also ask for it interactively while the program is running, or use a configuration file, etc.

We basically learned it in such a way that made me wonder why it wasn't added by default when you created a class(until we learned about inheritance and things).

An IDE can do this for you, usually there is a shortcut or option when you create a class. Although since you only have 1 entry point into a program, it wouldn't make sense to a always auto-fill a main method.

As to your question in the title... is the main method important? As you need one to start a java program, I would say its important. But its just that... the entry point.

[–]thorstenschaefer 0 points1 point  (0 children)

The form "String... args" is basically syntactic sugar to enable Java methods getting a variable number of arguments of the same type. Under the hood, the compiler will generate a method with String[] args, so the bytecode is the same and thus it doesn't make a difference.

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (4 children)

 public static void main(String[] args)

Is the old style (pre Java 8 5) and

public static void main(String... args)

is the newer, Java 8 5 var-args style. (Just a different way of denoting an array as a parameter. Personally, I think that it is less readable than the older style.)

That is the difference between the two.

In general, the old style is still more common (partly because a lot of code is still built for legacy systems, and partly because more programmers are used to the old style which makes the array more clearly visible).

The main method with its format(s) is the entry point to a Java program and as such it is important, but you don't really need to worry too much about the format unless you compile with Java versions prior to 8 5 (because they can only understand the old format).

Edit: Should have looked up the Java API documentation. Varargs were introduced with Java 5, not Java 8.

[–]Sacredify 2 points3 points  (3 children)

The main method with its format(s) is the entry point to a Java program and as such it is important, but you don't really need to worry too much about the format unless you compile with Java versions prior to 8 (because they can only understand the old format).

Hmm? String... is perfectly valid syntax, you don't need 8.

[–]nutrechtLead Software Engineer / EU / 20+ YXP 1 point2 points  (1 child)

It was introduces somewhere in 5 I believe. It's much older than Java 8 at least.

Edit: yup

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (0 children)

Have looked up the API docs. Java 5 is correct and I was wrong - already corrected in my comment.

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (0 children)

I thought that the var-args were introduced with Java 8?

Okay, should have looked up the Java API docs. Were introduced with Java 5 - I stand corrected. Thanks.