My First Java Program (Cont.)


Eevery Java program has a class name which normally matches the filename, and that every program must contain the main() method, which is a standard method used by JVM (Java Virtual Machine) to start execution of any Java program. The following list shows the steps of compiling the Java program and executing it in the command prompt of our Linux server undcemcs02:

 undcemcs02> man                       # On-line reference manuals
 What manual page do you want?

 undcemcs02> man uname                 # Print system information.
 uname - print system information
 uname [OPTION]...

 undcemcs02> uname -a                  # Display system information.
 Linux undcemcs02 3.10.0-957.1.3.el7.x86_64 No 1 SMP
 Thu Nov 15 17:36:42 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

 undcemcs02> which javac java          # Show the full paths of javac and java.
 /usr/bin/javac
 /usr/bin/java

 undcemcs02> emacs MyFirstClass.java   # Edit and create a file.

 undcemcs02> ls                       # List directory contents.
 MyFirstClass.java  

 undcemcs02> cat MyFirstClass.java     # Concatenate files and print.
 public class MyFirstClass {
   public static void main( String args[ ] ) {
     System.out.println( "Hello World" );
   }
 }

 undcemcs02> javac MyFirstClass.java    # Compile the program.

 undcemcs02> ls                        # List directory contents.
 MyFirstClass.java  MyFirstClass.class 

 undcemcs02> java MyFirstClass          # Launch the Java application.
 Hello World

The method System.out.println() is overloaded to accept all kinds of data types in Java. It takes various kinds of input (such as String, int, float, double, or even char) and prints it out. All of those methods are collectively referred as an overloaded method in Java.

MyFirstClass.java (a hello-world program)

 public class MyFirstClass {
   public static void main( String args[ ] ) {
     System.out.println( "" );
   }
 }