Course Content‎ > ‎Session 1‎ > ‎

Java Application and Java Applet

What is a Java Application and a Java applet?

There are two types of programs that can be developed in Java. These are:

1. Application
2. Applets

Java application

Java application is a stand-alone program. Java programs do not require any external viewer program i.e. a Java application can be directly executed using Java interpreter.

Java applet

Java applet refers to an application that is designed for use on the World Wide Web. To execute a Java applet, an external viewer program like a web browser or an applet viewer is required.

Creating your First Java Application

Given below is the most basic Java program, which will give an understanding to the basic concepts of Java language:

The program code given below can be written in Window’s notepad or MS-DOS’s Edit or any other text editor.

class FirstProgram
{
public static void main(String args[ ])
{
System.out.println("This is my first program in Java");
}
}
  
Java is a case-sensitive language. Let’s understand the points related to the above program.   The code given above is called as the source code. This code can be stored in a file, which will have the same name as the name of the class. So, it will be called as FirstProgram.java. The source code has to be compiled by means of a Java compiler i.e. javac. To compile the source code, you will type the following at the DOS prompt:

javac FirstProgram.java

When the source code has been compiled, the compiled code will be stored in a file FirstProgram.class. This file is automatically created, after you compile the source code. Now the program is ready to run. To run this program, you type the following at the DOS prompt:  

     java FirstProgram  

When you run the program, the following is displayed on the screen:

This is my first program in Java  

Let’s go through the following points to understand the different parts of the above program:

1. Java relies heavily on classes. The first statementclass FirstProgram is a class and not just a program. The name of the class is defined as FirstProgram. This name is used by the Java compiler i.e. FirstProgram.class. (Java compiler creates a class file for each class defined in a Java source file). This means that if there is more than one class defined in the source code i.e. .java file, then the Java compiler will store each class in a separate.class file.

2. Then comes the statement:

public static void main(String args[])

main is a method or a function (in Java it is called as a method). The main method is where the execution begins, when the class is executed in the Java interpreter. Themain method is defined as being public static and void.public means that the method can be called from anywhere inside or outside of the class. static means that the method is same for all instances of the class. void meansmain does not return a value. String args[ ]: main method is defined as taking a single parameter i.e. String args[ ]. args[ ] is an array of String objects that represent command line arguments passed to the program upon execution. If the program does not use command line arguments, args[ ] parameter is not used in the program. Then the next statement is:  

System.out.println("This is my first Java program");

When the main method is called, upon the execution of theFirstProgram.class, this statement prints:  

This is my first Java program
 
to the standard output stream i.e. screen. println is a method of the out object and out object is a member variable of the System object. out represents standard output stream.

3. Every statement in Java ends with a semi colon.

Creating your first Java applet

Now we will run the above program as an applet instead of application. We will refer to it as FirstApplet instead ofFirstProgram. Given below is the code to run the above program as an applet:

import java.applet.Applet;
import java.awt.Graphics;
public class FirstApplet extends Applet
{
public void paint(Graphics g) 
{
g.drawString("This is my first applet",100,100);
}
}  

You will save this code as a file FirstApplet.java because the class name is FirstApplet.

1. Now we will compile the Java applet file. Java applet file is compiled the same way as Java application file. At the DOS prompt, you will type:  

javac FirstApplet.java
 
If the file has been correctly compiled, it will be saved as FirstApplet.class. After the Java applet file has been successfully compiled, the next step is to run the applet. To run an applet inside a web page in a browser or in appletviewer, an HTML file has to be made. This HTML file will refer to FirstApplet.class file. Given below is the code for HTML file, which will run this applet. This code can be typed in Notepad or any other word editor:

<HTML>
<HEAD>
<TITLE>Demo of a Java applet</TITLE>
</HEAD>
<BODY>
<APPLET CODE="FirstApplet.class" WIDTH=300 HEIGHT=250>  
</APPLET>
</BODY>
</HTML>

Save this file as app1.html (you can give any name to html file but the extension should be .html). If you have to create html file for any of your applets, simply replaceFirstApplet.class with your Java class file name in the Applet tag.  For example you have to make html file forfile1.java, first compile file1.java, which will result in the file file1.class.  Then in code for app1.html, simply replace,

<APPLET CODE="FirstApplet.class" WIDTH=300 HEIGHT=250>  

with 
 
<APPLET CODE="file1.class" WIDTH=300 HEIGHT=250>

If you are using a Java-enabled browser such as Internet Explorer or Netscape to view your applet files, you can choose File | Open Page to select app1.html file. If you don’t have any browser installed on your system, you can use appletviewer application to run this applet.  Theappletviewer application comes as a part of JDK. Though appletviewer is not a web browser, but it can be used to test the applets. To run your applet simply type the following at the DOS prompt:

appletviewer app1.html

Now you can view your first applet running.  It will display the line:

This is my first applet

on the screen.

Now let’s try to understand the different parts of this Java applet:

import java.applet.Applet;

import java.awt.Graphics;

By default all the Java programs import java.lang package, which provides the core functionality of the Java language. But to import methods and classes from other packages,import method is used. import is a reserved word in Java and it is used to include the classes from other packages in the Java program. This command makes some of the standard features of Java language available to your program. There is a separate import command for each group of functions that is imported. To create any applet, class Applet has to be imported. You can import an individual class like:

import java.applet.Applet;

or, you can import an entire package of classes by using * i.e. asterisk :

import java.applet.*;
public class FirstApplet extends Applet
 
public keyword informs Java that this class i.e.FirstApplet and its contents should be made available to other classes. extends means that the class FirstAppletextends the Applet class. By extending the Applet class,FirstApplet class inherits the functionality of the Appletclass.

The opening brace { marks the beginning of the classFirstApplet.

public void paint(Graphics g)

This line declares a method called paint that overrides thepaint method of AWT package. This method is overwritten so that the applet can draw the String

This is my first applet

to a specific location. The modifier void states that the method does not return a value. When the paint method is called, an object of Graphics class has to be sent to it.Graphics is an abstract class for all graphics objects. gis the Graphics object.

g.drawString("This is my first applet",100,100);
 
The actual work is done by the method drawString. ThedrawString method accepts the arguments i.e. the string and position where this string has to be displayed. Here the string is:

This is my first applet

and it will be displayed at the x, y coordinate 100, 100. The closing brace } marks the end of the class FirstApplet.
Comments