Core Java - [First Java Program]

♠ Posted by Unknown in at 22:48

First Java Program 


Implementation of a Java Program involves a series of steps. The include.
  1. Creating the program
  2. Compiling the program
  3. Running the program.



Remember that, before we begin creating the program, the Java Development Kit (JDK) must be properly installed on our system.


1. Creating the Program


We can create a program using any text editor. Assume that we have entered the following program: 
Example: 

import java.lang.*; 
import java.io.*; 

class Test { 
     public static void main(String [] args) { 
          System.out.println("Hello!"); 
          System.out.println("Welcome to the world of Java."); 
          System.out.println("Let us learn Java."); 
      } 
}

We must save this program in a file called Test.java ensuring that the filename contains the class name properly. This file is called the source file. Note that all Java source files will have the extension .java. note also that if a program contains multiple classes, the file name must be the class name of the class containing the main method.


2. Compiling the Program.


To compile the program, we must run the Java compiler javac, with the name of the source file on the command line as shown below: 
                                         javac Test.java 
if everything is OK, the javac compiler creates a file called Test.class containing the bytecode of the program. Note that the compiler automatically names the bytecode file as <classname> .class


3. Running the program.


We need to use the Java Interpreter to run a stand-alone program. At the command prompt, type 
                                         java Test 

Now, the interpreter looks for the main method in the program and begins execution from there. Note, that we simply type “Test” at the command line and not “Test.java” or “Test.class”.

0 comments:

Post a Comment