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:-

Data types in java

In java every variable and every expression should has some type and every type is clearly define.
Each and every assignment should be checked by compiler for type compatibility hence java language considered as strongly typed programming language because type checking is very strong and type is very important.
Java is not considered as pure object oriented programming language because several OOP’s features are not supported by java (like operator overloading, multi threading, ect.) moreover we depending on primitive data types which are non objects.
Except Boolean and char remaining data types are considered as signed data types because we can represent both positive (+ve) and negative (-ve) numbers.
In java, there are 2 types of data types
 1. Primitive data types
2. Non primitive data types



byte:-
Byte is the best suitable, if we want to handle data in term of streams from the file or from the network
Size: 1byte (8bits), Range:  -128 to +127(-27 to +27-1)
+ve or _ve sign
26
25
24
23
22
21
20

64+32+16+8+4+2+1=127
Note:- the most significant bit as the sign bit ‘0’ mean +ve and ‘1’ mean –ve
byte b=10;  //Correct         byte b=127;  //Correct
byte b=128;  //Wrong(out of the byte range)   
Compile time error (jdk6 version):- possible loss of precision
                                                             found: int
                                                             required: byte

byte b=10.5; Wrong(it is float type value so we can’t assign float type to the byte type)
compile time error:- possible loss of precision
                                      found: double
                                      required: byte
byte b=true;  Wrong(‘true’ is Boolean type not byte type)
compile time error:- incompatible types
                                      found: boolean
                                      required: byte
byte b=”java”;  Wrong(“java” is String type not byte type)
compile time error:- incompatible types
                                      found: double
                                      required: byte



Note:- In jdk6 version we get an error message as like below
found:int
requied:byte
In jdk7 version we get an error message as below:
required:byte
found:int

short:-
The mostly rarely used data type in java is ‘short’. This data type is best suitable for 16bit processor like 8086 processor but these processors are outdated and hence the corresponding ‘short’ data type also outdated data type.
Size: 2bytes (16 bits), Range: -32768 to +32767(-215 to +215-1)
Example:-
class Test{
public static void main(String arg[]){
short a=32767;  //Correct
short b=32768; // Wrong(value is out the short range)
short c=10.3;  // Wrong(value is float type, we can’t assign it to the short type)
short d=true;// Wrong(value is boolean type, we can’t assign it to the short type)

}
}



int:-
The most commonly used data type in java is ‘int’.
Size: 4byte (32bits), Range: -2147483648 to +2147483647(-231 to +231-1)
Example:-
class Test{
public static void main(String arg[]){
int a=2147483647;  //Correct
int b=2147483648L; // Wrong
int c=10.3;  // Wrong
int d=true;  // Wrong
int e=”hello”;  // Wrong
}
}



long:-
Sometimes ‘int’ may not enough to hold big value then we should use ‘long’ data type.
Size: 8byte (64bits), Range: -263 to 263-1
Example:-
To hold amount of distance travelled by light in 1000daysat that time ‘int’ may not enough so we should use ‘long’ data type.
long l=126000*60*60*24*1000miles;
To hold number of characters in a big file ‘int’ may not enough then we use type of length() method.
long l=f.length();
Floating point data types:-
Note:- by default every floating point literal is double type, we can’t assign directly to float type. We can assign float type by suffixed with f or F;
float f=123.456; //Wrong(we should specify suffixed with f or F to the float value like 123.456f)
float f=123456; //Correct
double b=123.456;  //Correct
float f=123.456f; or float f=123.456F;    //Correct
Float:-
If we want declare 5 to 6 decimal points to a number then we should take ‘float’ data type.
Size:-  4 bytes(32bits), Range:-  -3.4e38 to 3.4e38.
Example:-  float f=10.01f;
Double:-
If we want declare 14 to 15 decimal points to a number then we should take ‘double’ data type.
Size:- 8byte(64bits), Range:-  -1.7e308 to 1.7e308.
Example:- double b=10.01;
Boolean:-
Size:- size is not applicable(virtual machine dependency).
Range:- not applicable but it allowed values are true or false.
Examples:-
boolean b=true;        //Correct
boolean b=True;      // Wrong(here ‘T’ is upper case so Boolean type is not allowed it)


boolean b=0;     //Wrong(zero is number, we can’t assign it to boolean)



boolean b=”true”;     //Wrong (“true” is string type, we can’t assign it to Boolean type)



Example:-  
class Test1
{
public static void main(String arg[])
{
boolean b=0;
if(b){
System.out.println("hello");
}
else{
System.out.println("hi");
}
}
}


Char data type:-
Old languages(c,c++) are ASCII code bits and the number of different allowed ASCII characters are less than or equal 256(<=256) to represent these characters 8 bits(1byte) are enough hence the size ‘char’ in old language is 1 byte(8bits).
Java Unicode based and the number of Unicode characters are greater than 256 and less than or equal 65536(>256 and <=65536) to represent these characters 16bits (2byte) are required in java and 8bits (1byte) not enough.
Size:-  2byte (16bits)    Range:-  0 to 65535.
Summary table of java primitive data types:-
Data type
Size
Range
Corresponding wrapper class
Default value
byte
1byte
-27 to 27-1
 (-128 to 127)
Byte
0
short
2bytes
-215 to 215-1
(-32768 to 32767)
Short
0
int
4bytes
-231 to 231-1 (-2147483648 to 2127483647)
Integer
0
long
8bytes
-263 to 263-1
Long
0
float
4bytes
-3.4e38 to 3.4e38
Float
0.0
double
8bytes
-1.7e308 to 1.7e308
Double
0.0
boolean
Not applicable
Not applicable but allowed values are true and false
Boolean
False (in ‘C’ language ‘true’)
char
2bytes
0 to 65535
Character
0(represent space character)

Note:- ‘null’ is the default value for object reference, we can’t use ‘null’ value for primitive data types. If we use ‘null’ value for primitive data types we will get compile time error.
Example:-
class Test
{
public static void main(String arg[]){
int i=null;
}
}
Output:-