Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Applet versus Application
Posted on: March 14, 2008 at 12:00 AM
Applets as previously described, are the small programs while applications are larger programs.
Applets don't have the main method while in an application execution starts with the main
method. Applets can run in our browser's window or in an appletviewer
Applet versus Application
Applets as previously described, are the small programs while applications are larger programs. Applets
don't have the main method while in an application execution starts with the main method. Applets can
run in our browser's window or in an appletviewer. To run the applet in an appletviewer will be an
advantage for debugging. Applets are designed for the client site programming purpose while the
applications don't have such type of criteria.
Applet are the powerful tools because it covers half of the java language picture. Java applets are the
best way of creating the programs in java. There are a less number of java programmers that have the
hands on experience on java applications. This is not the deficiency of java applications but the global
utilization of internet. It doesn't mean that the java applications don't have the place. Both (Applets and
the java applications) have the same importance at their own places. Applications are also the platform
independent as well as byte oriented just like the applets.
Applets are designed just for handling the client site problems. while the java applications are designed to
work with the client as well as server. Applications are designed to exists in a secure area. while the
applets are typically used.
Applications and applets have much of the similarity such as both have most of the same features and
share the same resources. Applets are created by extending the java.applet.Applet class while the java
applications start execution from the main method. Applications are not too small to embed into a html
page so that the user can view the application in your browser. On the other hand applet have the
accessibility criteria of the resources. The key feature is that while they have so many differences but
both can perform the same purpose.
Review of Java Applets: You have previously learned about the java applets. To create an applet just
create a class that extends the java.applet.Applet class and inherit all the features available in the parent
class. The following programs make all the things clear.
import java.awt.*;
import java.applet.*;
class Myclass extends Applet {
public void init() {
/* All the variables, methods and images initialize here
will be called only once because this method is called only
once when the applet is first initializes */
}
public void start() {
/* The components needed to be initialize more than once
in your applet are written here or if the reader
switches back and forth in the applets. This method
can be called more than once.*/
}
public void stop() {
/* This method is the counterpart to start(). The code,
used to stop the execution is written here*/
}
public void destroy() {
/* This method contains the code that result in to release
the resources to the applet before it is
finished. This method is called only once. */
}
public void paint(Graphics g) {
/* Write the code in this method to draw, write, or color
things on the applet pane are */
}
}
In the above applet you have seen that there are five methods. In which two ( init() and destroy ) are
called only once while remaining three (start() , stop() , and paint() ) can be called any number of times as
per the requirements. The major difference between the two (applet and application) is that java
applications are designed to work under the homogenous and more secure areas. On contrary to that,
java applets are designed to run the heterogeneous and probably unsecured environment. Internet has
imposed several restrictions on it.
Applets are not capable of reading and writing the user's file system. This means that the applet neither
can access nor place anything locally. To illustrate this lets take an example.. Many Window based C
applications uses the .INF file as the initialization file to store the information about the application and
any user preferences in 16-bit Windows or the Registry in 32-bit Windows. While in case of current applet
it is not possible.
One more thing to point here is that applets are unable to use the native methods, run any program on
the user system or load shared libraries. The major security concern here is that the local shared libraries
and the native methods may results in the loophole in the java security model.
Applets are not capable of communicating the server than one from which they are originating. There are
the cases in which an encryption key is used for the verification purpose for a particular applet to a server.
But accessing a remote server is not possible.
The conclusion is that the java applets provides a wide variety of formats for program execution and a
very tight security model on the open environment as on the Internet.
Introduction to Java Application : Java applications have the majority of differences with the java
applets. If we talk at the source code level, then we don't extend any class of the standard java library that
means we are not restricted to use the already defined method or to override them for the execution of
the program. Instead we make set of classes that contains the various parts of the program and attach
the main method with these classes for the execution of the code written in these classes. The following
program illustrate the structure of the java application.
public class MyClass {
/* Various methods and variable used by the class
MyClass are written here */
class myClass {
/* This contains the body of the class myClass */
}
public static void main(String args[]) {
/* The application starts it's actual execution
from this place. **/
}
}
The main method here is nothing but the system method used to invoke the application. The code that
results an action should locate in the main method. Therefore this method is more than the other method
in any java application. If we don't specify the main method in our application, then on running the
application will through an exception like this one:
In the class MyClass: void main(String args[]) is undefined
But at higher level major concern is that in a typical java application security model, an application can
access the user's file system and can use native methods. On properly configuring the user's environment
and the java application it will allow access to all kind of stuff from the Internet.
In most of the cases it is seen that the java application seems like a typical C/C++ application. Now we
are going to create plenty of applications to exemplify some of the methods and features of a specific
Java application. All of them are console based Java applications because here we are not going to cover
the AWT.
Java Applications : An Example
Lets create an application that executes at the command prompt. Lets create a new file named
ClassA.java.
public class ClassA{
//write the variables for Class
String Name;
int AccNumber;
float Bal;
//This method display the information on the screen.
void display(){
System.out.println("Name: " + Name);
System.out.println("Account Number: " + AccNumber);
System.out.println("Balance: " + Bal);
}
public static void main(String args[]) {
//Create an instance of ClassA
ClassA a = new ClassA();
//Assigning values to the variables in class ClassA
a.Name = "Vinod";
a.AccNumber = 467256282;
a.Bal =635;
//Draw the top border
for (int i = 0; i < 20; i++)
System.out.print("--");
//Title
System.out.println(" PARTICULARS");
//Call method to display the information
a.display();
//Draw bottom border
for (int i = 0; i < 20; i++)
System.out.print("--");
//Ending remark
System.out.println("End of display");
}
}
If the file ClassA.java and the javac.exe are in the same directory then compile the program just by giving
the following command.
javac ClassA.java
If the file ClassA.java and javac.exe are not in same directory the set the path of java \bin directory in the
environment variable and include the directory contained the file ClassA.java in the command prompt
then apply the above command.
After compiling the program, just apply the following command.
java ClassA
This will result in the following output.
---------------------------------------- PARTICULARS
Name: Vinod
Account Number: 467256282
Balance: 635.0
----------------------------------------End of display
The above example ClassA.java uses the three variables Name, AccNumber, and Bal and a display
method to display the values of the variables. Everything is all right in the above example. Here is a
closer look about the line System.out.println(). System is a class which is kept in java.lang package, out is
an object of System class that is used to print the message on the standard output and println() is the
method of the System class.
Note the points given below:
The file ClassA.java makes the .class file after compilation.
There is no need of specifying the extension when interpreting the file.
While distributing the file just provide the compiled file (.class file) and the interpreter.
System class contains the following variables and methods.
Variables of the System class
Variables
Utilization
public static PrintStream in
It is used to read the data from the standard input stream
public static PrintStream out It is used to write the data on the standard output stream
public static PrintStream err It is used to print the error message on the standard output stream.
Methods defined in the System class
Methods
Utilization
getProperties()
It returns a Properties class with the system properties.
getProperty (String key, String default)
Returns a String with a value for the specified property.
Or, returns the default if the specified property is not set.
setProperties (Properties props)
Sets the system properties based on the specified
properties.
gc()
Manually invokes the garbage collector. Note that unless
garbage collection is manually enabled or disabled, it is
done automatically by the Java virtual machine.
exit(int status)
Exits the application with the specified status code (0 if
successful).
currentTimeMillis()
Returns a long that holds the value in milliseconds since
January 1, 1970.
arraycopy (Object src, int src
Position, Object dst,
dstPosition, int len)
Copies an array.
runFinalization ()
Runs the finalization methods of any object pending
finalization.
Importing Other packages to your Java Application: Lets create a simple application that displays the
date. In this application you will see how to import packages in your application. Java libraries provide a
built in method currentTimeMillis(). This method returns the number of seconds since January 1970
representing in 64-bit integer long format.
public class PrintDate {
public static void main(String args[]) {
//Draw the upper border
for (int i = 0; i < 40; i++)
System.out.print("--");
//Display the timeT
System.out.println("Time in milliseconds since January 1, 1970:
" + System.currentTimeMillis());
//Draw the lower border
for (int i = 0; i < 40; i++)
System.out.print("--");
}
}
Compile the above program by giving the following command.
javac PrintDate.java
By giving the following command you will see the following output.
java PrintDate
Here is the output of the above program:
-------------------------------------------------------------------Time in milliseconds since January 1, 1970: 1181398838332
--------------------------------------------------------------------
To avoid the overflow and inaccuracy here we took integer data type as long. It is a 64-bit signed
integer and will contain the values up to the year 200,000,000 accurately. So we should not worry right
now because this problem will take a long time to occur.
Now come to the point: Suppose the user wanted today's date in your application then no need to worry
because java provides the built in class Date in the package java.util that provides this functionality. Since
java.util is not a default package so we have to import it explicitly to use the functionality of the class
Date. You will be known about the syntax of importing the package in your application. There is no
difference of importing the package in both Java Application and the Java Applet. But don't worry i will
provide you the code of importing the package in your application or applet. Here is the code of importing
the package.
import java.util.Date;
Write this code in the beginning of your application, then your application can access to all the non-private
member of Date class.
Now I would like to give one more example of the java application that will have access the private
members of the Date class.
import java.util.Date;
public class PrintDate2 {
Date todayDate = new Date();
public static void main(String args[]){
//Draw the upper border
for (int i = 0; i < 40; i++)
System.out.print("--");
//Instantiate the class PrintDate2
PrintDate2 d = new PrintDate2();
//Display the Date
System.out.println("Today's Date: " + d.todayDate);
//Draw the lower border
for (int i = 0; i < 40; i++)
System.out.print("--");
}
}
Compile and run the above application. On running the above application displays the current date and
also shows the upper and lower border.
Here is the output of the above program:
-----------------------------------------------------Today's Date: Sat Jun 09 16:31:51 BST 2007
------------------------------------------------------
Using args[] to pass Command Line Arguments: Any application can have one more attribute that is
they can receive the command line argument pass to it. Let us consider the case of an application named
ClassA to which we have to pass the arguments while running the application then what have to be done.
In this case we pass the argument by using the command line argument technique.
public class CommandLine {
public static void main(String args[]){
//Draw the upper border
for (int i = 0; i < 40; i++)
System.out.print("--");
//Check to see if no argument was passed
if (args.length == 0){
System.out.println("Enter the argument ");
}
// Loop to display the argument passed to the command line
for (int i = 0; i < args.length; i++)
System.out.println(" " + args[i]);
//Draw the bottom border
for (int i = 0; i < 40; i++)
System.out.print("--");
}
}
Here is the code used to pass the arguments by using the command line argument technique.
java CommandLine This is my first program
C:\Upload>java CommandLine This is my
first program
--------------------------------------------------------This
is
my
first
program
---------------------------------------------------------
What happens when we pass the arguments within the double quotes.
java CommandLine "This is my first program"
C:\Upload>java CommandLine "This is my
first program"
----------------------------------------------------------This is my first program
-----------------------------------------------------------
So the conclusion is that if we pass the argument on the command line by using the first technique then
the arguments are stored like this.
args[0]=This
args[1]=is
args[0]=my
args[0]=first
args[0]=program
While we pass the argument on the command line by using the second technique then the arguments are
stored like this
args[0]=This is my first program. To more clearly understand see the third technique:
javac CommandLine This is "my first program"
C:\Upload>java CommandLine This is "my
first program"
---------------------------------------------------------This
is
my first program
----------------------------------------------------------
The third output clears that the arguments in the above output are stored like this:
args[0]=This
args[1]=is
args[0]=my first program
Summary: In this chapter you studied about the differences and the similarities between the java applets
and application. and what is the roll of java application and the java applet in java programming. Java
applications are flexible in linking with the java native code and security than applets. So the overall
conclusion is that both java applications and java applets have the same priority but at their own places. If
java application is more flexible at one place then it has some drawback at other place where its
counterpart java applet provides the more flexibility