"Java" Java_10 constants and variables

Source: Internet
Author: User

1. Variables (variable)

  1.1 We manipulate the data in the storage space through variables, the variables refer to this storage space! The spatial location is deterministic, but what values are placed inside are not deterministic!

1.2 Java is a strongly typed language, and each variable must declare its type.

1.3 Java variable is the most basic storage unit in the program, its features include variable name, variable type and scope.

 1.4 variables must be declared before they are used ,  You can assign a storage unit of the corresponding length only after the variable declaration, in the form:

Type varName [=value] [{, Varname[=value]}] ;

Precautions:

<1> Each variable has a type, the type can be a base type, or it can be a reference type.

the <2> variable name must be a valid identifier.

the <3> variable declaration is a complete statement, so each declaration must end with a semicolon.

  1.5 examples of variable declarations:

    Double salary ;

Boolean done ;

Long earthpopulation ;

int Age ;

  1.6 Variable Partitioning

Local Variables (llocal variable) : A variable defined inside a method or statement block. The life cycle starts from the declaration position to "}" .

    instance variable (member variable member variable): A variable that is defined internally by a method outside of a class. From belonging to an object, the life cycle accompanies the object always.

    static variable (class variable static variable): Uses the static definition. from belonging to class, the life cycle accompanies the class always, from class loading to unloading

Note:     If it does not initialize itself, he automatically initializes the default initial value of the type (numeric variables are initialized to 0 or 0.0, and the initialization value of the character variable is 0, boolean default is false)

2. Code to indicate:

 Public classtestvar{//instance variables, properties, member variables    intm;  Public Static voidMain (string[] agrs) {intA//declaring local variablesA = 3;//Assign ValueSystem.out.println (the value of "a" is: "+a); Booleanh =false;//declaring a Boolean variable and assigning a valueSystem.out.println (The value of the Boolean type H is: "+h); floatf = 3.15f; System.out.println (The value of "floating-point F" is: "+G); //constant (no longer assignable after initialization)        Final intn = 3; //n = 5;//Error: Cannot assign a value to the final variable nSystem.out.println ("The value of the constant n is:" +N); }}

"Java" Java_10 constants and 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.