Programming





Literals in Java

Literals in Java


by Administrator | 27-Mar-2020
Java
 |  1
Tags:  Core Java  Literals  Boolean  Character  String  Integer  Float

Share: 


Literals are the actual value that will be used in the application for the variable, constant or to perform an operation. These are various types of literals in Java:

  1. Boolean literals
  2. Character literals
  3. String literals
  4. Integer literals
  5. Floating literals

a) Boolean literals: Used where Boolean data type is required. There are only two values for Boolean literals true or false

Note: We cannot use Integer values as a boolean literal in Java, like some programming languages.

b) Character literals: Any single character enclosed within a single quotation mark is a character literal. For example a’, etc.

Escape sequence characters can also be used as character literals. There are some predefined character literals. For example  \t’, ’\n ‘, ‘\b’, ‘\r’ etc.

For a char literal, their UNICODE and ASCII values can also be used in place of the actual character.  The UNICODE value of A is '\u0041' and the ASCII value for A is 65.

Program

public class CharLiteral {
 
    public static void main(String[] args) {
 
        char charA = 'A';
        System.out.println("charA: " + charA);
 
        char asciiA = 65;
        System.out.println("asciiA: " + asciiA);
 
        char unicodeA = '\u0041';
        System.out.println("unicodeA: " + unicodeA);
 
        char charNewline = '\n';
        System.out.println("charNewline: " + charNewline);
 
        char charUnknown = 65535;
        System.out.println("charUnknown: " + charUnknown);
 
    }
}

Output

charA: A
asciiA: A
unicodeA: A
charNewline:
 
charUnknown: ? 

c) String literals: A string is a collection of characters. A string literal can be any number of characters (including 0) enclosed within double-quotes.  All the rules of character literals are also applicable for String literals.

Program

public class StringLiteral {
    public static void main(String[] args) {
        String emptyStringLiteral = "";
        System.out.println("emptyStringLiteral: " + emptyStringLiteral);

        String space = " ";
        System.out.println("space: " + space);

        String stringA = "A";
        System.out.println("stringA: " + stringA);

        String unicodeString = "Unicode \u0041";
        System.out.println("unicodeString: " + unicodeString);

        String string = "Welcome to prgrmmng.com";
        System.out.println("string: " + string);
    }
}

Output

emptyStringLiteral:
space: 
stringA: A
unicodeString: Unicode A
string: Welcome to prgrmmng.com

d) Integer Literals: For using numbers into Java programming Integer literals are required. Integer literals can only contain whole numbers i.e a number without any decimal value. The datatypes for defining Integer literals are byte, short, int and long. There are sub categories for integer literals which are:

  1. Decimal literals
  2. Octal literals
  3. Hexadecimal literals
  4. Binary literals

1) Decimal literals: Digits from 0 to 9 can be used in decimal literals. The decimal literals must not start with 0 like 007, 0100 etc.

2) Octal literals: Digits from 0 to 7 are valid Octal literals. Octal literals must start with 0 (zero).  No other digits except 0 to 7 can be used.

3) Hexadecimal literals: These consist of digits from 0 to 9 and alphabets from A- F and a-f. A Hexadecimal literal must start with 0x (zero x).  

4) Binary literal: The digital allowed for binary literals are 0 and 1. A binary literal should be prefixed with 0b or 0B. This has been introduced in Java version 1.7.

Program

public class IntegerLiteral {
    public static void main(String[] args) {
        byte byteVal = 10;
        System.out.println("byteVal: " + byteVal);

        int size = 100; // Decimal Literal
        System.out.println("size: " + size);

        int ab = 012345; // Octal literal.
        System.out.println("ab: " + ab);

        int hexadecimalValue = 0x4589ce; // Hexadecimal Literal
        System.out.println("hexadecimalValue: " + hexadecimalValue);

        int binaryValue = 0B1001; // Binary Literal
        System.out.println("binaryValue: " + binaryValue);
    }
}

Output

byteVal: 10
size: 100
ab: 5349
hexadecimalValue: 4557262
binaryValue: 9

e) Floating Literals: Floating literals are used to represent floating values i.e a decimal number. In java float and double are floating datatypes. By default, every floating type in Java is of type double. A number needs to be prefixed with F/f for it to be stored inside float datatype where as it’s not mandatory for double. It can or cannot be prefixed with F/f/

Program

public class FloatLiteral {
 
    public static void main(String[] args) {
 
        double doubleValue1 = 123.456;
        System.out.println("doubleValue1: " + doubleValue1);
 
        double doubleValue2 = 123.456D;
        System.out.println("doubleValue2: " + doubleValue2);
 
        double doubleValue3 = 123.456E2;
        System.out.println("doubleValue3: " + doubleValue3);
 
        double doubleValue4 = 123.456e+2d;
        System.out.println("doubleValue4: " + doubleValue4);
 
        double doubleValue5 = 123.455E-2;
        System.out.println("doubleValue5: " + doubleValue5);
 
        float floatValue1 = 12.34f;
        System.out.println("floatValue1: " + floatValue1);
 
        float floatValue2 = 1234567891011.1213F;
        System.out.println("floatValue2: " + floatValue2);
    }
}

Output

doubleValue1: 123.456
doubleValue2: 123.456
doubleValue3: 12345.6
doubleValue4: 12345.6
doubleValue5: 1.23455
floatValue1: 12.34
floatValue2: 1.23456795E12

Comments:

Avatar MirlCress says

comprar cialis 5 mg online steenceserly # - Cialis chiert Cialis Che Cos'E HeillaalleraFlut Standard Dose Of Keflex
Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.