Java Application and Applet differences

Source: Internet
Author: User
Tags class definition requires

Which of the following is not a different point between Java application and applets?


A, applet no longer requires the main () method


B, Java applets have web browser execution


C, different ways of executing


D, applets are composed of several classes


answer is: D


used to learn Java when I heard of this thing, just know that this thing is not in use, but this thing is what exactly is not know, today to see such a problem, I began to study this applet. Here is the information collected on the Internet:


===================================================


1, applets are generally used in B/S page as a plug-in type of development, and application is mainly the development of desktop applications, application can not be loaded with JSP

The difference between
2, application, and Java applets. The Java language is a semi-compiled and partially interpreted language. Java user programs fall into two categories: Java application and Java applets. The two types of procedures in the composition of the structure and implementation mechanisms have a certain difference, mainly reflected in the following aspects:


(1) is different in operation mode.

The
Java application is a complete program that can be run independently, Java applets cannot run alone, and must be embedded in a Web page written in HTML, controlled by a Java-compliant browser.


(2) running tools are different.

After the
Java application program is compiled, a normal Java interpreter can be used to interpret the edges, and the Java applet must be performed through a Web browser or applet viewer.


(3) program structure is different.


Each Java application program must contain one and only one main method, which, when the program executes, first looks for the main method and starts running as the entry point. The class that contains the main method is often referred to as the main class, which means that the Java application program contains a main class. The applet program does not have the main class with the Main method, which is why the applet can not 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) are subject to different restrictions.


Java application programs can be designed to perform a variety of operations, including read/write file operations, but the Java Applet to the site's disk files can not be read or write operations. However, due to the introduction of applets, make Web pages have dynamic multimedia effects and interactivity, making Web pages that are written in hypertext, literally text-rich HTML language truly hyper-functional, not only to display textual information, but also to have various picture effects and dynamic graphic effects, So that the page looks lively and beautiful; In addition, applets enable Web pages to add functionality such as buttons, which increases interactivity.


====================================


look at one more:


Use Notepad to write a well-known Hellowww.java (this is a application program) 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, you first declare a new class with a reserved word class named Hellowww, which is a public class. The entire class definition is surrounded by braces {}. A main () method is defined in the class, where public indicates access, indicating that all classes can use this method; static indicates that the method is a class method that can be called directly through the class name, and void indicates that the main () method does not return any values.


for an application, the main () method is required and must be defined in the format shown above. The Jave interpreter executes the program with main () as the portal without generating any instances. You can define multiple classes in a Jave program, but you can define multiple methods in each class, but there can be only one public class, and the main () method can only have one, as the entry for the program. In the main () method definition, stringargs[] in parentheses () is a parameter passed to the main () method, with the parameter named args, which is an instance of class string, with arguments of 0 or more, each with a "class name parameter name", separated by commas. In the implementation of the Main () method (curly braces), there is only one statement: System.out.println ("Hello Wide Word web!") It is used to implement the output of a string that implements the same functionality as a printf statement in C and a cout<< statement in C + +. In addition, the//post content is commented.


Now we can run the program. Note In Hellowww.java files, the file name should be the same as the class name because the Java interpreter requires that the public class must be placed in a file with the same name as it. You can compile with Oldjavac and then use Oldjava to view the results as follows:


now assume that your JDK installation path is: c:jdk1.3.1, and Hellowww.java exists C:javademohellowww.java, now compile Hellowww.java for this assumption. Now you can implement the compilation and viewing results with the following methods.


This is finished, you can look at the following, use the DOS command to enter: C:>oldjava hellowww carriage return, what you see, the screen is not showing Hello Wide Word web!, hurry up and try it.


now look at another small example Hello.java (this is an applet program):


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 your program, you first enter all the packages under Java.awr and java.applet with the import statement, so that the program may use the classes defined in these packages, similar to the #include statements in C. Then declare a public class Hello, using extends to indicate that it is a subclass of the applet. In the class, we rewrite the paint () method of the parent applet, where parameter g is the graphics class, which indicates the context of the current painting. In the Paint () method, the method called G is drawstring (), and the string "Hello Wide Word Web" is output at coordinates (10,100), where the coordinates are represented by pixel points.


This program does not implement the main () method, which is one of the differences between applets (Hello.java) and application Application (Hellowww.java). Now to run the program:


C:>path=c:jdk1.3.1bin


C:>CD C:javademo


C:>javac Hello.java


Get byte code file hello.class. Since there is no main () method in the applet as the entrance to the Java interpreter, we have to write an HTML file, embed the applet in it, run it with Appletviewer, or run it on a Java-enabled browser. This HTML file example has (hello.htm) and is no longer elaborated. The description is to use the &lt;applet&gt; tag to start the Hello,code indicates the file where the byte code is located, width and height indicate the size of the applet.


now use Appletviewer to see the results: Enter C:>appletviewer hello.htm


a window pops up, showing Hello Wide Word web!, showing the results as shown in Figure hello.jpg:


as you can see from the example above, Java programs are made up of classes, and for an application, you must have a class that defines the main () method, and for an applet, it must be a subclass of the applet. In the definition of a class, you should include the declaration of the class variable and the implementation of the method in the class. Java is essentially 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.