Is there anyone explain me that how a java program works.I writed a simple hello world program.See the code below

public Sample{
public static void main(String args[]){
System.out.println(“Hello world”);

what is this public static void main ?
why main method is static ?
why main method has String args[] argument?
why String,System first letter is capital ?
Explain me how System.out.println() works,what is it,how the process is going on background ?
Explain me how java program compile,runs ?


The main method is the entry point for your program. It needs to be public so that it can be accessed from outside the class, static because it isn’t called on an instance of your class, and void because it has no return value. The string array holds the arguments that the program was started with.

Don’t know exactly why String/System are capitalized, but that’s probably some naming convention, perhaps they are classes and classes should be capitalized (I’m not super well versed with java so that is a guess)

Not sure what you’re asking about println

Java is first compiled into bytecode (no longer looks like your code file, but it’s also not something the computer can read, it isn’t cpu instructions, it’s something in-between) and is then executed by a virtual machine (jvm) which is like a cpu except it’s a program (hence virtual machine). As the program runs, the jvm may decide to compile parts of the code further (optimize) according to which parts are used the most and how they get used.

Compiling and running looks like the picture below. You can see me supplying “” and “Hello” as arguments (which String[] args holds in a main function) to javac (compiler) and java (jvm)
Not shown in the screenshot is that javac created a new file, Hello.class, which is what the jvm will execute


Thank you.Can you explain String[] args in main method in detail


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.