Java variables and basic data types

Source: Internet
Author: User

1.Java variables

1.1 Concepts of variables

    • A storage area in memory;
    • The region has its own name (variable name) and type (data type);
    • The data of the region can be changed continuously within the same type;

1.1.1 Why you need to define variables

    • Used for constant storage of the same type of constant and can be reused;

1.1.2 Defining the format of a variable

    • Variable name of data type = initialization value;

1.1.3 Variable Use range

    • The scope of the variable (valid between a pair of {}, "nearest principle");

1.2 Use of variables

1) Disclaimer

int A; Declares an integer variable named A;

int b,c,d; Represents a declaration of three integer variables, named b,c,d;

2) naming

    • Can only contain numbers, captions, _ (underscore), and $ (dollar sign), and cannot start with a number;
    • Strictly case-sensitive
    • Cannot use keywords
    • Can be named in Chinese, but not recommended
    • Naming rules: suggested "Hump naming", "See the name", such as defining a variable to indicate student score: Studentscore

3) Initialization

Simultaneous initialization of declarations:

Declare and Initialize first:

int a;a = 250;

4) Use

    • The operation of a variable is to manipulate the number it has stored.
int a = 5; Declare the variable A and assign a value of 5;int B = a+10; Declare variable B, take out the value of a of 5, plus 10, and assign to BSystem.out.println (b); The value of output B is 15a = A+b;  Remove the value of a of 5, then remove the value of B 15, and then the two number and re-assigned to ASYSTEM.OUT.PRINTLN (a); The value of output A is 20
    • The operation of the variable must match the data type
int a = 3.14;//error: Type mismatch, the type Mismatch:cannot convert from double to int
    • Variables must be declared and initialized before they are used
System.out.println (m);  Compile error, m not declared int m; System.out.println (m);  Compilation error, m not initialized

  

Java variables and basic data types

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.