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:

01undcemcs02> man                       # On-line reference manuals
02What manual page do you want?
03 
04undcemcs02> man uname                 # Print system information.
05uname - print system information
06uname [OPTION]...
07 
08undcemcs02> uname -a                  # Display system information.
09Linux undcemcs02 3.10.0-957.1.3.el7.x86_64 No 1 SMP
10Thu Nov 15 17:36:42 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
11 
12undcemcs02> which javac java          # Show the full paths of javac and java.
13/usr/bin/javac
14/usr/bin/java
15 
16undcemcs02> emacs MyFirstClass.java   # Edit and create a file.
17 
18undcemcs02> ls                       # List directory contents.
19MyFirstClass.java 
20 
21undcemcs02> cat MyFirstClass.java     # Concatenate files and print.
22public class MyFirstClass {
23  public static void main( String args[ ] ) {
24    System.out.println( "Hello World" );
25  }
26}
27 
28undcemcs02> javac MyFirstClass.java    # Compile the program.
29 
30undcemcs02> ls                        # List directory contents.
31MyFirstClass.java  MyFirstClass.class
32 
33undcemcs02> java MyFirstClass          # Launch the Java application.
34Hello 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( "" );
   }
 }