Simple Java Programs
In this page we will learn about Simple Java Programs, First Java Program | Hello World Example, How it compiles, Parameters used in first Java Programs, Ways of writing Java Program, Valid and Invalid java main method signature.
First Java Program | Hello World Example
For creating simple java program, we need to create a class that will contain the main method of java programming. Now we shall understand the requirement first.
In order to executing any java program, you need to:
- Install the JDK if you don't have installed it, Java Development Kit (JDK) www.oracle.com.
- Set path of the jdk/bin directory. https://www.tutorialforbeginner.com/java-installation
- Create the java program
- Compile and run the java program
First Java Programs : Hello Java
import java.io.*;
class Simple{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
Output:
Hello Java
How it compiles:-
When we assemble Java program using javac tool, java writer converts the source code into byte code.
Parameters used in first Java Programs:-
Let's find out what's the meaning of class
, public
, static
, void
, main
, String[]
, System.out.println()
.
Class
keyword is used to declare a class in java.Public
keyword is an access modifier which represents visibility. It means it is visible to all.Static
is a keyword. If we declare any method as static, it is known as the static method. The core advantage of the static method is that there is no need to create an object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create an object to invoke the main method. So it saves memory.Void
is the return type of the method. It means it doesn't return any value.Main
represents the starting point of the program.String[]
args is used for command line argument. We will learn it later.System.out.println()
is used to print statement. Here, System is a class, out is the object ofPrintStream
class,println()
is the method of PrintStream class. We will learn about the internal working of System.out.println statement later.
Ways of writing Java Program:
There are many different ways to write a Java program. The modifications that we can do in a Java program are given below:-
- By changing the sequence of the modifiers, method prototype is not changed in Java.
Let us see the simple code of the main system:static public void main(String args[])
- The subscript notation in Java array can be used after type, before the variable or after the variable.
Let us see the different codes to write the main system.public static void main(String[] args) public static void main(String []args) public static void main(String args[])
- You can provide var-args support to the main method by passing 3 ellipses (dots).
Now Let us see the simple code of using the var-args in the main system. We will learn about the var-args later in Java New Features chapter:public static void main(String... args)
- Having a semicolon at the end of class is optional in Java.
Now let's see the simple code:class Sample{ static public void main(String... args){ System.out.println("Wellcome to Java Word"); } };
Valid java main method signature are given below:
public static void main(String[] args)
public static void main(String []args)
public static void main(String args[])
public static void main(String... args)
static public void main(String[] args)
public static final void main(String[] args)
final public static void main(String[] args)
final strictfp public static void main(String[] args)
Invalid java main method signature are given below:
public void main(String[] args)
static void main(String[] args)
public void static main(String[] args)
abstract public static void main(String[] args)