Differences between Java application and applet

Source: Internet
Author: User

Which of the following is NOT the difference between Java application and applet?
A. The main () method is no longer required in the applet.
B. Java applet has Web browser for execution
C. Different execution methods
D. the applet consists of several classes.
The answer is: D.
I have heard of this thing before when I was learning java. I just know that this thing is not used, but I don't know what it is. Today I see such a problem, I started to study this applet. The following information is collected on the Internet:
========================================================== ==============
1. applet is generally used for plug-in-type development on B/S pages, while application is mainly used for development of desktop applications, and application cannot be loaded using Jsp.
2. Differences between Application and Java Applet. Java is a semi-compiled and semi-interpreted language. Java User Programs are divided into two types: Java Application and Java Applet. These two types of programs have some differences in the composition structure and execution mechanism, mainly reflected in the following aspects:
(1) The running mode is different.
Java Application is a complete program that can run independently. Java Applet cannot run independently. It must be embedded into a Web page written in HTML, control execution through a Java-compatible browser.
(2) Different running tools.
After the Java Application program is compiled, it can be interpreted and executed with a common Java interpreter. Java Applet can only be executed through a Web browser or Applet observer.
(3) The program structure is different.
Each Java Application must contain one and only one main method. When executing a program, you must first find the main method and run it as the entry point. The class that contains the main method is often called the main class, that is, the Java Application program contains a main class. The Applet does not have the main class containing the main method, which is exactly why the Applet cannot run independently. Although the Applet does not have a main class containing the main method, the Applet must have a class derived from java. applet. Applet, which is provided by the Java system.
(4) The limitations are different.
Java Application programs can be designed to perform various operations, including file read/write operations. However, Java Applet neither reads nor writes the disk files on the site. However, due to the introduction of Applet, Web pages have dynamic multimedia effects and interactive performance, this enables a Web page written in HTML language named Hypertext and plain text to have the hypertext function, which not only displays text information, in addition, it can have various image effects and dynamic graphics effects, so that the page is vivid and beautiful. In addition, the Applet adds buttons and other functions to the Web page, thus increasing interaction.
==========================================
Let's look at another one:
Use NotePad to write a program familiar with HelloWWW. java (this is an Application program). The Code is as follows:
Public class HelloWWW
{
Public static void main (String args [])
{
System. out. println ("Hello Wide Word Web! "); // Output Hello Wide Word Web!
}
}
In the HelloWWW program, a new class is first declared using the reserved word class. Its class Name Is HelloWWW, which is a public class ). The entire class definition is enclosed by braces. A main () method is defined in this class. public indicates the access permission, indicating that all classes can use this method. static indicates that this method is a class method, it can be called directly through the class name; void indicates that the main () method does not return any value.
The main () method is required for an application and must be defined in the preceding format. The Jave interpreter uses main () as the entry to execute the program without generating any instances. Multiple classes can be defined in the Jave program. Multiple methods can be defined in each class, but at most one public class can be defined, and only one main () method can be defined as the entry of the program. In the definition of main () method, Stringargs [] in parentheses () is a parameter passed to the main () method. The parameter is named args, which is an instance of the String class, there can be 0 or more parameters. Each parameter is specified by "class name parameter name". Multiple parameters are separated by commas. In the implementation of the main () method (in braces), there is only one statement: System. out. println ("Hello Wide Word Web! "); It is used to implement string output. This statement implements the same functions as the printf statement in C language and the cout <statement in C ++. In addition, the content after // is annotated.
Now we can run this program. Note: In the HelloWWW. java file, the file name should be the same as the class name, because the Java interpreter requires that the public class be placed in the file with the same name. You can use OldJavac for compilation, and then use OldJava to view the results. The method is as follows:
Now let's assume that your JDK installation path is C: JDK1.3.1, and HelloWWW. java contains C: JavaDemoHelloWWW. java. Now let's compile HelloWWW. java based on this assumption. Now you can use the following methods to compile and view the results.
After the compilation is complete, you can see that enter: C:> OldJava HelloWWW in the doscommand and press Enter. What do you see? Is the Hello Wide Word Web displayed on the screen !, Try it now.
Now let's look at another small example Hello. java (this is an Applet ):
Import java. awt .*;
Import java. applet .*;
Public class Hello extends Applet
{
Public void paint (Graphics g)
{
G. setColor (Color. red );
G. setFont (new Font ("Helvetica", Font. PLAIN, 24 ));
G. drawString ("Hello Wide Word Web", 10,100 );
}
}
This is a simple Applet (small application ). In the program, first use the import Statement to input all the packages under java. awr and java. applet, so that the program may use the classes defined in these packages, which is similar to the # include statement in C. Declare a public class Hello and use extends to specify that it is a subclass of the Applet. In the class, we override the painting () method of the parent Applet. The parameter g is the Graphics class, which indicates the context of the current painting. In the paint () method, call the g method drawString () and output the string "" Hello Wide Word Web "at the coordinate (10,100 ", the coordinates are represented by Pixel points.
This program does not implement the main () method, which is one of the differences between Applet (Hello. java) and Application (HelloWWW. java. Run the program now:
C:> Path = c: JDK1.3.1Bin
C:> cd c: JavaDemo
C:> Javac Hello. java
Get the bytecode file Hello. class. Since the Applet does not have the main () method as the entry to the Java interpreter, we must write an HTML file, embed the Applet into it, and then run it using appletviewer, or run on a browser that supports Java. This html file contains (hello.htm) and will not be elaborated. Note that the <applet> flag is used to start Hello. The code indicates the file where the bytecode is located, and the width and height indicate the size of the applet.
Now we can use Appletviewer to check the result: Enter C:> appletviewer Hello.htm
A window is displayed, showing Hello Wide Word Web !, The result hello.jpg is displayed:
From the above example, we can see that a Java program is composed of classes. For an application, there must be a class that defines the main () method. For applet, it must be a subclass of the Applet. The class definition should contain the declaration of class variables and the implementation of methods in the class. Java is basically the same as C and C ++ in terms of basic data types, operators, expressions, and control statements, but it also adds some new content.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.