1st simple java programs-general Linux technology-Linux programming and kernel information. The following is a detailed description. Now that Java object-oriented basics have been discussed, let's look at some practical Java programs. Let's start by compiling and running the following short sample program. You will see that this program has more functions than you think.
/*
This is a simple Java program.
Call this file "Example. java ".
*/
Class Example {
// Your program begins with a call to main (). public static void main (String args []) {System. out. println ("This is a simple Java program. ");}}
Note: In the following introduction, we will use the standard JDK (Java Developer's Kit, Java Development Kit), which can be obtained from Sun Microsystems. If you are using other Java development environments, the Java program compilation and running processes may be different. In this case, please refer to the compiler user manual for specific guidance.
1. Enter the program
For most computer languages, file names containing program source code are arbitrary, but not for Java. The first thing you need to know about Java is that the source file name is very important. For this Example, the source program file name should be Example. java. The following describes the causes.
In Java, a source program file is called a compilation unit ). It is a text file that contains one or more class definitions. The Java compiler requires that the source program file use the. java file extension. Note that the file extension length is 4 characters. Therefore, your operating system must support long file names. This means that DOS and Windows 3.1 do not support Java (at least currently ). Of course, it works normally in Windows 95/98 and Windows NT/2000.
From the above Example, we can see that the class name defined in the program is also Example. This is no coincidence. In Java, all code must reside in the class. According to the Conventions, the class name must be the same as the file name of the source program. You should also ensure that the file name is case-sensitive and the class name is the same, because Java is case-sensitive. Although the conventions with the same file name and class name seem a bit authoritarian, they help you easily maintain and organize programs.
2. Compile the program
To compile the sample program Example, you must run the compiler program javac and specify the source program file name on the command line. The format is as follows:
C: \> javac Example. java
The javac compiler generates a file named Example. class, which contains the bytecode of the program. As discussed earlier, the Java bytecode contains the instruction code to be executed by the Java interpreter. Therefore, javac output is not a code that can be directly run.
To really run this program, you must use a java interpreter named Java. The specific method is to use the class name Example as a command line parameter input, in the following format:
C: \> java Example
Run this program and output the following content:
This is a simple Java program.
After the Java source code is compiled, each individual class is put into its own output file, and the file name is added with the class Name ". class" extension. This is why the Java source program file must have the same name as the class contained in it-the source program file will be the same as the ". class" file. Running the Java interpreter actually specifies the name of the class you want the interpreter to run. It will automatically search for files containing the name and with the. class extension. If found, it runs the Code contained in the specified class.
2.2.3 detailed discussion of 1st sample programs
Although Example. java is short, it includes several key features of all Java programs. Let's carefully analyze each part of the program.
The program starts with the following lines:
/* This is a simple Java program.
Call this file "Example. java ".
*/
This is a comment ). Like most other programming languages, Java allows you to add comments to the source program file. The content in the comment will be ignored by the compiler. In fact, annotations are used to explain or explain the operations of a program to anyone who reads the source code program. In this Example, the program is described and the source program name is Example. java. Of course, in real applications, annotations are usually used to explain how certain parts of a program work or some special functions.
Java supports three types of annotations. The comments at the top of the sample program are called multiline comments ). This type of annotation starts with "/*" and ends with "*/". Any content between the two annotators will be ignored by the compiler. As shown in the "multi-line comment" name, a multi-line comment can contain several lines of text.
The code for the next line of the program is as follows:
Class Example {
This row uses the keyword class to declare a new class. Example is the class name identifier. The entire class definition (including all its members) will be located between a pair of curly braces, brackets are used in the same way as C or C ++ in Java. Currently, you do not have to consider the details of classes, but note that in Java, all program activities occur in the class, this is why Java programs are object-oriented.
The following line of program is a single line comment:
// Your program begins with a call to main ().
This is the second type of annotation supported by Java. A single-line comment starts with "//" and ends at the end of the line. Generally, programmers use multi-line comments for long comments, while single-line comments for short and one line.
The code for the next line is as follows:
Public static void main (String args []) {
This row starts with the main () method. As mentioned above, this is the first line of execution that the program will start to execute. All Java applications start to execute by calling main () (this is the same as C/C ++). We cannot make a precise explanation of each part of the row here, this requires a detailed understanding of the characteristics of Java encapsulation, but since most examples in part 1 of this book use this line of code, we will give a brief introduction to each part.
The public keyword is an access specifier that allows programmers to control the visibility of class members. If a class member has a public name before it, it indicates that the member can be accessed by code outside of the declared class (private is opposite to public, it prohibits members from being accessed by code outside the class ). In this example, main () must be defined as public, because it needs to be called by code other than its class when the program starts execution. The keyword static allows you to call main () without first implementing a special instance of this class. This is necessary because the Java interpreter calls main () before any object is created (). The keyword void only notifies the compiler that main () does not return any value. You will see that the method can also return values. If it seems confusing, don't worry. All these concepts will be discussed in detail in subsequent chapters.
As mentioned earlier, main () is the method called at the beginning of a Java program. Remember that Java is case sensitive. Therefore, main and Main are different. The Java compiler can also compile classes that do not contain the main () method, but the Java interpreter cannot run these classes. Therefore, if you enter Main instead of main, the compiler will still compile your program, but the Java interpreter will report an error because it cannot find the main () method.
All information you want to pass to the method is received by the variable specified in the brackets behind the method name. These variables are called parameters ). Even if a method does not require a parameter, you still need to place an empty pair of parentheses after the method name. In main (), there is only one parameter, that is, String args []. It declares a parameter called args, which is an instance array of the String class (note: array is a set of simple objects ). String type object stores character strings. In this example, args receives any command line parameters that are displayed when the program is running. This program in this example does not use this information, but other programs mentioned later in this book will use them.
The last character of this row is "{". It indicates the beginning of the main () program body. All code contained in a method will be included in the brackets.
In addition, main () is only the place where the interpreter starts to work. A complex program may contain dozens of classes, but these classes only require a main () method for the interpreter to start working. When you start to reference a Java small application embedded in the browser, you do not need to use the main () method at all, because the Web browser uses another different method to start the small application.
The following code is displayed. Note that it appears in main.
System. out. println ("This is a simple Java program .");
The line outputs the string "This is a simple Java program." on the screen, and the output result is followed by a blank line. The output is actually implemented by the built-in method println. In this example, println () shows the string passed to it. You will see that the println () method can also be used to display information of other types. This line of code started with System. out. It is too early to describe it in detail and involves a lot of complicated content. Simply put, System is a predefined class that can access the System, and out is the output stream connected to the console.
As you may have guessed, console output (input) is not frequently used in actual Java and small applications. Because the vast majority of modern computing environments are essentially window and graphical interfaces, console I/O is mainly used by simple utility programs and demonstration programs. Later in this book, you will learn other methods for generating output using Java. However, we will continue to use the console I/O method.
Note that the println () statement ends with a semicolon. In Java, all statements end with a semicolon. Other lines of the program do not end with a semicolon, because technically they are not program statements.
The first "}" in the program ends main (), and the last "}" End Class Example definition.
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