Any Constant value which can be assigned to a variable.
Example: int x=10;
Here, int represents Data Type/Key Word, x represents Variable/Identifier, 10 represents Constant/Literal.
Integral Literal:
For Integral data types (byte, short, int , long) we can specify literal value in the following ways.
1.Decimal Literals (Base 10)
Allowed digits are 0 to 10
Example: int x=10;
2.Octal Literal (Base 8)
Allowed digits are 0 to 8
Literal value should be prefixed with zero(0).
Example: int x=010;
3.Hexadecimal Literal( Base 16)
Allowed digits are 0 to 9, a to f
Literal value should be prefixed with 0X or 0x
Example 1 : int x=0X10;
For extra digit ( a to f ) , we can use both lower case and upper case character. This is one of very few area where java is not case sensitive.
These are only possible ways to specify literal values for integral data types.
Q. Which of the following declarations are valid?
int x=10; (valid)
int x=0786; (in valid C.E. Integer number too large)
int x=0777; (valid)
int x= 0XFace; (valid)
int x=0XBeef; (valid)
int x=0XBeer; (C.E ; expected)
Example 2:
class Test{
public static void main (String [] args){
int x=10;
int y=010;
int z=0X10;
System.out.println(x+“ ”+y+” ”+z);
}
}
o/p- 10 8 16
Note: If we are giving the values as decimal, octal, hexadecimal then by default JVM internally converts all the values in to decimal form and gives the o/p as decimal form.
- By default every integral literal is of int type, but we can specify explicitly as long type by suffixed with l or L.
int x=10; (valid)
long l=10L; (valid)
int x=10L; C.E Possible loss of Precesion required int, found long
long l=10; (valid as int value can be stored in long variable i.e 32 bit value can be stored in 64 bit allocating variable)
- There is no direct way to specify byte and short literals explicitly, but indirectly we can specify. When ever we are assigning integral literal to the byte variable and if the value with in the range of byte, then compiler treats it automatically as byte literal. Similarly short literal also.
byte b=10; valid
byte b=127; valid
byte b=128; Possible loss of Precision found int, required byte
short s=32767; valid
short s=32768; Possible loss of Precision found int, required short
Floating Point Literal:
By default every floating point literal is of double type and hence we can't assign directly to the float variable.
But we can specify floating point literal as float type by suffixed with f or F.
Example:
float f=123.456; //possible loss of Precision found double ,required float
float f=123.456f; //valid
double d=123.456; //valid
we can specify explicitly floating point literal as double type by suffixed with d or D.
Of course this convention is not required.
Example:
double d=123.456d;
float f=123.456d; // C.E possible loss of precision found double , required float.
We can specify floating point literals only in decimal form and we can't specify in octal and hexadecimal forms.
Example:
double d=123.456; //valid
double d=0123.456; // valid as this is the decimal literal not octal literal as 0 before floating value is not considered and considered as decimal.
double d=0X123.456 //C.E. Malformed floating point literal
We can assign integral literal directly to floating point variables and that integral literal can be specified either in decimal, octal or hexadecimal forms.
Example:
double d=0786; //invalid, integer value too large
double d=0XFace; // valid
double d=0786.0; //valid
double d=0XFace.0; // invalid
double d=0777; //valid
double d=10; //valid
We can't assign floating point literals to integral types.
Example:
double d=10; // valid
int x=10.0; // possible loss of precision found double , required int
We can specify floating point literal even in exponential form(scientific notation)
Example:
double d=1.2e3; // valid and o/p=1200.0 ( since 1.2*10^3)
float f=1.2e3; // C.E possible loss of precision found double, required float.
float f=1.2e3; // valid
boolean Literal:
The only allowed values for boolean data type are true or false.
Examples:
boolean b=true; (valid)
boolean b=0; (invalid, gives compile time error, “incompatible type”, found int, required boolean)
boolean b=True; (invalid, gives compile time error, “can't find symbol”, class Test)
boolean b=”True”; (invalid, gives compile time error, “incompatible type”, found java.lang.String, required boolean)
Ex.1 Ex. 2
class Test{ class Test{
public static void main(String []args){ public static void main(String []args){
int x=10; while(1){
if(x){ System.out.println(“Hello”);
System.out.println(“Hello”); }
} }
else{ }
System.out.println(“Hello”);
}
}
}
These two programs are valid in case of c & c++, but in java it gives error.
Here both the program will give compilation error “Incompatible Types ” found int, required boolean. Because if and while loop condition needs a boolean value only in case of java, and as java is strongly typed programming language this is not supported in java.
char Literal:
We can specify char literal as single character with in single quotes.
Example:
char ch='a'; // valid
char ch=a; // C.E. Can't find symbol, symbol: variable a, class Test
char ch=”a”; // C.E. Incompatible type found java.lang.String required char
char ch='ab'; //C.E 1 unclosed charcter literal
//C.E 2 unclosed charcter literal
//C.E 3 not a java statement
We can specify char literal as integral literal which represents UNICODE value of the character and that integral literal can be specified either in decimal or octal or hexadecimal forms, But allowed range is 0 to 65535.
Example:
char ch=97; System.out.println(ch); // o/p- a
char ch=0XFace; //valid
char ch=0777; // valid
char ch=65535; //valid
char ch=35536; // Invalid C.E. Possible loss of precesion, found : int required :char
Note: follow www.unicode.org to get all the respected symbol of characters between 0 to 65535.
We can represent char literal in UNICODE representation which is nothing but '\uXXXX' where XXXX stands for 4 digit hexadecimal number.
Example:
char ch='\u0061'; System.out.println(ch); // o/p- a
Every escape character is a valid char literal.
Example:
char ch='\n' // valid
char ch='\t' // valid
char ch='\m' // Invalid C.E Illegal Escape character
Escape characterDescription1 \nNew Line2 \tHorizontal Tab3 \rCarriage Return4 \bBack Space5 \fForm Feed6 \'Single Quote7 \''Double Quote8 \\Back Slace
Q. Which of the following are the valid char literal?
char ch=65536; //Invalid due to out of Range
char ch=0XBeer; //Invalid due to r symbol
char ch=\uface; // Invalid due to single quote missed
char ch='\m'; //Invalid C.E. Illegal Escape character
char ch='iface'; //Invalid due to i symbol
char ch='ubeef'; //valid
String Literal:
Any sequence of characters with in double quotes is treated as String Literal.
Example:
String s=”purnachandra”;
1.7 Enhanced features to Literals:
Binary Literal:
For integral data types until 1.6 versions we can specify Literal values in the following ways.
1. Decimal form
2. Octal form
3. Hexadecimal form
But from 1.7 version on wards we can specify Literal values even in binary form also.
Allowed digits are 0 and 1..
Literal value should be prefixed with 0b or 0B.
Example:
int x=0B1111; System.out.println(x); // o/p- 15
Usage of _(underscore) in literal:
From 1.7 version on wards we can use _ symbol between digits of numeric literals.
Example:
double d= 123456789;
double d=1_2_3_456.7_8_9; //Indian standard
double d=123_456.7_8_9; //US standard
The main advantage of this approach is the readability of the code will be improved.
At the time of compilation these underscore symbols will be removed automatically, Hrnce after compilation the above lines ll become
double d=123456.789;
We can use more than one underscore symbol also between the digits.
double d= 1___23_4_5___6.7_8_9;
We can use the underscore symbol only between the digits .If we are using any where else we ll get compile time error.
double d=_1_23_456.7_8_9; //Invalid
double d=1_23_456_/7_8_9; //Invalid
double d= 1_23_456.7_8_9_; //Invalid
Note:
1. 8 byte long value we can assign to 4 byte float variable because both are following different memory representation internally.
float f=10 l;
System.out.println(f); o/p- 10.0
2. char and short data types both occupy same 2 bytes memory space , even though we can't able to store char value in short data type, because char does n't have signed value (-ve value), but byte contain signed values. So the maximum value of char(65535) cant be stored in byte variable.
No comments:
Post a Comment