Implement Hello World with Java

xiaoxiao2021-03-06  41

The compiler of the Java program and the compiler of the Java program are JavaC.exe, which is responsible for explaining the execution of the selected bytecode. We only need to understand a problem, compile the Javac program to compile the source Java file, run with Java toolbox (in the Windows platform is java.exe) to run a class (.class file), but extension of class files Name can be omitted. Note: The class file is the executable code of the Java program, called the class file. Javac compiles the Java file into a zona code, which is the Class file. Javac is used as follows: Javac -g-o-debug-dependend-noarn-verbose-classpath path-norite-d Dir -d Directory indicates the root directory of the class level. After compile with Javac -D Dir YourFile.java, you can store the production .class file in the DIR directory. -Classpath path Defines the path to the Javac search class. It will cover the setting of the default ClassPath environment variable. For detailed parameter settings, you can view help with Javac -help. Under normal circumstances, you only need to use the following command to compile and run the Java program. Compilation: Javac FileName.java Execution: Java Yourclass

The programming drill moves forward to write a program, which will be very accomplished when running, so many books in the programming language will guide the reader in a simple program. This is indeed a good idea, with a successful experience, I believe that the next learning is difficult to fall. Let's take a look at two simple Java programs:

Example 1: Console (commonly known as DOS window) Output Hello World, let you drink the first cup of coffee // filename helloworldapp.java import java.IO.// introduced Java package public class helloworldApp // Define class public static void Main string args // Main method, the program starts to execute System.out.Println "Hello this is a simply test" // output string, character string is included in the quotation // how to compile, run a program? The last phase we introduced JDK. Now we have a good JDK, save the above code to a text file helloWorldapp.java. Then run Javac HelloWorldapp.java in the current directory may you need to wait a few seconds to compile completion. Now, don't have a helloworldapp.class ok in the directory, run Java HelloWorldApp, now you will see the output of the program: Hello ?? this is a simply test! Tip: If "Exception in Thread" main "Java.lang.noclassdeffounderror helloworldapp" error, then set your ClassPath path (you should set a good environment variable first), run the "SET ClassPath =." Command in the current directory of HelloWorldapp.class to set the classpath for the current table of Contents. Then run Java Test will not have something wrong. Or please refer to the previous period. The program is now running. Then I now tell you that the part started with "//" is a java program comment, each statement ends with a semicolon ... Example 2: Write a simple window program, you can only in the console The running program is not interested! Well, let's take a look at the Hello World program that can run under Windows. / File name: Test.java/ / The first program. @Author Warton @version 1.0 2004-01-01 / Import javax.swing.joptionpane // Need to call JOTIONPANE class public class test // Define Camera PUBLIC Static Void Main String Args // Start MAIN Method / / Display a message box JOPANE.SHOWMESSAGEDIALOGNULL "HELLO ?? this is a Basic Java Program"

System.exit 0 // The program end // ends Method // End Class to compile and run the program.

Exercise Code Analysis Now let's analyze the structure of two classic Hello World programs so that you will give you a more classic program than Hello World. Where import is introduced into the Java package, such as statement import java.io.i, introducing a Java foundation IO package, and ends with a semicolon. The IO package package contains various input and output flow operations, just like the #include "stdio.h" or #include "iostream" in the C / C language. Different from the C language program with a Class Start: Public Class Test defines a PUBLIC type TEST class, the execution of the class starts from the main () method, and the main () function in the C / C language is similar. Public class test defines a class, the class is a public public type, and the class name is TEST. Note that the primary class name of the Java should be the same as the Java file name you want to save. That is, the class name defined here is Test, then the file should be saved as Test.java (so, we compile with the javac test.java command to run this class with the java test command). The main () method has parameters String ARGS for transferring parameters from the command line to the application. We can use ARGS 0 Args 1 ... Args n to access these parameters. For example, use System.out.Println Args 0 to output the first command line parameters, this is similar to the C language. Import Javax.swing.joptionPane introduced Java's interface design package Swing JOPANE class. The Pane.ShowMessageDialog method shows a message box. System.exit 0 is an exit program. // The previous part is the code annotation, // The @ 开 开 标 标 is the Javadoc tag (this will explain in detail in the subsequent chapter, but now you can run "javadoc -d doc -author -version test.java" test Try, you will see a DOC folder in the current directory, there is a beautiful HTML document generated by Javadoc). Enhancement, I believe that many friends have learned C / C language in schools! In order to let you understand the first routine more deeply, we will use a C language program to explain. Of course, if you don't have a C / C language basis, you can skip. #include "stdio.h" // introduced the main file main // c main function, equivalent to the Java's main method (in Java, a method of class, called Method) Printf "this is a simply test" / / Output string // End Perhaps your program started from the C language, it is not exposed to the pure C language, so simple, we use pure C language to describe this program: #include "iostream.h" / / Press C language standard to write in #include "iostream" void main void // c program to start executing COUT << "this is a simply test ?? <

转载请注明原文地址:https://www.9cbs.com/read-77277.html

New Post(0)