youtube facebook twitter gmail pinterest

Hello World Java Program

First Simple Program:
Let’s start by compiling and running a simple program.This is a simple Java program.Name this file as "firstprogram.java".

class firstprogram {
// Your program begins with a call to main() should be in this format.
public static void main(String args[])
System.out.println("This is a simple Java program.");

Description about the Program:
The first thing that you must remember about Java is that the naming of the source file.For this example,the name of the source file should be "firstprogram.java".Because source file is considered as a compilation unit in java.Java compiler checks for the source file with the ".java" extension.As you can see the name of the program and the class name are name(i.e.firstprogram).It is not just coincidence.But is necessary to give the same name.The reason is that the name of the class sholud match with the name of the file that holds the program.Java is case-sensitive so any captilization of file name should also be checked carefully.

Compilation of the Program:
To compile the firstprogram.java,first we must execute the compiler by using the javac command followed by specifying the name of the source file on the command line, as shown here:

C:\>javac firstprogram.java
The javac compiler creates a file called firstprogram.class that contains the bytecode of the program.Thus,the output of javac is not code that can be directly executed.

Execution of the program:
To actually run the program, the Java command line source is used. Type java followed by program name in command line as shown here:

C:\>java firstprogram
The output for the above program is shown here:
This is a simple Java program.

Whenever Java source code is compiled, each individual class is put into its own output file named after the class and using the .class extension.It will automatically search for a file by that name that has the .class extension. Once it finds, it will execute the code contained in the specified class.

Advertisement Powered By Google Adsense


Share on whatsapp


Java java tutorials learn java java programming tutorial learn java online learning java professionals tutorials Multithreading Generics Object Oriented Language Methods Polymorphism Inheritance