Java Learning Note 11/15: A simple Java example

Source: Internet
Author: User

Let's start with a simple Java program. Take a look at the program below and try to see if you can tell what it is doing! Example: A simple example of testjava.java  //Testjava.java,java  public class Testjava {         & nbsp   public static void Main (String args[])  {                   & nbsp     int num; Declare an integer variable num                         num = 3; Assign the integer variable to 3                      //output string, where the "+" number is used to concatenate the variable               SYSTEM.OUT.PRINTLN ("This is the number" +num);             System.out.println ("I have the +num+" book!) ");            }    } output: This is the number 3   I have 3 books!    If you do not understand the above program is not related, first knock it into the Java editor, save it, edit, run, you can see the above output.   from the above output results can be seen System.out.println () function, is the output parentheses contained in the text, as for the public, class, static, void the meaning of these keywords, will be in later chapters to do moreGo deep into one layer of discussion. Program Description:  1, 1th Action Program comments, the Java language comments are "//" flag started, comments to help the program reading and detection, the annotated content will not be executed at compile time. 2, the public class Testjava in the 2nd line is the Java keyword, class is the meaning of "class", followed by the class name, in this program named Testjava. Public is used to indicate that the class is publicly available, that is, it can be accessed throughout the program. It is important to note that if a class is declared as public, it is also necessary to take the file name sing Woo the same class name, in a Java file, there can be more than one public class, otherwise. Java files cannot be named. 3. Line 4th public static void Main (String args[]) is the starting point for the program to run. Line 4th to 10th functions like a function in a general programming language, but is called method in Java. So the main () function (main function) in C is called the Main () method in Java. The body of the  4, Main () method is from the opening brace "{" of line 5th to the closing curly brace "}" of line 11th. Each individual Java program must have main () method to run because it is the starting point for the program to start running.  5, line 6th "int num;" The purpose is to declare num as a variable of an integer type. You must declare the type before using the variable.  6, line 7th "num=3;" is an assignment statement that assigns the integer 2 to the variable num that holds the integer.  7, Line 8th of the statement is: System.out.println ("This is the number" +num); When the program runs, it outputs the contents of the quotation marks ("") on the display. Includes two parts: "This is a number" and the integer variable num stores the value.  8, System.out refers to the standard output, usually related to the computer's interface devices, such as printers, monitors and so on. The subsequent continuation of the text println is made up of print and line, meaning that the contents of the following brackets are printed on the standard output device-the monitor. Therefore, the statement on line 8th will be wrapped, that is, move the cursor to the beginning of the next line to continue the output. The reader can put SyStem.out.println (), Change to System.out.print (), and look at the difference between line breaks and non-wrapping. The closing brace for  9, line 10th tells the compiler that the main () method ends here.  10, the 11th line of the closing brace tells the compiler class Testjava to end here. Here is just a brief introduction of Testjava This program, I believe that the reader has a preliminary understanding of the Java language. Although the Testjava program is very short, it is a fairly complete Java program!

Java Learning Note 11/15: A simple Java example

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.