corejavae10v1p3.4 3rd Java BASIC programming Structure-3.4 variables

Source: Internet
Author: User

1. In Java, each variable must have a type, in the variable declaration, the type must precede the variable name. Examples are as follows:

Double salary; int vacationdays; Long earthpopulation; Boolean done;

Each declaration must end with a semicolon, and the declaration is a complete statement, and each complete statement in Java must end with a semicolon.

2. Variable naming rules

Variable names must start with a letter, consist of letters and numbers, and letters in Java refer to Unicode characters.

Reserved words cannot be used.

Case sensitive.

Specification

You can declare multiple variables in a row, but not recommended, and not readable.

Do not name two different variables in case of differences

Note: You can check with the Isjavaidentifierstart and Isjavaidentifierpart method in the Character class (Java.lang.Character).

3.4.1 Variable Initialization

Declaring a variable as soon as possible using an assignment statement for display initialization, not using uninitialized variables, the following statements compile errors in Java

int//  error--variable not initialized

Declarations and initializations can be written on a single line or multiple lines.

The statement should be written as far as possible in the first place.

Java does not distinguish between declarations and definitions, unlike C + + +.

Declaring a variable in Java does not actually allocate storage space to the variable, which is equivalent to a pointer declaration in C, just registering a

int A in Java, which is equivalent to int * A in C;

3.4.2 constant (constant)

In Java, you use final to specify that a variable is a constant, not a const in C, but a const is still a reserved word for Java.

 Public class constants{    publicstaticvoid  main (string[] args)    {          Finaldouble cm_per_inch = 2.54;         double paperwidth = 8.5;         double paperheight = one;        System.out.println ("Paper size in centimeters:" + paperwidth * cm_per_inch + "by" + paperheight * cm_per_inch );    }}                

The final keyword specifies that the variable can only be assigned once, once it is assigned, cannot be changed, and the habit is that the constant name is all uppercase.

corejavae10v1p3.4 3rd Java BASIC programming Structure-3.4 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.