JAVA Literals
-
Upload
ashutosh-trivedi -
Category
Software
-
view
68 -
download
0
Transcript of JAVA Literals
![Page 1: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/1.jpg)
ASHUTOSH TRIVEDI 1
Literals:
A constant value which can be assigned to the variable is called Literals.
![Page 2: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/2.jpg)
ASHUTOSH TRIVEDI 2
For the Integral data types (byte, short, int, long) the following are various ways to specify Literal value.
Decimal Literals :-
Allowed digit are 0 to 9 Example: int x=10;
Integral Literals:
![Page 3: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/3.jpg)
ASHUTOSH TRIVEDI 3
2) Octal Literals :-
Allowed digit are 0 to 7 Literal value should be prefixed with 0 [zero]
Example: int x= 010; 3) Hexadecimal Literals :-
Allowed digit are 0 to 9 , a to f [OR] A to F For the extra digits we can use both upper case & lower case this is one of very few places where java is not case
sensitive. Literal value should be prefixed with 0x [OR] 0X Example: int x= 0x10; [OR] Int x=0X10; These are the only possible ways to specify integral Literal.
![Page 4: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/4.jpg)
ASHUTOSH TRIVEDI 4
Example:
class TestLiterals{ public static void main(String args[]) { int x=10; int y=010; int z=0X10; System.out.println(x+"---"+y+"-----"+z); } }
Output:- (10)8 = (?)10
0*80 +1*81 =8 (10)16 = (?)10
0*160 +1*161 =1610---8-----16
![Page 5: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/5.jpg)
ASHUTOSH TRIVEDI 5
Que-) which of the following declarations are valid. 1.int x=10;2. int x=066;3. int x=0786; CE: integer number too large: 07864.int x=0XFACE; // 642065.int x=0xBea; // 3050
![Page 6: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/6.jpg)
ASHUTOSH TRIVEDI 6
By default every integral Literal is of int type but we can specify explicitly as long type by suffixing with l or L
Example;
1) int i=10; // Valid
2) int i=10l; CE: possible loss of precision int i=10l; ^ required: int
found: long
2) long l=10l; // Valid
3) long l=10; // Valid
![Page 7: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/7.jpg)
ASHUTOSH TRIVEDI 7
There is no way to specify integral Literal is of byte & short types explicitly.
If we are assigning integral Literal to the byte variable & that integral literal is within the range of byte then it treats as byte literal automatically. Similarly short Literal also.
byte b=10;
Valid
byte b=130;
CE: possible loss of precision int i=10l; ^ required: byte found: int
![Page 8: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/8.jpg)
ASHUTOSH TRIVEDI 8
By default the decimal point values represent double type & hence we cannot assign directly to float variable.
So if we want to assign the floating point values to the variables we must attach the suffix F or f to the number. If we are not providing the number we will get compilation error possible loss of precision.
Floating point literal & double literal:-
![Page 9: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/9.jpg)
ASHUTOSH TRIVEDI 9
Example-
float f=123.456;CE: possible loss of precisionFound: doubleRequired: float
float f=123.456f; // valid
double d=123.456; //valid
![Page 10: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/10.jpg)
ASHUTOSH TRIVEDI 10
We can specify floating point literal explicitly as double type by suffixing
with d or D.
Ex- double d=123.456D; // valid
float f=123.4567d; // InvalidCE: possible loss of precisionFound: doubleRequired: float
![Page 11: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/11.jpg)
ASHUTOSH TRIVEDI 11
We can specify floating point literal only in decimal form & we can’t specify in octal & hexadecimal form.
Example:
double d=123.456;(valid)double d=0123.456;(valid) //it is treated as decimal value but not octal double d=0x123.456; //C. E: malformed floating point literal(invalid)
![Page 12: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/12.jpg)
ASHUTOSH TRIVEDI 12
Which of the following floating point declarations are valid?
float f=123.456; //C.E:possible loss of precision(invalid)float f=123.456D; //C.E:possible loss of precision(invalid)double d=0x123.456; //C.E:malformed floating point literal(invalid)double d=0xFace; (valid)double d=0xBeef; (valid)
![Page 13: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/13.jpg)
ASHUTOSH TRIVEDI 13
We can assign integral literal directly to the floating point data types and that integral literal can be specified in decimal, octal and Hexa decimal form also.
Example:
double d=0xBeef;System.out.println(d);//48879.0
But we can't assign floating point literal directly to the integral types.
Example:
int x=10.0;//C.E:possible loss of precision
![Page 14: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/14.jpg)
ASHUTOSH TRIVEDI 14
A char literal can be represented as single character within single quotes.
Example:
char ch='a';(valid)char ch=a;//C.E:cannot find symbol(invalid)char ch="a";//C.E:incompatible types(invalid)char ch='ab';//C.E:unclosed character literal(invalid)
Char literals:-
![Page 15: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/15.jpg)
ASHUTOSH TRIVEDI 15
We can specify a char literal as integral literal which represents Unicode of that character.
We can specify that integral literal either in decimal or octal or hexadecimal form but allowed values range is 0 to 65535.
Example:
char ch=97; (valid)char ch=0xFace; (valid) System.out.println(ch); //?char ch=65536; //C.E: possible loss of precision(invalid)
![Page 16: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/16.jpg)
ASHUTOSH TRIVEDI 16
The only allowed values for the boolean type are true (or) false where case is important. i.e., lower case
Example:
boolean b=true; (valid)boolean b=0; //C.E:incompatible types(invalid)boolean b=True; //C.E:cannot find symbol(invalid)boolean b="true"; //C.E:incompatible types(invalid)
Boolean literal:-
![Page 17: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/17.jpg)
ASHUTOSH TRIVEDI 17
![Page 18: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/18.jpg)
ASHUTOSH TRIVEDI 18
The following 2 are enhancements
1. Binary Literals
2. Usage of '_' in Numeric Literals
• Binary Literals:
For the integral data types until 1.6v we can specified literal value in the following ways
1. Decimal
2. Octal
3. Hexa decimal
1.7 Version enhancements with respect to Literals:
![Page 19: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/19.jpg)
ASHUTOSH TRIVEDI 19
But from 1.7v onwards we can have specified literal value in binary form also.
The allowed digits are 0 to 1. Literal value should be prefixed with Ob or OB
int x = 0b111;System.out.println(x); // 7
![Page 20: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/20.jpg)
ASHUTOSH TRIVEDI 20
Usage of _ symbol in numeric literals:
From 1.7v onwards we can use underscore (_) symbol in numeric literals.
double d = 123456.789; //valid double d = 1_23_456.7_8_9; //validdouble d = 123_456.7_8_9; //valid
![Page 21: JAVA Literals](https://reader031.fdocuments.co/reader031/viewer/2022013004/587830aa1a28abef5d8b68fd/html5/thumbnails/21.jpg)
ASHUTOSH TRIVEDI 21
The main advantage of this approach is readability of the code will be improved at the time of
compilation ' _ ' symbols will be removed automatically, hence after compilation the above lines will become double d = 123456.789
We can use more than one underscore symbol also between the digits.
Ex: double d = 1_23_ _456.789;
We should use underscore symbol only between the digits.
double d=_1_23_456.7_8_9; //invaliddouble d=1_23_456.7_8_9_; //invaliddouble d=1_23_456_.7_8_9; //invalid