Rookie note two, constants and variables

Source: Internet
Author: User

Constant

Constants are variables that do not change in the course of their use. When you declare and initialize a variable, you can specify a variable as a constant by adding a const to the front of the variable. Example: const int a=200;

Characteristics of constants:
Constants must be initialized at the time of declaration. Once you have specified its value, you can overwrite it.
The value of a constant must be used for calculations at compile time. Therefore, you cannot initialize a constant with a value that is extracted from a variable.
Constants are always static. You do not have to (in fact, not allow) a const declaration that contains modifier static.

There are at least 3 benefits to using constants in your program:
Easy to read, easy to modify and avoid mistakes.



Variable
An object that has variable names and values.
Declaration of variable: [access modifier] type variable name [= initial value]; Example: int i=0;
The variable name must be a letter or an underscore cannot have a special symbol at the beginning.

Note:
Variables must be initialized. Otherwise, the compiler has no basis for inferring the variable type.
The initializer cannot be empty.
The initializer must be placed in an expression.
The initializer cannot be set to an object unless a new object is created in the initialization.

Rookie note two, 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.