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...

  1. Integer Constants
  2. Real Constant / Floating-point constants
  3. Character constants
  4. String constants
  5. 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

  1. Integer constant can be positive, negative, or zero.
  2. 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.
  3. Decimal places are not allowed.
  4. 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.2345 all are floating-point constants.

Rules for floating-point constants

  1. All positive and negative numbers are allowed.
  2. Decimal places are allowed.
  3. 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.
  4. Valid range: -1.7E38 to 1.7E38
  5. 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. 1.234E6, 0.7E-4 are some examples of exponent notation. where 0.7E-4 is equivalent to 0.7 * 10 -4.
  2. 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

  1. A character constant must be a single character.
  2. A character constant must be enclosed in single-quotes.
  3. 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.

  1. ASCII (American Standard Code for information interchange)
  2. 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 

CharactersUnicode
A - Z65 - 90
a - z97 - 122
0 - 948 - 57
space32
enter13

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

  1. 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

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