we will learn how to write the simple / first program of java. We can write a simple hello java program easily after installing the JDK.
To create a simple / first java program, you need to create a class that contains main method. Lets understand the requirement first.
For executing any java program, you need to
Lets create the hello java program: FirstProgram.java
class FirstProgram{
public static void main(String args[]){
System.out.println("Welcome To java First Program");
}
}
To compile: javac FirstProgram.java
To execute: java FirstProgram
Output : Welcome To java First Program
Lets see what is the meaning of class, public, static, void, main, String[], System.out.println().
There are many ways to write a java program. The modifications that can be done in a java program are given below:
1) By changing sequence of the modifiers, method prototype is not changed.
Lets see the simple code of main method.
static public void main(String args[])
2) subscript notation in java array can be used after type, before variable or after variable.
Lets see the different codes to write the main method.
public static void main(String[] args)
public static void main(String []args)
public static void main(String args[])
3) You can provide var-args support to main method by passing 3 ellipses (dots)
Lets see the simple code of using var-args in main method. We will learn about var-args later in Java New Features chapter.
public static void main(String... args)
4) Having semicolon at the end of class in java is optional.
Lets see the simple code.
class A{
static public void main(String... args){
System.out.println("hello World");
}
};
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)
public void main(String[] args)
static void main(String[] args)
public void static main(String[] args)
abstract public static void main(String[] args)
If there occurs a problem like displayed in the below figure, you need to set a path. Since DOS does not know javac or java, we need to set a path. Path is not required in such a case if you save your program inside the JDK/bin folder. But its a good approach to set path. Click here for How to set path in java.
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning