Sunday, February 8, 2009

java Literals


A constant value which can be assigned to a variable is known as Literal.If we are assigning any outside range value for any data type ,we will get a compile time error saying Possible Loss of Precision found int required byte.
For the integral data types (int ,byte,short,long) :we are allowed to specify a literal value of any any one of the following three forms.
---> Decimal literal (normal way)
--->Octa literal (prefixed with 0 )
--->Hexa decimal (prefixed with 0x )

int x=10 ------------> Decimal
int x=010 ------------>Octa
int x=0X10 -------------->Hexa
In the Hexa decimal notation for the extra digits we are allowed to specify either small or upper case a,b,c or A,B,C (this is one of the few places where java is not case sensitive).
Example:
class Sample {
public static void main(String add[]) {
int i = 10;
int j=010;
int k=0x10;
System.out.println( i+”….”+j+”…”+k);
} }
Output: 10,8,16
JVM gives only decimal form as output regardless of input as octa or hexa.
All the integral literals (decimal.octa,hexa) are by default int.
We can specify explicitly a long literal by suffixing l or L.
There is no direct way to specify the short,byte values.
Assigning chart (Possible assignments):
With out final compiler gives error .with final is possible because int has 4 bytes by declaring it final and that literal (10) is with in the size of byte.

int Literal Example:
class Literalone {
public static void main(String ass[]) {
int i='a';
byte b1='b';
char ch='b';
//byte b=ch; ---------->error (PLP found : char required: byte
System.out.println(i); -------------> output is 97
//System.out.println(b);
} }

floating Point Literal:
The floating point literals by default double.
Ex: float f=123.453; ----------> is invalid
Got error as PLP required :float found:double
A floating point literal can be specified as float type by suffixing with f or F
Example:
float f=128.453f or F (valid)
float f=123.456; (not valid)
double d=123.456; (valid)
double d=123.453f; (valid)
double d=123.345d or D; (valid)
A floating point literal can be explicitly double by suffixing with D or d;
float f=123.56d; (not valid)
A floating point literals must always specify in the decimal form only .there is no hexa decimal or octal form.
float f=123.0; (not valid)
float f=123; (valid)
float f= 0123; (valid)
float f=0x123; (valid)
We can assign an integral literal (decimal,octal,hexa) to the float data type directly (*no need to specify the f or F).
We can’t assign a floating point literal to the integral types.
int i=123.34 (not valid)

boolean Literal:
boolean valid literals are true or flase
boolean b=10; (not valid error PLP found:int reqired :boolean)
char literals:
A char literal can be specified as a single charter in single codes.
Ex: char ch=’@’; (valid)
char ch=a; (not valid)
char ch=’ad’; (not valid) error unclosed character literal.
An integral literal which represents the Unicode value of the character.
Ex: char ch=97; output: a
The valid integral literals must be from 0 to 65535.
for the Unicode values which are not sported by the system,we get ‘?’ as output.
char ch=0x(face) (valid)
char ch=ox61; output:1
The Unicode may be in octal /hexa decimal form.
char ch=97 ;
char ch= ‘a’;
char ch= 0x61;
char ch=’\uXXXX’; --------------------->Unicode representation
Ex: char ch=’\u0061’; output=a;
Unicode representation in nothing but \u followed by 4 digit hexadecimal number .(only small u,capital u is not allowed).
char ch=’\uface’; valid output: ?
char ch=’\ubeef’; valid
char ch=\ubeef; invalid ‘ ‘ missed.
char ch=’\ibeef’; invalid u missed.
Escape charater
Ex: char ch=’\b’; valid
char ch=’\t’; valid
char ch=’\n’; valid
char ch=’\f’; valid
char ch=’\k’; not valid
Escape sequences:
unicode value charater
\u0008 backspace
\u0009 horizental tab
\u000a new line
\u000d carriage return
\u000c form feed
\u0027 single quote
\u0022 double quote
\u005c back space

String literal:
Instead of \n and /r we are not allowed to the corresponding unicode charater \u000a and \u000d
Vailation leads to compail time error ever in the comments also.
String s=” laxman scjp”; (valid)
String s=”laxman \n scjp”; (valid)
String s=”laxman \t scjp”; (valid)
String s= “laxman \u0009 scjp”; (valid)
String s= “laxman \u000a scjp”; (not valid) gives error illegal escape character
String s=”laxman \u000d scjp”; (not valid) gives error illegal eacape chracter

0 comments:

Post a Comment