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.
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.
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.
If the file ClassA.java and the javac.exe are in the same directory then compile the program just by giving the following command.
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.
This will result in the following output.
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:
System class contains the following variables and methods.
Variables of the System class
Methods defined in the System class
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.
Compile the above program by giving the following command.
By giving the following command you will see the following output.
Here is the output of the above program:
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.
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.
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:
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.
Here is the code used to pass the arguments by using the command line argument technique.
java CommandLine This is my first program
What happens when we pass the arguments within the double quotes.
java CommandLine "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
While we pass the argument on the command line by using
the second technique then the arguments are stored like this
javac CommandLine This is "my first program"
The third output clears that the arguments in the above output are stored like this:
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.