Gadgets

Monday, 2 February 2015

Java Literals



Literals:-
int i=10;
int – data type/keyword
i – name of variable/identifier
10 – literal/constant/value
Any constant value which can be assigned to the variable is called literal.
Integral Literals:-
We can specify literal values to the integral data types (byte, short, int, long) in the following 3 ways
Note:- JVM converts every value into decimal form value.
Decimal Form:-
Decimal form base 10(0-9) and it is allowed digits are 0 to 9.
int x=10;    int x=20;
Octal Form:-
Octal form base 8(0-7) and allowed digits are 0 to 7.
Literal value should be prefix with zero (0)
int x=010;     int x=020;
Hexadecimal form:-
Hexadecimal form base 16 (0-9 and ‘a’ to ‘f’), for extra digit we use lower case or upper case (a to f or A to F).
Literal value should be prefixed with 0x or 0X
int i=0x10;      int i=0x20;      int i=0X30;
Example 1:-
class Test{
public static void main(String arg[])
{
int a=10;
int b=010;
int c=0x10;
System.out.println("decimal : "+a+"  oct : "+b+"   hexa decimal : "+c);
}
}
Output:-
Example 2:-
int a=20, b=020,c=0x20;
By default every integral literal is ‘int’ type, if we want declare ‘long’ type we should suffixed with l or L.
int x=10;  correct
long y=10l;   correct
long z=10; correct
int i=10l;   Wrong(here ‘10l’ is long type so we can’t assign long type to the int type)
Example:-
class Test{
public static void main(String arg[])
{
int x=10;
long y=10l;
long z=10;
System.out.println("x value : "+x+"  y value : "+y+"   z value : "+z);
}
}
Output:-
There is no direct way to specify ‘byte’ and ‘short’ literal but when we are assigning integral to the ‘byte’ variable and if that value is with the range of ‘byte’ then compiler treats it automatically as ‘byte’ literal similarly ‘short literal also.
Example:-
byte a=10;  correct
byte a=127;  correct
byte a=128; wrong (it is out of the byte range, we will get compile time error)
short a=32767;   correct
short b=32768;  wrong (it is out of the short range, we will get compile time error)
Note:- these are the only possible ways to specify literal value for integral data types.
Floating Point Literals:-
By default every floating point literal is double type and hence we can’t assign directly to float variables but we can assign float type by using suffixed with f or F.
Note:- we can assign integer values to the float type and that integer value either in decimal or octal or hexadecimal. But we can’t assign float value to int type.
float f=123.456f;  correct
float x=0x123; correct(integer hexadecimal value)
float y=01234; correct(integer octal value)
float f=123;   correct
float f=123.456d;  wrong(here 123.456d is double type, we will get compile time error)
float f=123.456;  wrong(By default JVM treats every float value is double type so here 123.456 is double type, we will get compile time error)
Example 1:-
class Test
{
public static void main(String arg[])
{
float x=123.456f;
float y=123;
double z=123.456;
System.out.println("x value : "+x+" y value: "+y+" z value : "+z);}
}
Output
Example 2:-
class Test
{
public static void main(String arg[])
{
float x=123.456d;
float y=123.456;
System.out.println("x value : "+x+" y value: "+y);
}
}
Output
We can specify suffixed to the double type with d or D but this convention is not required.
Double d=123.456; correct
Double d=123.456d; correct (but suffix ‘d’ is not required)
Double d=123; correct
Example 3:-
class Test{
public static void main(String arg[])
{
double x=123.456d;
double y=123;
double z=123.456;
System.out.println("x value : "+x+" y value: "+y+" z value : "+z);
}
}
Output
 
We can specify floating point literal only in decimal form, floating point literals not allowed octal and hexadecimal form.
Double b=123.456; correct
Double b=0123.456; correct (0123.456 is not integer octal, it is just float value that value started with zero)
Double b=0x1234.567; wrong (here 0x123.456d is hexadecimal type, we will get compile time error)
Example of correct declaration:-
class Correct{
public static void main(String arg[]){
float a=10;
float b=10.10f;
float c=010;                                                          
float d=010.10f;
float e=0x10;
double f=10;
double g=10.10;
double h=010.10;
double i=0x10;
double j=10.10d;
System.out.println("..."+a+"..."+b+"..."+c+"..."+d+"..."+e+"..."+f+"..."+g+"..."+h+"..."+i+"..."+j);
}
}
Example of Wrong declaration:-
 Example 1:-
class Wrong{
public static void main(String arg[]){
float a=10.10;
float b=10.10d;
}
}
Example 2:-
float f=0x10.00;
Example 3:-
class Wrong{
public static void main(String arg[]){
double a=0x10.10;
double b=0x10.10d;
}
}

We can specify floating point values even in exponent ion form also known as scientific notation.
double d=1.2e3;
float f=1.2e3f;
Boolean Literal:-
The only allowed values for Boolean data type are true and false.
boolean b=true; correct
boolean b=false;  correct
boolean b="true";  wrong(here “true” is String type so Boolean not allowed it)
boolean b=True;  wrong(here ‘T’ is upper case so Boolean type is not allowed it)
boolean b=0;   wrong(here zero is int type so Boolean not allowed it)
Char Literal:-
We can specify ‘char’ literal as a single character with in single cot.
char ch=’a’;
char ch=a;
char ch=”a”;
char ch=’ab’;
Example 1:-
class Test {
public static void main(String arg[]) {
char ch='a';
System.out.println(ch);
}
}
Output:-
Example 2:-
class Test {
public static void main(String arg[]) {
char ch='ab';
System.out.println(ch);
}
}
Output:-
Example 3:-
class Test {
public static void main(String arg[]) {
char ch="a";
char ch1=a;
}
}
Output:-
We can represent char literal as integral literal which represent Unicode of that character that integral literal can be specified either in decimal or octal or hexadecimal form but allowed range is 0 to 65535
Example:-
class Test {
public static void main(String arg[]) {
char ch=97;
char ch1=0777;
char ch2=0xface;
char ch3=65535;
System.out.println("ch-->"+ch+" ch1-->"+ch1+" ch2-->"+ch2+" ch3-->"+ch3);
}
}
Output:-
We can represent char literal even in Unicode which is
‘\uXXXX’ with 4digit hexadecimal
Char ch=’\u0061’;   correct
Char ch=’\uface’;    correct
Char ch=’\iabcd’;   wrong (we should give \u not \i)
Char ch=\uabcd;    wrong (we missed single cot)

String Literal:-
We can specify String literal as sequence of characters with in double cot(“ “).
String s=”scjp”;
Java1.7 version enhanced literals:-
Binary literal:-
In java1.6 version for integral data types (byte,short,injt,long) we can specify literal values in the following ways
Decimal, octal, hexadecimal.
But in java1.7 version we can specify literals values in binary form also
Binary form allowed digits are 0 and 1. Should be prefixed with 0b or 0B
int x=0b1111;
Example:-
class Test {
static public void main(String arg[]) {
int i=0b0110;
System.out.println(i);
}
}
Output:-
0110 value is 6
In java1.7 version we can use underscore symbol between the digits of numeric literal
int x=123_4567_89;
The main advantage of this approach is readability of the code will be improved.
At the time of compilation the underscore symbol is remove automatically by the compiler hence after compilation the x value will become
int x=123456789;
We can add more than one underscore symbol also between the digits and we use underscore symbol only between digits. If we use any ware else we will get compile time error.
Example:-
class Test
{
public static void main(String arg[])
{
int i=12_3_45_678_90;
System.out.println(i);
}
}
Output:-

No comments:

Post a Comment