Java Basics-constants, variables, member variables, local variables

Source: Internet
Author: User
Tags naming convention value of pi

In Java, data is stored and represented in the form of constants and variables (in fact, all the data for the program are in both of these forms).

variables

The variable represents the state of the program. The program changes the value of the variable to change the state of the whole program, or more, that is, the implementation of the program's functional logic.
In order to conveniently reference the value of a variable, you need to set a name for the variable in your program, which is the variable name.

Because the Java language is a strongly typed language, variables must first be declared before they are used, and the syntax for declaring variables in a program is as follows:
Data type variable name;

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 that will be described later. The variable name is the identifier of the variable and requires a naming convention that conforms to the identifier, which, in practice, corresponds to the purpose of the variable so that it is easy for the program to read. The data type and variable names are separated by spaces, with no limit on the number of spaces, but at least 1 are required. The statement uses ";" as the end.
You can also set the value of the variable while declaring it, with the syntax in the following format:
Data type variable name = value;

int x = 10;

In this syntax format, the preceding syntax is consistent with what is described above, and subsequent "=" represents the assignment, where the "value" represents the specific data, and the difference between "= =" represents the equality of the judgment. In this syntax format, the type of the required value needs to be the same as the data type that declares the variable.
In the program, the value of the variable represents the state of the program, and the value stored in the variable can be referenced by the variable name in the program, or the variable can be re-assigned. For example:

int n = 5;    = 10;

Member variables:

A variable defined in a class, also known as a property (not defined in a method)

Local variables  

Variables defined in the method

Constants

Constants represent values that cannot be changed while the program is running.

Constants have 2 main functions in the process of running a program:
1. Representative constant, easy to modify the program (for example: the value of pi)
2. Enhance the readability of the program (for example: constant up, down, left, and right-resolved representatives up and down, with values of 1, 2, 3, and 4, respectively)
The syntax format and variable type of a constant, just precede the syntax format of the variable to add the keyword final. In the Java encoding specification, the constant name must be capitalized.

The syntax format for constants 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 assigned, but only once, and the sample code is as follows:
final int up;
up = 1;

Java Basics-constants, variables, member variables, local variables

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.