On a constant

 

1. What is a constant?

Refers to an amount in the constant running Java its value remains constant, and the constant is noted here that different concepts are constant values, and constant values ​​specific visual manifestations constant, constants are formalized performance.

Defining constants : the Java program we define a constant by the final keyword. Note that when a constant need to give a statement on the initial value, and its value once the assignment can not be modified.

Constant value : also called literal constant value, his manifestations of specific numerical values, so there are many types of data, such as integer and string like. The following introduced one of these constant values.

II. Classification constant

In Java constants are generally divided into four categories: constant integer value, real value of the constant, Boolean constants, and the character string constants.

Integer constant

The default is int type integer constant, -32 bits occupies 4 bytes in memory.

  1. Decimal: 10, -89

  2. Octal form: -0212,034 / octal numbers beginning with 0

  3. Hexadecimal: 0x43, -0X78 / hexadecimal number beginning with 0x or 0X

Real constant

Real is a floating-point type, the default double type in Java, occupies 8 bytes -64 bits in memory.

  1. Decimal form: 1.23, -0.45

  2. Scientific notation form: 1.75e4,34E4

Boolean constants

Boolean constants are only two values, false (false), true (true).

Character and string constants

In Java, a single quote character string assigned to contain double quotes. This requires special attention to the two can not be mixed,

Escape character: in Java also allows the use of a special form characters represent some constant value expressed by the general difficult characters, characters in the sequence of characters at the beginning of this particular form is known as the escape character.

Relationship of the constant value, constant name and constant

If we take the constant analogy to a house, then the constant value is something in the house, the room is a brand name of the constant number.

Why give a name constants
  1. If the constant value a long, complicated, we can give him a name, in the following code

    Writing If you need to use this value directly instead of writing name on it, which is conducive to the proper degree program writing.

  2. See text meaning is very important to know when writing the code behind if someone needs to rewrite your code a meaningful name than a string of numbers or characters easier to understand.

  3. Also for subsequent maintenance, modifications, if required to change a constant value, we only need to modify the statement can be initially defined.

Guess you like

Origin www.cnblogs.com/-Archenemy-/p/11830939.html