Literals in Java



A literal represents a constant value which can be assigned to the variables



Integral Literal

We can specify an integral literal in the following ways.
Decimal literals: allowed digits are 0 to 9

Ex: int x = 10;

Octal literals: allowed digits are 0 to 7 but here literal value should be prefixed with 0(zero)

Ex: int x = 010;

Hexadecimal literals: the allowed digits are 0 to 9, A- F (Both lower, Upper case) literals should be prefixed with 0x or oX

Ex: int x = 0x10;


Ex:

class Test 
{
    public static void main(String arg[]) 
    {
        int x = 10;
        int y = 010;
        int z = 0x10;
        System.out.println(x + "..." + y + "..." + z);
    }
}

O/P:- 10…8…16

Except decimal, octal, hexadecimal there is no other way to represents constant values for the integral datatype. By default every integral lateral is of int datatype we can specify explicitly. An integral literal is of long type by suffixing with l or L.

Ex:
10  int value.
10l  long value.
long l = 10l;
int i = 10l;

C.E: possible loss of precision found : long
Required:int

There is no way to specify explicitly an integral literal is of type byte and short.
If the integral literal is with in the range of byte then the JVM by default treats it as byte literal.
Similarly short literal also.

Floating – point literals

By default floating-point literals are double type we can specify explicitly as float type by suffixing with ‘f’ or ‘F’.

Qus- Which of the following are valid declarations

A. float f = 10.5;  
B. float f = 10.5f;
C. double d = 10.5;
D. double d = 10.5f;
E. double d = 10.5D;

Ans- B,C,D,E

we can specify explicitly a floating point literal of double type by suffixing with d or D. we can also represent float-point literals by using scientific notation.
Ex:
double d = 10e23;
int i = 10e23; // C.E possible loss of precision found : double required : int.

Floating point literals can be specified only in decimal form. i.e we can’t use octal and hexa decimal representation for floating point literals.

Ex:
Double d = 0x123.456;  //C.E: Malformed floating-point literal.

Qus- Which of the following are valid declarations
A. float f = 123.456;
B. float f = 0x123.456F;
C. float f = 0x123;
D. float f = 1.2e36;
E. double d = 1.2e36;

Ans- B,C,E

Boolean Literals

The only allowed values for boolean datatype are true, false.

Qus- Which of the following are valid declarations
1. boolean b = true;
2. boolean b = FALSE;
3. boolean b = 0;

Ans- 1

character literal

A char literal can be represented as a single character with in single quotes.
Ex:

char ch = 'a';
char ch = 'ab'; //C.E: unclosed character literal.
char ch = a;

we can represent a char literal by using it’s Unicode value. For the allowed Unicode values are 0 to 65535.

Ex:
char ch = 97;
System.out.println(ch); // O/P: a
char ch = 65535;
char ch = 65536; // C.E : possible loss of precision found : int required :char

we can represent a char literal by using Unicode representation which is nothing but \uxxxx’

Ex:

char ch = '\u0061'
System.out.println(ch);  O/P:a
char ch = '\ubeef';
char ch = '\uface';
char ch = '\iface';
char ch = '\uface';

we can also represent a char literal by using escape character.

Ex:
char ch = '\b';
char ch = '\n';
char ch = '\l';

The following is the list of all possible escape characters in java.

\b  backspace
\n   new line
\r   carriage return
\f   formfeed
\t   horizontal tab
\’   single quote
\”   double quote
\\   back slash


No comments:

Post a Comment