2.1 Simple Java Programs

Source: Internet
Author: User

2.1.1 Types of Java programs
    • Application Program
    • Applet Program

The structure and operating environment are different.
The former is a standalone program that requires an executor (called a virtual machine) to run.
The latter is a non-independent program embedded in an HTML Web page.
Run by a dedicated appletviewer.
Or it can be run by a Web browser (called a Java Virtual machine).

Application Program
 Public class Hello {    publicstaticvoid  main (string[] args) {            System.out.println ( "Hello World");}    }

Points:

Class is the subject
public class name has the same name as the file
The notation of main () is fixed
System.out.print and println and printf

Applet Program

(now obsolete, almost no longer used!) )

import java.awt.*; import java.applet.*; import javax.swing.*;  Public class extends JApplet {     publicvoid  paint (Graphics g) {        g.drawstring ("Hello world! ", 20,20);}    }

Points: 

Import means importing
Extendsjapplet represents inheritance
? Applets or JApplet can be
Has the paint () method, which indicates how to draw
No main () method

2.1.2 Basic composition of Java programs
 Package edu.pku.tds.ch02; import java.util.*;  Public class hellodate {

Package statement (0 or 1 sentences)
Import statement (0 or more sentences)
Importing class libraries for other classes
Class definition--class (1 or more)
    A file can have only one public class (with the same name as the file)

class = Class Header + Class body
Class member = Field + method
Fields (field, attribute, variable) methods (method, function)
Method = Method Header + Method body

Compiling and running of 2.1.3 Java program
    • SOURCE program Editing

Any text editor available

    • Program compilation

Using the javac tool in the JDK

    • Program run

Using Java tools

editing, compiling and running of application
    • Program Editor: Editor-the file name should match the class name of the public class

Case sensitive

    • Program Compilation --Convert to bytecode (bytecode) file, extension. class

(The. class file contains instructions for a Java Virtual machine)
Compilation can be javac.exe using the JDK tool.
such as Javachello.java

    • program runs -the process of executing instructions in a. class file.

such as Javahello
(Note: Do not write Java Hello.class, because the class name is required, not the file name)

editing, compiling and running of applets
    • The Java applet program must be embedded in the HTML and interpreted by the WWW browser responsible for interpreting the HTML file as an interpreter to interpret the execution program.
    • The Java applet introduces the content of dynamic interaction in www.
    • 1. Edit and compile the source program.
    • 2. Embed the applet in the HTML file.

Use <applet> Tags:
<appletcode= "Helloworldapplet.class"
width=200 height=40 background=white>

Running applets with Appletviewer

Appletviewerhelloworldapplet.html

2.1.4 Several other tools
    • The main tools

Javac compiling
Java Runtime (console and graphical interface program)
JAVAW running the graphical interface program
Appletviewer Running Applet Program

    • Several other commonly used tools

    Jar Packaging Tool

(1) Compiling Javaca.java
(2) Packaging jar Cvfma.jar A.mana.class
C is created (create), V for display details (verbose), F for specified file name, m for manifest file
(3) Running Java-jar A.jar

Where A.man is the manifest file (manifest), which reads as follows:
manifest-version:1.0
Class-path:.
Main-class:a
The manifest file can be arbitrarily named, the common is to use MANIFEST.MF

    Javadoc Generating Documents

Javadoc–d Directory name Xxx.java
/** * * This can be marked with the following
@author description of the class indicates the author of the development of such a module
@version the description of the class indicates the version of the class module
@see a reference to the description of classes, properties, methods, and related topics
Description of a parameter in a method @param description of the method
Description of the method return value @return The description of the method
@exception the description of the method, describe the exceptions that the method might throw

    API documentation for Java

Online documentation
Http://docs.oracle.com/javase/8/docs/api/index.html
You can also download a document in a Web page format
Doc.zip
You can also search for documents in CHM format from the Internet
such as JDK_API_1_6_ZH_CN. Chm

    JAVAP viewing class information and disassembly

Using JAVAP to view information about a class
JAVAP class Name
Using JAVAP Disassembly
Javap–c class Name

(This page is excerpted from: https://www.icourse163.org/learn/PKU-1001941004?tid=1002731007#/learn/content?type=detail&id= 1003758024&sm=1)

2.1 Simple Java Programs

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.