constant Concept Simplified in Java
Constant:
If we do not want the value of a variable to change in java, after it is initialized.
Java provides a keyword called final that can be applied to a variable declaration to ensure that the value stored in the variable can not be changed.
After the variable has been initialized. Once a variable is having final modifier it is impossible to change it’s value.
If tried, JVM will throw an error and reject the updated value. So Constant are those kind of variable whose value can not be changed or they hold fixed values during execution.
They refer to the final variables whose value can not be changed during run time.
It is also legal to apply the final modifier to local variables and formal parameters. But it is very useful in number variables.
The readability of the code is greatly enhanced with using a named constant.
static final YEARS=2018;
Constant Concept Simplified in Java
Java constants can be divided into four broad categories:
- Numeric Constants
- Character Constants
- Symbolic Constants.
- Enumerated Constants
- Integer Constant
- Real Constants
- Decimal Integer Constants
- Octal Integer Constants
- Hexadecimal Integer Constants
- Fractional Point Constant
- Exponential ConstantsCharacter Constants can be divided into 2 categories:
- Character Constants
- String Constants.
Constant Concept Simplified in Java
Character constants can also be divided into- Single Character Constant
- Backslash Character Constant.
1.1 Integer Constant:An integer constant refers to a sequence of digits.1.1.1. Decimal Integer Constant:Decimal Integer Constant consists of digits(0 to 9) .They may also preceded by a optional minus sign like 123,-345,678,0 etc.Space ,comma,non digit characters are not permitted here.1.1.2. Octal Integer Constant:Octal Integer Constant consists of digit (0 to 7) with a leading 0 like-037,0,0657 etc.1.1.3. Hexadecimal Integer Constant:Hexadecimal Integer Constant is a sequence of digits preceded by 0x or 0X .
It can consist of digits (0 to 9), characters (A to F) as (10-15) like 0X2,0xbcd etc.
1.2. Real Constants:
They represent real numbers containing fractional part or Exponential part.1.2.1 Fractional Point Constant:
Fractional Point Constant represents by numbers containing fractional parts.They are called real or floating point constants. like- 0.083,.05 etc.
These decimal notation followed by a decimal point and fractional part represents fractional point constant.
1.2.2 Exponential Constant:
A real number constant can be represented as Exponential Constant notation.Like 215.65 can be expressed as 2.1565 e (to the power) 2.
generic formula- mantissa e (to the power) exponent.Mantissa is either a real number expressed in decimal notation or an integer.
Exponent is an integer with optional plus or minus sign.E can be written in lowercase(like e) or uppercase (like E).
However there is no space is permitted inside the expression.Constant Concept Simplified in Java
2.1.1 Single Character Constant:
Single Character Constant is a fixed value enclosed within a single quote.Like-‘5′,’x’.Numbers enclosed inside single quote is not equal to number.
2.1.2 Back Slash Character Constant:
Back Slash Character Constant are supported by java and used in output methods:Sign Meaning Sign Meaning ‘b’ backspace ‘”‘ double quote ‘n’ newline ‘f’ form feed ‘t’ Horizontal tab ‘r’ carriage return ”’ single quote ‘\’ back slash The consists of two characters known as escape sequence.
2.2 String Constant:
String Constant is a sequence of characters enclosed within double quote.The characters may be alphabets,digits,special characters(including blank space).Like-“Hello”,”1983″ etc.
3. Symbolic Constants :
When we want to define an unique constant that may appear in number of places (like pi=3.14),we may face two issues:- Problem in modification of the program– Here we need to search throughout the code and explicitly change the value of the constant wherever it has been used.
- If any value left unchanged they will produce wrong result.
- Problem in understanding the program-When a numeric value appears in a program,it is not always clear especially when the same value means different things in different places.We may forget what a certain number mean. In that case also we need to revisit the code to understand.
Constant Concept Simplified in Java
In order to avoid these issues we use Symbolic constants.The best practices for Symbolic Constant declaration:- Make the variable all uppercase like FIRSTNAME,PI. Even in the java’s standard library uses this convention like Color.RED or Color.YELLOW.
- Put a type final about the modifier. like- final symbolicName=value
- Always assign a value to the variable
- Symbolic constants can be declared for types.
- They can not be declared inside method body.They should be used only as class data members in the beginning of the
Enumerated Constants:
Enumerated type constants are also example of named constant .The definition of an enum is given as:
enum Alignment{LEFT,RIGHT,CENTER} //the constants are: Alignment.LEFT; Alignment.RIGHT Alignment.CENTER;
Technically Alignment is a class having three constants like below:
public static final int ALLIGNMENT_LEFT=0; public static final int ALLIGNMENT_RIGHT=1; public static final int ALLIGNMENT_CENTER=2;
The only problem with this enum is that JVM has no way of knowing that we intend the value of the variable to represent an Allignment.As a result it will not throw error if the value that is assigned to the variable is not one of the three valid alignment values.With the enumerated type, only values that can be assigned to a variable type.Alignment are the constant values that are listed in the definition of the enumerated type.As a result any attempt to assign an invalid value to the variable is a syntax error which the compiler will detect when the code is compiled.class.