Variables and constants in Java, "Three Java Learning notes"

Source: Internet
Author: User
Tags modifier naming convention type null

Variables and constants

In the program there is a large amount of data to represent the state of the program, some of the data in the course of the program's operation, the value will change, some data in the program during the operation of the value can not be changed, the data in the program is called variables and constants.

In the actual program, you can choose whether to use a variable representation or a constant, depending on if the data has changed in the program's operation.

Variable

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;
For example: int x;
In this syntax format, the data type can be any type in the Java language, including the base data type and the composite data type. 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;
For example: int x = 10;
In this syntax format, the preceding syntax is consistent with what is described above,The subsequent "=" represents the assignment, where the "value" represents the specific data, and the difference between "= =" is expressed as the determination of equality. 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;
n = 10;
in the actual development process, you need to declare what type of variable, how many variables need to be declared, what values need to be assigned to the variable, all according to the logic of the program, the list is only the format of the expression.

Constant

    constant represents a value that cannot be changed while the program is running.
    constants have 2 functions in the process of running a program:
          1. Represents constants for easy program modification (for example: pi value)
          2. Enhance the readability of the program (for example:     The syntax format and variable type of a constant: Simply add the keyword final before the syntax format of the variable. 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 can only be assigned once, The sample code is as follows:
 final int up;
    up = 1;

Variable type

Local variables

A local variable is declared in a method, constructed method, or in a block of statements, created when executed in a method, construction method, or block of statements, and when they are executed, the variable is destroyed; the access modifier cannot be used for local variables ; only visible in the method, constructor, or statement block that declares it The local variable is allocated on the stack, the local variable has no default value, so it must be initialized before it can be used.

Member variables (instance variables)

member variables are declared in a class, but outside of methods, construction methods, and statement blocks When an object is instantiated, the value of each instance variable is then determined;

    class variable (static variable)

Class variables, also known as static variables, are declared in the class with the static keyword, but must be outside of methods, construction methods, and statement blocks. no matter how many objects a class creates, the class has only one copy of the class variable, which is the resource of the class . Static variables are rarely used except those declared as constants, which are declared as variables of type public/private,final and static, and immutable after initialization of constants. Static variables are stored in static stores and are often declared as constants, and static declaration variables are rarely used alone. Static variables are created at the beginning of the program and are destroyed at the end of the program. has similar visibility to instance variables, but most static variables are declared as public types in order to be visible to the consumer of the class. The default values are similar to instance variables, can be assigned at declaration time or specified in construction methods, and static variables can also be initialized in static statement blocks. Static variables can be accessed by: Classname.variablename. When a class variable is declared as a public static final type, the class variable name must be capitalized, and if the static variable is not public and final, it is named in the same way as the instance variable and the local variable.

Variables and constants in Java, "Three Java Learning notes"

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.