Difference Between Applet and Application in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

In this article, we will understand the Difference between Applet and Application in Java. Before understanding the Difference between an Applet and an Application in Java, let us know what they mean. Java application is like any Java program which can be executed on a machine without any other application. It can be used without any GUI (Graphical User Interface).

While on the other hand, a Java applet is a program that can be run on a Java-supported browser. Java applets are embedded with the help of HTML. Therefore, Java applets are generally used for Graphical purposes.

Java Applet

A Java applet is a Java program that can be embedded in an HTML website. It runs inside the web browser. An HTML website uses the <APPLET> tag to use it. It does not have the access to all the resources of the system like local storage, and thus it can not read and write files on the system without permission. It does not require a main() method. For running the Applet code, a Java-supported web browser uses the Java Virtual Machine of the system to compile the code and then run it. A Java applet executes methods in this sequence : init(), start() and paint(). It does not need all these methods, but if there are multiple methods then it follows the sequence.

Features of Java Applet :

  • It requires a Java-supported web browser for its execution.
  • All the Java applets are a sub-class of the java.applet.Applet class.
  • main() method is not required
  • it follows this sequence while executing methods : init(), start(), paint() and then other methods.
  • Termination of a Java applet is done by stop() and destroy() methods.
  • It is used to display dynamic content.
  • They do not have access to local storage.
  • The Operating System does not completely trust Java applets.

Sample Code for a Java Applet

Example - 1 :
Let's say that we want to design a Java applet that displays any message.

Code :

To see how this applet runs, we need to embed it into an HTML file. We will use the applet tags.

Let us see the code for the same :

To run the applet, we will have to compile the java file using the javac in the terminal/command prompt.

We will run the following command :

Remember :
Modern Chrome and Firefox browsers do not support Java. To use this Java Applet, we will have to use some java supported browser or we can use the appletviewer. In this article, we will use the appletviewer.

Command to run in terminal/command prompt :

Output :

Java Applet Program Output

Java Application

Java application is a Java program that is stand-alone, i.e. it does not require a web browser to run. It uses JVM (Java Virtual Machine) to compile and run the code. It uses the resources of the system using the Java security model, and it is generally stored in the system where it is running. It creates .class files. A Java application requires a main() method for its execution. main() method calls for other required methods. It can read and write the local files of the system.

Features of Java Application :

  • It is a Java program and requires a main() method for its execution.
  • It has full access to the local storage as well as the network of the system where it is executed.
  • It is trusted by the Operating System.
  • It does not require any applications such as web browsers for its execution.

Sample Code for a Java Application

Example - 1 :
Let's say that we want to design a Java application that adds 2 numbers.

Code :

Output :

Key Difference Between Applet and Application in Java

Execution Environment:

  • Applet: Runs in web browsers using a Java plug-in.
  • Application: Runs standalone on any device with a JVM.

Security Restrictions:

  • Applet: Restricted access (e.g., can’t access local filesystem) for security reasons.
  • Application: Full access to system resources.

Life Cycle Methods:

  • Applet: Has specific methods like init(), start(), stop(), and destroy().
  • Application: Mainly relies on the main() method.

User Interface:

  • Applet: Generally part of a web page; uses AWT or Swing for GUI.
  • Application: Can have GUI or command-line interfaces.

Difference Between Applet and Application in Java

Let us differentiate Java Applet and Java Application

ParametersJava AppletJava Application
DefinitionA Java Applet is a program that runs on a Java-supported web browser. Java applets are used to implement in HTMLs.A Java Application is a Java program that runs on the system with the help of JVM.
main() methodFor executing a Java applet, we do not need a main() method, instead for Java applets we have methods such as init(), start(), and paint() to initiate the program.For executing a Java application we require a main() method that calls other methods or it has other instructions.
CompilationJava applets are compiled with the help of Java Compiler and executed with the help of a Java-supported web browser.Java applications are compiled and executed with the help of the Java compiler (Java virtual machine).
File AccessA Java applet has no access to the local files of the system or the network.A Java application has full access to the local files of the system and the network.
RunFor executing a Java applet, we do not need to save it in the file system.For the execution of Java Application, it needs to be saved in the file system of the computer.
ExecutionIt just needs a Java-supported web browser to execute.It needs JRE (Java Runtime Environment) to execute.
Read or Write OperationsIt can not read or write local files of the system.It can read and write local files on the system.
SecurityJava applets are not trusted by the system, and therefore, they experience security boundations.Java applications are trusted by the system and therefore they can access the data of the system without any interference.
ProgrammingWe can make Java applets which can display messages.We can make Java applications that can add 2 numbers.
ConnectionJava Applets cannot establish server connections.Java Applications can establish connection with other servers and pass and receive data.
InstallationJava Applets need no pre-installation.To run java programs/applications, JDK or at least JRE needs to be installed.
Access LevelApplets don’t have local disk and network access.Java applications can access all kinds of resources available on the system.

Explore Scaler Topics Java Tutorial and enhance your Java skills with Reading Tracks and Challenges.

Conclusion

  • Java applications are programs that run on a system without any web browser.
  • Java Applets are the programs that need a Java-supported web browser for their execution.
  • Java applets are implemented in HTML.
  • Java applications need main() method for its execution and Java applets need init(), start() or paint() methods.
  • Java applications are trusted by the system and thus they have storage access to the system, while Java applets do not have storage access as they are not trusted programs.
  • Java applications need to be saved on the system to execute, and Java applets do not need to be saved on the system for execution.