In this blog we will learn How to execute First Java Program and Brief explanation of the Java Syntax.
To create your first Java program, begin by creating a new file in your text editor (like Notepad) and save the file as HelloWorld.java. Then paste this code in that file and save the file:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Note: In order for Java to recognize this as a public class (and not throw a compile time error), the filename must have the same name as the class name(HelloWorld in this example) with a .java extension and should have public access modifier before it. Naming conventions recommend that Java classes begin with an uppercase character, and be in camel case format, meaning no underscores (_) or dollar signs ($), even though these can both technically be used, and the first letter of each word is capitalized.
To compile your code, open the directory where HelloWorld.java is located in a terminal window:
cd/path/to/containing/folder/
Note: cd is a terminal command which mean change directory
and enter javac followed by the file name and extension as follows:
$ javac HelloWorld.java
Note: The javac command invokes the Java compiler.
The compiler will then generate a bytecode file called HelloWorld.class which can be executed in the Java Virtual Machine (JVM). The Java programming language compiler reads source files and convert them into bytecode class files.
To run your program, enter java followed by the name of the class with the main method (HelloWorld in our example). Note how the .class is omitted:
$ java HelloWorld
Note: The java command runs a Java application.
This will output to your console:
Hello, World!
You have successfully programmed and built your very first Java program!
Note: In order for Java commands (
java,javac, etc) to be recognized, you will need to make sure:
- A JDK is installed (e.g. Oracle, OpenJDK and other sources)
- Your environment variables are properly set up
You will need to use a compiler (
javac) and an executor (java) provided by your JVM.java -versionandjavac -versionon the command line, will report which versions (e.g.1.8.0_73) of these are installed.
A closer look at the Hello World program
The “Hello World” program contains a single file, which consists of a HelloWorld class definition, a mainmethod, and a statement inside the main method.
public class HelloWorld {
The class keyword begins the class definition for a class named HelloWorld. Every Java application contains at least one class definition.
public static void main(String[] args){
This is an entry point method (defined by its name and signature of public static void main(String[])) from which the JVM can run your program. Every Java program should have one. It is:
- public: meaning that the method can be called from anywhere mean from outside the program as well.
- static: meaning it exists and can be run by itself (at the class level without creating an object).
- void: meaning it returns no value.
It accepts:
- An array (typically called
args) ofStrings passed as an arguments to main function (e.g. from command line arguments).
Almost all of this is required for a Java entry point method. The exceptions being:
- The name
argsis a variable name, so it can be called anything you want, although it is typically calledargs. - Whether its parameter type is an array (
String[] args) does not matter because arrays can be passed into varargs.
Note: A single application may have multiple classes containing an entry point (
main) method, and the entry point of the application is determined by the class name passed as an argument to thejavacommand.
Inside the main method, we see the following statement:
System.out.println("Hello, World!");
Let’s break this statement down element-by-element:
this parenthesis signifies the closure of the parameters being passed into the printlnmethod.
| Element | Purpose |
|---|---|
System |
this denotes that the subsequent expression will call upon the System class. |
. |
this is a “dot operator”. Dot operators provide you access to a class’s data1. In this case, this dot operator allows you to reference the out static field within the System class. |
out |
this is the name of the static field of PrintStream type within the System class containing the standard output functionality. |
. |
this is another dot operator. This dot operator provides access to the println method within the out variable. |
println |
this is the name of a method within the System class’s out variable. This method in particular prints the contents of the parameters into the console, on a new line. |
( |
this parenthesis indicates that a method is being accessed (and not a field) and begins the parameters being passed into the println method. |
"Hello, World!" |
this is the String literal that is passed as a parameter, into the println method. The double quotation marks on each end delimit the text as a String. |
) |
|
; |
this semicolon marks the end of the statement. |
} // end of main function scope</span> } // end of class HelloWorld scope
The method body and class body are then closed.
Awesome buddy 🙂
Wonderfull explaination is here, Like it
LikeLike