Constant talk

A constant

In the Java language , primarily using the final keyword (flexible use of the Static keyword in Java classes) to define constants. When the constants are set, under normal circumstances it is not allowed to make changes. It may be defined as a constant using the following form: final double PI = 3.1315. When you define this constant need to pay attention to the following: First, the time constant defined by the constant need to be initialized. In other words, it must be initialized when constant declaration. Related to different local variables or member variables. When after initialized when constants defined in the application can not again be assigned to the constant. If forced assignment, then the database will pop up an error message, and refused to accept a new value.

Classification 2 constants

Integer constant value

Java integer constant value mainly has the following three forms.

  • Decimal form: such as 54, -67,0.
  • Octal form: the Java representation of the octal constant begins with 0, as 0125 represents the decimal number 85, -013 to represent decimal numbers-11.
  • Hexadecimal form: the Java representation of hexadecimal constants start with 0x or 0X, such as 0x100 represents the decimal number 256, -0x16 represents the decimal number -22.


Integer (int) representing the default constant in memory 32, a value of an integer type, when required during operation when the value exceeds 32 bits long, it can be represented as a long integer (long) value. Long L type will have to add 1 or after the number, such as 697L, represents a long integer, which accounts for the 64-bit memory.

Real constant value

Java's real constant values ​​There are two main forms.

  • Decimal form: a decimal number and composition, and must have a decimal point, such as 12.34, -98.0.
  • Method Number scientific notation form: The 1.75e5 or 32 & E3, which must be digital before e or E, and the digits after the e or E must be an integer.


Java real constant representing the default in the memory 64, is a double (double) value. Considering the need to conserve system resources is running, and the data value is not a large range of operation and high accuracy is not operational circumstances, it can be expressed as a numerical single-precision (float) a.

Usually single-precision value to be added later in the constant F or F, such as 69.7f, float type represents a real number, which accounts for 32-bit (depending on the version of system level) in memory.

Boolean constant value

Java Boolean constants are only two values, that is false (false) and true (true).

And character string constant value

Java is a character constant values ​​caused by single quotes to a character, such as' e ', E'. Note that, Java string constant values ​​of single and double quotation marks can not be mixed. Double quotes used to represent strings, like "11", "d" are all single-character string represented.

 

Benefits constant named

1) you do not have to repeatedly enter the same value;

2) If it is necessary to modify a constant value, only need to modify a local source code changes;

3) Constant to assign a descriptive name to improve readability,

 

 

 

 

Guess you like

Origin www.cnblogs.com/yuanjiabei/p/11832282.html