Header Ads

What is literals in java

What is literals in java

What is literals in java




Literals:

A literal in java is any constant value which can be assigned to the variable.


Example:

int c=10

Here,

10 is constant value| literal

c is name of variable identifier

int is datatype | keyword




Integral Literals:

For the integral data types (long,int,short and byte) we can specify literal value in the following ways.


1) Decimal literals: Permitted digits are 0 to 9.

Example: int x=10;




2) Octal literals: Permitted digits are 0 to 7. Literal value should be prefixed with zero.

Example: int x=010;




3) Hexa Decimal literals:

1. The allowed digits are 0 to 9, A to Z.

2. For the extra digits we can use both upper case and lower case characters.

3. This is one of very few areas where java is not case sensitive.

4. Literal value should be prefixed with ox(or)oX.


Example: int x=0x10;


These are all  possible ways to specify integral literal.


Which of the following are valid declarations?

1. int c=0777;     //(valid)

2. int c=0786;     //C.E:integer number too large: 0786(invalid)

3. int c=0CFACE;   //(valid)

4. int c=0cbeef;   //(valid)

5. int c=0cBeer;   //C.E:';' expected(invalid) //:int c=0xBeer; 

6. int c=0cabb2ed; //(valid)





Example:

int x=10;

int y=010;

int s=0x10:

System.out.println(x+"----"+y+"----"+z);          //10----8---16

By default in java every integral literal is int type but we can specify explicitly as long type by suffixing with small "l" (or) capital "L".

Example:

int x=10;         //(valid)

long 1=10L;       //(valid)

long l=10;        //(valid)

int x=101;        //C.E:possible loss of precision (invalid)


In Java,there is no direct way to specify byte and short literals explicitly. But whenever,the integral literal is assigned to the byte variables and its value within the range of byte compiler automatically treats as byte literal. Similarly short literal also.

Example:

byte k=127;      //(valid)

byte l=130;      //C.E:possible loss of precision (invalid)

short q=32767;   //(valid)

short j=32768;  //c.E:possible loss of precision (invalid)








Floating Point Literals:

In Java,Floating point literal is by default double type but we can specify explicitly as float type by suffixing with f or F.

Example:

float f=123.456;  //C.E:possible loss of precision (invalid)

float f=123.456f; //(valid)

double d=123.456; (valid)




Floating point literal can be explicitly specify as double type by suffixing with d or D.


Example:

double d=123.45 6D;



In Java, floating point literal can be specified only in decimal form and we cannot specify in octal and hexadecimal forms.

Example:

double d=123.456;       //(valid)

double d=0123.456;      //(valid) //it is treated as decimal value but                                                             not octal

double d=0x123.456;     //C.E:malformed floating point literal                                                     (invalid)




Which of the following floating point declarations are valid in java?

1. float f=123.456;      //C.E:possible loss of precision(invalid)

2. float f=123.456D;     //C.E:possible loss of precision(invalid)

3. double d=0x123.456;   //C.E:malformed floating point literal(invalid)

4. double d=0xFace;      //(valid)

5. double d=0xBeef;      //(valid)



In Java, integral literal directly can be assigned to the floating point data types and that integral literal can be specified in decimal, octal and Hexadecimal form also.


Example:

double d=0xBeef;   

System.out.println(d);      //48879.0

But we cannot assign floating point literal directly to the integral types.



Example:

int k=10.0;                //C.E:possible loss of precision



Floating point  literal can be specified even in exponential form also(significant notation).


Example:

double d=10e2;                 //---->10*10^2 (valid)

System.out.println(d);         //1000.0

float f=10e2;                  //C.E:possible loss of precision (invalid)

Float f=10e2F;                 //(valid)













Boolean literals:

The only permitted values for the boolean type are true (or) false where case is important.i.e. lower case.



Example:

1. boolean h=true;         //(valid)

2. boolean i=0;              //C.E:incompatible types(invalid)

3. boolean j=True;        //C.E:cannot find symbol(invalid)

4. boolean k="true";     //C.E:incompatible types(invalid)









Char literals:

1) In Java,a char literal can be represented as single character within single quotes.


Example:

1. char ch='a';      //(valid)

2. char ch=a;        //C.E:cannot find symbol(invalid)

3. char ch="a";      //C.E:incompatible types(invalid)

4. char ch='ab';     //C.E:unclosed character literal(invalid)

2)  A char literal can be specified as integral literal which represents Unicode of that character.


We can specify that integral literal either in decimal or octal or hexadecimal form but allowed valuces range is 0 to 65535.


Example:

1. char ch=97;             //(valid)

2. char ch=0xFace;         //(valid)

System.out.println(ch);    

3. char ch=65536;          //C.E: possible loss of precision(invalid)


3) A char literal can be represented by Unicode representation which is nothing but

"\uxxxx' (4 digit hexa-decimal number).


Example:

1. char ch='\ubeef';

2. char chl='\u0061';

System.out.println(chl);    //a

3. char ch2=\u0062;         //C.E:cannot find symbol

4. char ch3='\iface';       //C.E:illegal escape character

5. Every escape character in java is a char literal.


Example:

1) char ch='\n';           // (valid)

2) char ch='\1';           //C.E:  illegal escape character (invalid)




Which of the following char declarations are valid?

1. char ch=a;               //C.E:cannot find symbol(invalid)

2. char ch='ab';           //C.E:unclosed character literal(invalid)

3. char ch=65536;      //C.E:possible loss of precision(invalid)

4. char ch=\uface;      //C.E:illegal character: \64206(invalid)

5. char ch='/n';           //C.E:unclosed character literal(invalid)

6. none of the above.  (valid)












String literals:

Any sequence of characters with in double quotes is considered as String literal.


Example:

String s="Ashok";     (valid)



1.7 Version enhancements with respect to Literals:


The following 2 are enhancements

1. Binary Literals

2. Usage of '_' in Numeric Literals













Binary Literals :

For the integral data types until 1.6 version we can specified literal value in the following ways

1. Decimal

2. Octal

3. Hexadecimal




But from 1.7 version onwards we can specified literal value in binary form also.

The allowed digits are 0 to 1.

Literal value have to be prefixed with Ob or OB.

int t = Ob111;

System.out.println(t);   // 7







Usage of _symbol in numeric literals :

From 1.7v onwards we can use underscore( ) symbol in numeric literals.


1. double d = 1_23_456.7_8_9;    //valid.

2. double d =123_456.7_8_9;      //valid

The readability of the code will be improved with these approach.At the time of compilation '_'symbols will be removed automatically, hence after compilation the above lines will become double d = 123456.789


In Java,more than one underscore symbol can be used between the digits.

Ex : double d =1_23_ _456.789;

 Underscore symbol can be used only between the digits


double d= 1_23_456.7_8_9;        //invalid

double d=1_23_756.7_9_;           //invalid

double d=1_23_456_.7E_9;         //invalid

double d='a';

System.out.println(d) ;          //97

integral data types

float f=1OL;

System.out.println(f) ;          //10.0

floating-point data types





Diagram:


byte short--->short and char--->int---->long----->float------>double

(1 byte)          (2 byte)           (4 byte)   (8 byte)    (4 byte)      (8 byte)



Today's inspirational quotes: 

"Believe in yourself,you will be unstoppable."




Steps to Subscribe my blog in 15 secs:
1. Enter your mail id in "Subscribe" box.
2. Enter Captcha for verification.
3. A mail will be received with link in your mail id.Just Click on link  and  then Done .Thank you for subscribing :)

Happy Reading.Happy Learning!!! See you in next post :)

No comments

Note: Only a member of this blog may post a comment.