Understand how Java Programs work

Source: Internet
Author: User
Tags square root

Java Programms that run locally on your computer is called applications. Programs that run on Web pages is called applets.

You can run Java applications from a command line using Java, a program that invokes the JVM. Eclipse uses this program behind the scenes when you run programs. The command can include extra intems of information, as in this example: java textdisplayer readme.txt/p. Extra information sent to a program was called an argument.

***********************************************************

1. Application example1:the full Text of Root.java

Public class Root {

Public Static void Main (string[] args) {

int Number = 225;

System. out . println ("The square root of"

+number

+"is"

+math. sqrt (number)

);

}

}

************************************************************

###############################################

************************************************************

2. Application Example2:the full Text of Root.java

Public class Protect1 {

Public Static void Main (string[] arguments) {

System. out . println ("the" + arguments[0)

+ "+ arguments[1 " + "Fox"

+"jumped over the"

+ arguments[2]

+"dog."

);

}

}

************************************************************

Note: Why do you define the main method as a static method in Java?

(1) In a class, preceded by a modifier, static is called a static variable (a class variable), preceded by a modifier static called a static method (a class method). static methods and static variables are objects that belong to a class and not to a class.
(2) A reference to a static method and a static variable is directly referenced by the class name.
For example, there is a static int x in class point and a class variable that we want to reference: point.x=89;
(3) A non-static method and a non-static member variable cannot be called in a static method. Conversely, you can. Static methods can only invoke methods of static methods or instances.
Cause: Static variables and static methods allocate memory space when the class is loaded, and when non-static methods call them, they already have memory space, of course, it can be called!
(4) A static method can be invoked with the object of the class.
We know that the main method is not part of a class in C + +, it's a global approach, so when we do, the C + + compiler can easily find the main method, but when we execute a Java program, Because Java is a class as a program of the organizational unit, when we want to execute, we do not know which class the main method will be placed in, do not know whether to produce an object of the class, in order to solve the problem of the operation of the program, we define the main method as static, so that, When we execute a Java code, we write at the command prompt: Java point is a class, and the interpreter calls the static Main method in the point class, without having to produce the object of the point class. When we load the class of point, the main method is also loaded as a portal to our Java program.

************************************************************

2. Applet example:the full Text of Rootapplet.java

Import java.awt.*;

Public class Protect1 extends javax.swing.japplet{

int Number ;

Public void init () {

Number = 225;

}

Public void Paint (Graphics screen) {

Graphics2D screen2d = (graphics2d) screen;

Screen2d.drawstring ("The square root of" +

Number +

"is" +

Math. sqrt (number), 5, +);

}

}

************************************************************

Understand how Java Programs work

Related Article

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.