The Best Technical Blog Ever


Simple Java Program

Write a simple program in a text editor with the file name The file name should be the class name which contains the main method. Java programs are case sensitive. In general, classes are created to perform a specific task. The structure of Java class is defined by its class name followed by an open curly brace, a list of methods and fields, and a close curly brace. The name of the class is formed by alphabetical characters and digits without spaces, the first character must be alphabetical. In Java, every line of code that can actually run needs to be inside a class.

Example 1
simple java program

Compile the program with the javac command.(Java compiler). That means convert the source code into bytecode using JVM. So any computer with a Java VM can interpret and run the program.


Here, You need to set the class path to point to the directory where the Sample class is located. The class path can be set using either the -classpath option with the javac compiler command and java interpreter command, or by setting the CLASSPATH environment variable. If there are any syntax errors, go back to Notepad and edit the program. Once the program successfully compiles into bytecodes, then you can interpret and run it. By invoking the JVM byte code interpreter, we can convert the Java byte codes into platform-dependent machine codes. So your computer can understand and run the program. It is done by using the command,

java Sample

At the command line, you can see the result as,

It is a first example program

program explanation

In this example program, the 'public' keyword defines the access specifier, that means any other class can access this class. The line "public static void main (String [] args )" shows where the program will start its execution. The main() method will always reside in one of class files. The Java language does not allow methods outside of class definitions.

  • public -again means that any class can access it.
  • static -defines that you can run this method without creating an instance of Sample class.
  • void- defines that this method doesn't return any value.
  • main- name of the method

The main method in program consists of a single statement "System.out.println("It is a first example program"); " The statement outputs the character between quotes to the console.

  • System- is a pre-defined class that holds some useful methods and variables.
  • out- is a static variable within System that represents the output of the program (stdout).
  • println- is a method of out that can be used to print a line.

Example 2

The data type of the parameter variable args of the main method is an array of String. args[0] is the first element of this array. args[1] is the second element of this array. When we execute this program using the following syntax:

java Sample word0 word1

The system will pass the word0 to the first String parameter variable and the word1 to the second String parameter variable and so on. Here args.length is 2.

Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status