Constants in Java: Definition and Usage Explained
Table of Content:
Constants in Java
There are several values in the real world which will never change. A square will always have four sides, PI to three decimal places will always be 3.142, and a day will always have 24 hours. These values remain constant. When writing a program it makes sense to represent them in the same way - as values that will not be modified once they have been assigned to a variable. These variables are known as constants.
Constants in java are fixed values those are not changed during the Execution of program java supports several types of Constants those are :
- Integer Constants
- Real Constants
- Single Character Constants
- String Constants
- Backslash Character Constants
Integer Constants
Integer Constants refers to a Sequence of digits which Includes only negative or positive Values and many other things those are as follows
Example- Decimal Integer Constants
- Ocatal Integer Constants
- Hexadecimal Integer Constants
- An Integer Constant must have at Least one Digit
- it must not have a Decimal value
- it could be either positive or Negative
- if no sign is Specified then it should be treated as Positive
- No Spaces and Commas are allowed in Name
Decimal Integer Constants
Decimal constant consists of a set of digits 0 through 9, preceded by an optional –or + sign.
1, 3, 7, 8, 65, 543676664
Octal Integer Constants
An octal integer constant consists of any combimation of digits from the set 0 through 7, with an leading 0.
038, 320, 0456, 0552, 0432
Hexadecimal Integer Constants
An octal integer constant consists of any combimation of digits from the set 0 through F, with an leading 0x or 0X.
0x4, 0X456, 0x552, 0x32, 0x43
Real Constants
Integer numbers are unable to represent distance, height, temperature, price, and so on. These informations are contaning fractional parts or real parts like 56.890. Such numbers are called Real or Floating Point Contants
Example- A Real Constant must have at Least one Digit
- it must have a Decimal value
- it could be either positive or Negative
- if no sign is Specified then it should be treated as Positive
- No Spaces and Commas are allowed in Name
Like 251, 234.890 etc are Real Constants
In The Exponential Form of Representation the Real Constant is Represented in the two Parts The part before appearing e is called mantissa whereas the part following e is called Exponent.
- In Real Constant The Mantissa and Exponent Part should be Separated by letter e
- The Mantissa Part have may have either positive or Negative Sign
- Default Sign is Positive
Single Character Constants
A Character is Single Alphabet a single digit or a Single Symbol that is enclosed within Single inverted commas.
- Character Constant Can hold Single character at a time.
- Contains Single Character Closed within a pair of Single Quote Marks
- Single Character is smallest Character Data Type in C.
- Integer Representation: Character Constant reprent by Unicode
- It is Possible to Perform Arithmetic Operations on Character Constants
Examples of Character Type:
'a' 'A' '1' '4343' '#' '-' '<' 'X' ETC.
How to Declare Character Variable?
Way 1: Declaring Single Variable
char variable_name;
Way 2: Declaring Multiple Variables
char var1,var2,var3;
Way 3: Declaring & Initializing
char var1 = 'A',var2,var3;
String Constants
String is a Sequence of Characters Enclosed between double Quotes These Characters may be digits, Alphabets Like "Hello", "1234" etc.
- The string is "Sequence of Characters".
- String Constant is written in Pair of Double Quotes.
- The string is declared as Array of Characters.
- In C, String data type is not available.
- Single Character String Does not have Equivalent unicode Value
Example | Meaning |
---|---|
"a" | String with Single Character |
"Ali" | String With Multiple Characters |
"123" | String With Digits |
"How are You" | String With Blanks |
'R'
is not Equal to "R"
.
'R'
it represent a character.
"R"
it represent a string.
Backslash Character Constants
Java Also Supports Backslash Constants those are used in output methods For Example \n is used for new line Character These are also Called as escape Sequence or backslash character Constants For Example :
- Although it consists of two characters, it represents a single character.
- Each escape sequence has unicode value.
- Each and Every combination starts with back slash()
- They are non-printable characters.
- It can also be expressed in terms of octal digits or hexadecimal sequence.
- Escape sequence in character constants and string literals are replaced by their equivalent and then adjacent string literals are concatenated
- Escape Sequences are preprocessed by Preprocessor.
\b Backspace \f Form feed \n New line \r Carriage return \t Horizontal tab \" Double quote \' Single quote \\ Backslash \v Vertical tab \a Alert(very small sound) \? Question mark \N Octal constant (N is an octal constant) \xN Hexadecimal constant (N is a hexadecimal constant)
Escape Character | Name | Unicode Code | Decimal Value |
\b | Tab | \u0009 | 8 |
\t | Tab | \u0009 | 9 |
\n | Linefeed | \u000A | 10 |
\f | Formfeed | \u000C | 12 |
\r | Carriage Return | \u000D | 13 |
\\ | Backslash | \u005C | 92 |
\" | Double Quote | \u0022 | 34 |