Skip to main content

Java literals:



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 in 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.

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

Instead of \n and /r we are not allowed to the corresponding Unicode character \u000a and \u000d
Validation leads to compile time error ever in the comments also.


String literal:
String s=” Sekhar 4 J scjp”; (valid)
String s=” Sekhar 4 J \n scjp”; (valid)
String s=” Sekhar 4 J \t scjp”; (valid)
String s= “Sekhar 4 J \u0009 scjp”; (valid)
String s= “Sekhar 4 J \u000a scjp”; (not valid) gives error illegal escape character
String s=” Sekhar 4 J \u000d scjp”; (not valid) gives error illegal escape character

Comments

Popular posts from this blog

Yahoo! Calendar "Add Event" Seed URL Parameters

I can't seem to find any official documentation on this, so here are my notes. Some information gathered from  http://richmarr.wordpress.com/tag/calendar/ Other information gathered through trial and error, and close examination of the "Add Event" form on Yahoo!'s site. Yahoo! Calendar URL Parameters Parameter Required Example Value Notes v Required 60 Must be  60 . Possibly a version number? TITLE Required Event title Line feeds will appear in the confirmation screen, but will not be saved. May not contain HTML. ST Required 20090514T180000Z Event start time in UTC. Will be converted to the user's time zone. 20090514T180000 Event start time in user's local time 20090514 Event start time for an all day event. DUR value is ignored if this form is used. DUR 0200 Duration of the event. Format is HHMM, zero-padded. MM may range up to 99, and is converted into hours appropriately. HH values over 24 hours appear to be modulated by 24. Durations t...