Simple Java Program
by Krishna viswambharan
Posted on 18 Jul 2018 Category: Java
Edited on 25 Jul 2018
Simple Java program
In this section we will learn how to write a simple java program.
The first thing that you must learn about java programming is that the name you give to the source file is very important. For this, example the name of the source file is Hello.java. The name of the class defined by the program is also Hello, this is not coincidence. In java the name of the class should match with the name of the file that holds the program.
Let’s look at a simple java program
public static void main(String args)
System.out.println("Welcome to Java Program");
The keyword class declares the new class. Hello is an identifier that is the name of the class. Line 3 begins the main() method. The public keyword is an access specifier which allows the programmer to control the visibility of class member. The keyword static allows main() to be called without having to instantiate particular instance of the class. The keyword void simply tells the compiler that main() does not return a value. In main() there is only one parameter, String args declares a parameter named args, which is an array of instances of the class String. Line 4 outputs the string “Welcome to Java program “. The line begins with System.out, System is a predefined class that provides access to the system and out is the output stream that is connected to the console.
Create the java program
Compile and run the java program
To compile : javac Hello.java
Run : java Hello