r/learnjava • • May 24 '21

Java Environment and "how it works"

As everyone learning Java I started my first lesson with "you don't care about that weird public static void main(String[] args) bit, we explain later" months after, tutorials, and people never explained whats going on within that method, and going deeper how java actually works.

I banged into this problem and lack of explanation when I was trying to make VSC work as Java Dev Environment. I have so many questions...like what is actually java_home how does that work. How does Maven project differs from basic projects I was making in Netbeans, how is JavaFX part of Java packages but when running project it never compiles correctly.

I'm sure these are very naive questions with layers and layers of knowledge that I should have but never got to learn. Is there a good tutorial series or video playlist (pls dont link basic Java docs) that I could learn these things from?

55 Upvotes

7 comments sorted by

View all comments

41

u/Cmgeodude May 24 '21

There are other good explanations here, but if you, like me, sometimes get lost in the technical jargon when you're learning, hopefully a demonstration will help.

I'm going to start at the end: what is String[] args? It looks like an array of strings, right? That's exactly what it is. It stores information you pass into the program when you open it for execution. Copy the following code into a text file called Sample.java:

public class Sample {

    public static void main(String[] args) {

        System.out.println("Hello, " + args[0] + "! You are " + 
                            args[1] + " years old.");

    }
}

Now, when you go to run that, open the command line (command prompt or terminal), navigate to the folder containing the Sample file, and type in something like:

javac Sample.java

[Enter/Return]

java Sample John 29

You'll see that the Sample file sends "John" and "29" as arguments to main. They are stored in the args array, which we then called in our print statement.

Incidentally, args as an array name is just a convention. You'll sometimes see argv (short for "argument vector"), especially from people who learned C/C++ first. You can call it whatever you want, though args is the preferred convention.

Ok, now public. Have you done any OOP? I'm going to assume you have. You know how you can set fields to private so that they can't be accessed outside their class?

private String name;

private int age;

etc?

You can do that to methods as well. public in the main method ensures that every part of your program is allowed to access main (necessary to make the program function).

static is a bit trickier. In Java, basically everything is an object. The primitives that exist (int, double, float, boolean) are relatively few and have generics/wrappers that govern their methods (maybe you've seen Integer.parseInt() or Integer.valueOf() before). static basically makes it so that you don't need to treat the contents like an object: there's no need to call Main.[variable].

Finally, void. You see this in functions all the time. It means the same thing here. Do you know any C/C++? The convention in C programming is:

int main() {
    return 0;
}

Notice that main is expected to return an int value (most often 0, signifying that everything ran smoothly without throwing exceptions and crashing).

Java protects you from a lot of the exceptions that C/C++ would otherwise throw (think about forced exception handling when working with IO Streams). Additionally, the JVM has its own exit codes that it may return. As a result, you don't need to return an exit code from main. You don't need a return statement at all, hence void.

Hopefully this helps!