Constant & Literals in Java

Constants:
  • A constant is a variable whose value cannot change once it has been assigned. 
  • Java doesn't have built-in support for constants.
  • It has special two-variable modifiers...such as static and final.

1. Static Modifier:

  • Static is used without first creating an instance of the class.
  • Example: main() method.
    • main() method doesn't need any object.
    • main() method is executed for every program and declared as static.
  • A static class member is associated with the class itself, rather than an object. All class instances share the same copy of the variable.
  • Syntax: static datatype variable = value;
  • Example: static int book = 101;

2. Final Modifier

  • The final modifier means that the variable's value cannot change. 
  • Once the value is assigned, it cannot be reassigned. 
  • Primitive data types can be declared using the final modifier.
  • Syntax:  final datatype variable_name = value;
  • Example:  final int myNum = 15;

Difference between Static Vs Final

  • 1. Static define by static & final define by final keyword.
  • 2. Final variable become constant & static variable become common to all object in the given class.
  • 3. The final variable cant be processed in the inherited class while static variable same for each object in the given class.
  • 4. The final variable value cant be change & static variable can set the default value but can be reinitialized.

Java Program

Literals:
  • Any constant value which can be assigned to the variable is called as literal/constant.
    • Syntax: datatype variable_name = value;
    • Example: int x = 100; 
  • 1. Integral literals
  • 2. Floating-Point literal
  • 3. Char literal
  • 4. String literal
  • 5. boolean literals

1. For Integral data types.
  • For Example byte, short, int, long
  • A. Decimal literals (Base 10):
    • In this form, the allowed digits are 0-9.
    • Example: int x = 101;
  • B. Octal literals (Base 8): 
    • In this form, the allowed digits are 0-7.
    • Example: int x = 0146; // The octal number should be prefix with 0.
  • C. Hexa-decimal literals (Base 16): 
    • In this form, the allowed digits are 0-9 and characters are a-f. 
    • We can use both uppercase and lowercase characters. 
    • Here, java is not case-sensitive (with 0X or 0x).
    • Example: int x = 0X123Face;  // The hexadecimal number should be prefix
  • D. Binary literals: 
    • With JDK 1.7 onward we can specify binary literals value.
    • In this form allowed digits are 0 and 1. 
    • Literals value should be prefixed with 0b or 0B.
    • Example: int x = 0b1111;
Example:
int a = 101; // decimal-form literal 
        int b = 0100; // octal-form literal 
        int c = 0xFace; // Hexa-decimal form literal 
        int d = 0b1111; // Binary literal 
        System.out.println(a); 
        System.out.println(b); 
        System.out.println(c); 
        System.out.println(d);


2. Floating-Point literal
  • For Example float, double
  float a = 101.230; // decimal-form literal 
        float b = 0123.222; // It also acts as decimal literal 
        float c = 0x123.222; // Hexa-decimal form 
        System.out.println(a); 
        System.out.println(b); 
        System.out.println(c);

3. char literal:
  • A. Single quote: 
    • single character within a single quote.
    • Example:char ch = 'a';
  • B. Char literal as Integral literal: 
    • Char literal as integral literal which represents Unicode value.
    • It can be either in Decimal, Octal and Hexadecimal forms. 
    • It allowed range is 0 to 65535.
    • Example: char ch = 062;
  • C. Unicode Representation:
    • Char literals in Unicode representation ‘\uxxxx’. 
    • Here 'xxxx' represents 4 hexadecimal numbers.
    • Example: char ch = '\u0061';
  • D. Escape Sequence: 
    • Every escape character can be specify as char literals.
    • Example: char ch = '\n';
Example:
char ch = 'a'; // signle character literl within signle quote 
        char b = 0789; // It is an Integer literal with octal form 
        char c = '\u0061'; // Unicode representation 
        System.out.println(ch); 
        System.out.println(b); 
        System.out.println(c); 
        // Escape character literal 
        System.out.println("\"  is a symbol"); 

4. String literal
  • Any sequence of characters within double quotes is treated as String literals.
  • Example: String s = "Hello";
5. boolean literal
  • Only two values are allowed for Boolean literals i.e. true and false.
  • Example: boolean b = true;
Example: 
boolean b = true; 
        boolean c = false; 
        boolean d = 0; 
        boolean b = 1; 
        System.out.println(b); 
        System.out.println(c); 
        System.out.println(d); 
        System.out.println(e); 



Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form