Java programming 18-variables and constants

Source: Internet
Author: User

Java programming those things 18 -- variables and Constants by Chen yuefeng from: http://blog.csdn.net/mailbomb 3.6 variables and constantsThere is a large amount of data in the program to represent the state of the program. Some of the data changes in the middle of the program running process, and some of the data cannot be changed during the program running process, these data are called variables and constants respectively in the program. In actual programs, you can choose whether to use variable representatives or constant representatives based on whether the data changes during the program running. 3.6.1 VariablesVariable represents the state of the program. The program changes the state of the entire program by changing the value of the variable, or greater, that is, implementing the functional logic of the program. To easily reference the value of a variable, you must set a name for the variable in the program. This is the variable name. For example, in a 2D game program, to represent the position of a character, two variables are required, one is the X coordinate and the other is the Y coordinate. During the program running, the values of these two variables change. Because Java is a strongly typed language, variables must be declared before they are used. The syntax format of declaring variables in the program is as follows: data type variable name; for example: int X; in this syntax format, the data type can be any type in the Java language, including the basic data types described earlier and the composite data types to be introduced later. The variable name is the identifier of the variable. It must comply with the naming rules of the identifier. In actual use, the name generally corresponds to the purpose of the variable, so that the program can read it easily. The data type and variable name are separated by spaces. The number of spaces is unlimited, but at least one is required. The statement ends. You can also set the value of a variable while declaring it. The syntax format is as follows: data type variable name = value; for example: int x = 10; In this syntax format, the preceding syntax is consistent with the content described above. "=" in the future indicates the value assignment, and "value" indicates the specific data. In this syntax format, the required value type must be consistent with the data type of the declared variable. You can also declare multiple variables of the same type at a time. The syntax format is as follows: data type variable name 1, variable name 2 ,... Variable name N; for example, int x, y, z; In this syntax format, variable names are separated by commas (,). there can be any number of variable names. You can also assign values to variables when declaring multiple variables. The syntax format is as follows: data type variable name 1 = value 1, variable name 2 = value 2 ,... Variable name n = value n; for example: int x = 10, y = 20, Z = 40; you can also assign values when declaring a variable, for example: int X, in the preceding syntax format, if multiple variables are declared simultaneously, the types of these variables must be the same. If the declared variables have different types, you only need to declare them separately, for example, int n = 3; Boolean B = true; char C; in the program, the value of the variable represents the state of the program, in a program, you can use the variable name to reference the value stored in the variable, or you can assign a value to the variable. For example, int n = 5; n = 10; during actual development, you need to declare the types of variables, how many variables need to be declared, and what values need to be assigned to the variables, it is determined by the program logic. Here we only list the expression formats. 3.6.2 constantA constant represents a value that cannot be changed during the program running. Constants play two main roles in program running: l Represents constants, which facilitates program modification. L enhances program readability. Syntax format and variable type of constants, you only need to add the keyword final before the syntax format of the variable. In Java coding specifications, the constant name must be capitalized. The syntax format of a constant is as follows: Final data type constant name = value; final data type constant name 1 = value 1, constant name 2 = value 2 ,...... Constant name n = value n; for example: Final double Pi = 3.14; Final char male = 'M', female = 'F'; in Java syntax, constants can also be declared first, then, assign values, but assign values only once. The sample code is as follows: Final int up; up = 1; the sample code corresponding to the two usage of constants is as follows: l represents the constant final double Pi = 3.14; int r = 5; double L = 2 * pI * r; double S = pI * r; in this sample code, constant PI represents the Gini value in mathematics, that is, the circumference rate. This is a mathematical constant. The subsequent variable R represents the radius, l represents the circumference of the circle, and s represents the area of the circle. If you need to increase the accuracy of the Program Computing, you only need to modify the PI value 3.14 to 3.1415926, and re-compile the program, the subsequent values will automatically change, so that the code is easy to modify, easy to maintain. L enhance the readability of the program. Int ction; Final int up = 1; Final int down = 2; Final int left = 3; Final int right = 4; in the sample code, the direction variable indicates the value of the direction. The following four constants, namely up, down, left, and right, indicate the upper, lower, and left. The values are 1, 2, 3, and 4 respectively, it can improve the readability of the program. 3.6.3 statement BlockIn the program, the content contained in a pair of braces {} is called a statement block. The statement blocks can be nested with each other. There is no limit on the hierarchy of nesting, for example: {int ;} statement block nesting: {int B; {char C ;}} the above Code only demonstrates the syntax and has no logic significance. In the subsequent syntax introduction, there will also be the concept of statement blocks, so we will not repeat them. 3.6.4 scope of the VariableEach variable has a specific scope, which is also called a valid range or scope. It can only be used within this range. Otherwise, a syntax error is prompted. Normally, variables with the same name cannot be declared within a scope of action. The scope of a variable starts from the position of the variable declaration and ends with the braces of the statement block where the variable declaration is located. For example, run the following code: {int A = 10; A = 2;} Char C;} in this Code, variable A ranges from row 3 to row 5, the range of variable C is from row 6 to row 7. 3.6.5 scope of constantThe range of a constant is exactly the same as that of a variable. 3.6.6 SummaryThis article describes variables and constants. The following is a complete code that can be compiled and run in JDK or eclipse. The code file name is variableandconst. java, the sample code is as follows: public class variableandconst {public static void main (string [] ARGs) {int n = 0; char c = 'a'; system. out. println (n); n = 10; system. out. println (n); system. out. println (c) ;}}note: in this Code, system. out. the println function is the value stored in the output variable.

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.