Variables in the Java language

Source: Internet
Author: User

1. Variables

A variable is a pronoun that refers to the data in memory.

A variable is a quantity that can be changed----> refers to different data.

2. Use of variables

Variables must be declared before use:

Syntax: data type variable name;

int A;

You can declare multiple variables of the same type in a single statement

such as: int a,b,c;

int a=1,b,c=2;

3. Naming of variables

Provisions:

    • Can contain only letters, numbers, _,$, and cannot start with a number
    • Strictly case-sensitive
    • Cannot use keywords (words that have been used in Java)

Suggestions:

    • Chinese named variables are not recommended
    • Better "See the name"
    • Hump Naming method: First letter lowercase, the remaining letters capitalized.

4. Initialization of variables

Java syntax rules: variables must be initialized before they are used

Initialize two ways:

    • Declaring simultaneous initialization

int a=5;

    • First declare after initialization

int b;b=8;

5. Use of variables

Must match the data type:

such as: int a=2.5;//error, type mismatch

6. Scope of variables

    • From the beginning of the variable declaration to the nearest curly brace that contains it.
    • Scope overlap, variables cannot have duplicate names.

7. The difference between a member variable and a local variable

Local variables:

    • Defined in the method.
    • Without a default value, you must set the default value yourself.
    • When the method is called, it exists in the stack, and is purged from the stack at the end of the method call.

Member variables:

    • Defined in the class, outside of the method.
    • There is a default initial value that can be initialized without explicit initialization.
    • After all classes are instantiated, they exist in the stack, the objects are recycled, and the member variables are invalidated.

Variables in the Java language

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.