2nd java sample programs-general Linux technology-Linux programming and kernel information. The following is a detailed description. For programming languages, variables are the most basic concept. You may know that a variable is a memory location with a name and can be assigned a value. In addition, the variable value can be changed during the running of the program. The next program will introduce how to declare variables and assign values to variables. In addition, this program also describes some new features output by the console. From the comments at the beginning of the program, we can see that you should name this file Example2.java.
/* Here is another short example.
Call this file "Example2.java ".
*/
Class Example2 {
Public static void main (String args []) {
Int num; // this declares a variable called num
Num = 100; // this assigns num the value 100
System. out. println ("This is num:" + num );
Num = num * 2;
System. out. print ("The value of num * 2 is ");
System. out. println (num );
}
}
When you run the program, you will see the following running results:
This is num: 100
The value of num * 2 is 200
Let's see how this result is produced. We focus on different code from the previous example. The first line of code not displayed in the previous program is:
Int num; // this declares a variable called num
This row declares an integer variable named num. Like most other languages, variables must be declared first in Java before they can be used.
The following is a general form of declaring variables:
Type var-name;
Here, type indicates the type of the variable to be declared, and var-name indicates the name of the variable to be declared. If you want to declare multiple variables of the same type, you only need to use commas to separate them. Java defines several data types: integer, character, and floating-point ). The int keyword refers to the integer type.
In the program, the following code will assign 100 to the variable num.
Num = 100; // this assigns num the value 100
In Java, the value assignment is an equal sign.
The following program outputs the string "This is num:" Before outputting the variable value :".
System. out. println ("This is num:" + num );
In this statement, the plus sign "+" before the variable num is used to connect the value of num with the string before it, and then output the content of the result string (in fact, the variable num is assigned a value first, then converted into a string, and then connected to the string before the plus sign. This process will be discussed in detail later in this book ). This method can be promoted. With the plus sign "+", you can connect as many strings as possible in the println () method.
In the following statement, the result of the variable num multiplied by 2 is assigned to the variable num. Like most other languages, Java uses the "*" symbol to represent multiplication. After this line of statements is executed, the value of the variable num is 200.
The following two lines of code in this program are:
System. out. print ("The value of num * 2 is ");
System. out. println (num );
There are several new contents in the two rows. First, The built-in print () method is used to display The string "The value of num
* 2 is ". This string is not followed by a line break, which means that if the second output is generated, it will start to output in the same line. The print () method is similar to the println () method, but it does not output a new line (line feed) after each call ). Second, when calling println (), note that the variable num can be used by itself. Both method print () and method println () can be used to output any built-in type value of Java.
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.