Use Java to implement Hello World

Source: Internet
Author: User

Java program compilation and running
The Compilation Program of Java program is javac.exe, which is responsible for interpreting the compiled bytecode.
We only need to clarify the question. compilation is to compile the source Java file using the Java program. The runtime is to run a class (. class file) using the Java tool (java.exe on Windows platform), but the extension of the class file can be omitted.
Note: The Class file is the executable code of the Java program, called the Class file. Javac compiles Java files into bytecode, that is, Class files.
The usage of Javac is as follows:
Javac-g-O-debug-depend-nowarn-verbose-classpath path-nowrite-d dir
-D directory indicates the root directory of the class hierarchy. After javac-d DIR yourfile. java is compiled, the generated. class file can be stored in the DIR directory.
-Classpath path defines the path of the Javac search class. It will overwrite the settings of the default CLASSPATH environment variable.
For detailed parameter settings, you can use javac-help to view help. Generally, you only need to use the following command to compile and run the Java program.
Compile: javac filename. java
Run: java yourClass

Programming drills
First, write a program, and it will be very fulfilling when it runs. Therefore, many programming language-based books will use a simple program to guide readers. This is indeed a good idea, with a successful experience, I believe that the next learning will be hard for you.
Let's take a look at two simple Java programs:

Example 1: Output Hello world in the console (commonly known as the DOS window) to bring you the first cup of coffee
// Filename helloworldapp. Java
Import java. Io. // introduce the Java package
Public class helloworldapp // defines the class name
Public static void Main string ARGs // main method. The program starts to execute
System. Out. println "Hello this is a simply test" // output string, enclosed by quotation marks
// End the program
How to compile and run a program? We introduced JDK in the previous issue. Now, after installing JDK, we first save the above Code to a text file helloworldapp. java.
Then run javac helloworldapp. Java in the current directory.
It may take several seconds for you to complete the compilation. Now let's see if there is another helloworldapp. Class in the directory.
OK, run Java helloworldapp. Now you will see the output result of the program: Hello ?? This is a simply test!
Tip: If "Exception in thread" main "java. lang. noClassDefFoundError HelloWorldApp "error, you need to set the path of your Classpath (you should set the environment variable first) in Helloworldapp. run "set classpath =. "command to set classpath to the current directory. Then, you can run java test without errors. Alternatively, see the environment configuration section of the previous phase.
Now the program is running. Now, I will tell you that the part starting with "//" is the comment of the Java program. Each statement ends with a semicolon ......

Example 2: compile a simple window Program
Maybe you are not interested in such programs that can only be run in the console! Well, let's look at a Hello World program that can be run in Windows.
/File Name: test. java/
/
The first program.
@ Author warton
@ Version 1.0 2004-01-01
/
Import javax. swing. JOptionPane // you need to call the JOptionPane class
Public class test // define the class name
Public static void main String args // start the main method
// Display a message box
JOptionPane. showMessageDialognull "Hello ?? This is a basic Java program ″

System. Exit 0 // program ended
// End Method
// End class
Compile and run the program.

Drill code analysis
Now let's analyze the structure of the above two classic Hello World programs, so that you can write more classic programs than Hello world.
Here, import is the introduction of Java packages, such as the statement import java. Io. Introduce the Basic Java Io package and end with a semicolon.
The IO package contains various input/output stream operations, just like the # include "stdio. H" or # include "iostream" operations in C/C ++. Different from C ++, Java programs start with a class: public class test defines a public type test class. Class execution starts from the main () method, similar to the main () function in C/C ++.
Public class test defines a class. The class is of the public type and the class name is test. Note that the main class name in Java should be the same as the name of the Java file to be saved. That is to say, if the class name defined here is test, the file should be saved as test. Java (in this way, we will use the javac test. Java command for compiling ?? Run this class using the Java test command ).
The main () method contains the string ARGs parameter, which is used to transmit parameters from the command line to the application. We can use ARGs 0 ARGs 1... ARGs n to access these parameters. For example, system. Out. println ARGs 0 is used to output the first command line parameter, which is similar to the C language.
Import javax. Swing. joptionpane introduces the joptionpane class in the Java interface design package swing. The pane. showmessagedialog method displays a message box.
System. Exit 0 means to exit the program.
// The previous part is the code annotation, And the @ mark between the two is the javadoc mark (this will be explained in detail in subsequent chapters, however, you can run "javadoc-D Doc-author-version test. java ).

Enhanced understanding
I believe many of my friends have learned the C/C ++ language at school! To give you a better understanding of the first routine, we first use a C language program. If you do not have the C/C ++ language basics, skip this step.
# Include "stdio. H" // introduce the header file
Main // C ++ main function, which is equivalent to the main method of Java (in Java, it is a method of the class, called method)
Printf "this is a simply Test
"// Output string
// End
Maybe your programming started from the C ++ language and never touched on the pure C language. It is very simple. We will describe this program in the pure C ++ language:
# Include "iostream. H" // written in accordance with the c ++ language standard # include "iostream ″
Void main void // C ++ program starts execution
Cout <"this is a simply test ?? "<

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.