An Applet is generally a small application module that is developed to perform one specific task and it runs under the umbrella of another bigger application. In other words, an Applet often acts as a plug-in which can easily be integrated into a suitable application.

Similarly, Java Applet is a small module written in the Java programming language which can be injected into any webpage to perform a specific task. The origin of the word “Applet” was first noticed in PC Magazine, 1990.

Java Applet is run in a Java Virtual Machine (JVM) delivered on the browser as Java Bytecode. It was first pioneered in 1995 when the first version of Java language was released. It is supported in multiple platforms such as Windows, Mac, Linux, etc. The multi-platform support is inherited from Java Bytecode, which runs across environments using JVM.

The key reason for the success of Java Applet was that it could run within a browser, on command-line interface, could be remotely invoked, and also execute offline. Being confined within the boundaries of an Applet module, a Java Applet cannot interact or control the code outside. Therefore, it was used very wisely for small specific tasks only.

Unlike HTML and CSS, Java Applet could induce 3D graphic rendering, catch events from peripheral devices, and create games. This supremacy helped a lot in areas like visualization, teaching, gaming, real-time communication, etc.

How is Java Applet built and executed?

Almost every web browser executes a Java Applet in a sandbox environment, keeping it away from any local machine access such as file system or clipboard. An Applet’s code is downloaded and embedded into a browser to display it to an end-user.

To write a Java Applet, you must inherit the java.applet.Applet class, or the javax.swing.JApplet class when using Swing. It is a direct descendant of Container via Panel. Therefore, a Java Applet possesses all capabilities of a Java language and interface.

Java Applet Inheritance

A sample Java Applet is shown below:

import java.applet.Applet;
import java.awt.*;

public class MyFirstJavaApplet extends Applet {

     public void init() { }

     public void stop() { }

     public void paint(Graphics g) {
          g.drawString("This is my first Java Applet.", 50, 50);
          g.drawArc(50, 40, 15, 15, 0, 360);
     }
}

In order to compile and execute the above Applet, save the file as MyFirstJavaApplet.java. Compile it using Java and you should get a file called MyFirstJavaApplet.class. This class file should then be invoked within a web browser using the Applet or Object tag.

<html>
<body>
<APPLET code="MyFirstJavaApplet.class" WIDTH="200" HEIGHT="100">
</body>
</html>

Advantages and Disadvantages of Java Applet

Advantages:

  1. It is cross-platform and supported by most major web browsers.
  2. It works as a plug-n-play programming module. Therefore, a single Java Applet can be used within multiple applications.
  3. Its execution speed is fast since the Applet is cached by most web browsers.
  4. Since an Applet is standalone program module, it’s easily scalable and maintainable.
  5. Because a Java Applet cannot access user’s local machine, it makes it safe and secure.

Disadvantages:

  1. Java needs to be installed in a web browser to execute Java Applet.
  2. Few native mobile browsers in ecosystems such as iOS or Android do not support Java Applets at all.
  3. Many Java Applets may require a specific version of Java Runtime Environment (JRE). If a newer version of JRE is required, the Java Applet may download the entire package before it executes.