Our First Java Program



In this lesson we will learn to write our first Java program. Before doing so, let’s first understand some basics of Java program files.

Java Program Files:

Java program files are very similar to the .c or .cpp program files. In Java the source code is written in the .java file. Each .java source file can contain at most one public class. If there is a public class, then the class name and file name must match. Furthermore, every piece of java code must be the part of a class.

The Java compiler creates Bytecode from the .java program file and stores the same in a .class file. This Bytecode, contained in the .class file, generated by the Java compiler is ready to be executed by the Java Virtual Machine (JVM). For each class in a source file (both public and non-public classes), the compiler creates one ".class" file, where the file name is the same as the class name.
Our First Java Program:
Let’s write our first Java program:
class HelloWorld {               
                   public static void main( String [ ] args )         {                                 
                                    System.out.println("Hello world!");              
     }        
}
You must be glad to see that the above code is very similar to the C or C++ code. When the above code is run after compilation, it simply prints “Hello world!” to the console.