## Java Constants

### Constants in Java

Observe the expression below...

y = x + 5;

In the above expression, x and y are variables and 5 is a constant.

So constant is an entity that doesn't change during the program execution.

Constant can be of multiple types as listed below...

- Integer Constants
- Real Constant / Floating-point constants
- Character constants
- String constants
- boolean constants

### Integer Constants

Any number which doesn't have decimal points are integer constants. For example **2, 456, -231, 0** all are **integer constants**.

#### Rules of integer constants

**Integer**constant can be positive, negative, or zero.- Spaces and special symbols are not allowed. For example,
**72,314**and**72 314**are**invalid**integer constants whereas**72314**is a**valid**integer constant. - Decimal places are not allowed.
**Allowed range**,**-2147483648 to 2147483637**. This is a general integer type value, it can be different based on the type will discuss in upcoming chapters.

### Floating point constants

Numbers with decimal places are known as floating-point constants. For example,23.456, 0.0007, -1.2345all are floating-point constants.

#### Rules for floating-point constants

- All positive and negative numbers are allowed.
- Decimal places are allowed.
- Spaces and special symbols are not allowed. For example, 23,5.67 and 23-3.1 are invalid floating-point constants whereas 1.234, 0.007 valid floating-point constant.
- Valid range:
**-1.7E38 to 1.7E38** - Floating-point constants can be written in two formats as explained below.

##### Fixed point notation

12.33, 4.56, 0.007 are some examples of fixed point notations.

##### Exponent notation

- 1.234E6, 0.7E-4 are some examples of exponent notation. where
**0.7E-4**is equivalent to**0.7 * 10**.^{-4} - In
**0.7E-4**, the part was written before**E**is called**mantissa**and the part written after E is called the**exponent**.

### Character constants

A single character type value is called character constant. A character constant is generally written in single quotes. For example, 'A', '0', 'm' are some character constants.

#### Rules for character constants

- A character constant must be a single character.
- A character constant must be enclosed in single-quotes.
- Java uses Unicode character encoding.

#### Character Encoding

As you know, a computer memory store everything in the form of 0's and 1's. Which is called the binary representation of data.

A number can easily be converted into binary equivalent, but how a character is converted into binary language.

So each character is represented by a number. On your keyboard, each character is represented by a number internally. The mapping of character and numbers are called Character encoding.

There are two popular character encoding in computer science.

- ASCII (American Standard Code for information interchange)
- Unicode

#### ASCII

ASCII stands for American standard code for information interchange. It is an **8-bit** encoding scheme in which each character occupies 8 bits memory to represent a character internally. Generally, this encoding covers alphabets, digits, white spaces, and some special symbols.

It has only **256** characters in the list ranges from **0 to 255**.

#### Unicode

As the age of the computer grows, multiple language support is required. So a new character encoding launched which contains characters and symbols of almost every popular language over the world.

This is a **16-bit** encoding scheme that requires 16 bits of memory space to store a character in memory.

It includes **65536** characters ranges from **0 to 65535**. The first **256** characters of **Unicode** are the same as **ASCII** so it is also called a **superset** of **ASCII**.

#### Mostly used Characters and Unicode

Characters | Unicode |

A - Z | 65 - 90 |

a - z | 97 - 122 |

0 - 9 | 48 - 57 |

space | 32 |

enter | 13 |

### String constant

A sequence of characters is called a string. For example, "Alex", "8290773375", "abc@example.com" are some string constants.

#### Rules for string constants

- It must be enclosed in double-quotes.

### boolean constants

boolean constants are used to represent binary states like true or false, right or wrong, on or off. In java, true and false are two boolean constants.

#### Rules for boolean constants

- true and false are boolean constants in java.
- In java,
**true != 1**and**false != 0**like other languages C / C++.