Literals
Literal is the value that we pass in the variables. In other words, Literals are syntactic representation of boolean,numeric,character and string type. These values can be fixed or constant and are stored in the variable.Generally literal are categorized into two types i.e. Numeric Literals and Non Numeric Literals.
Integer Literal : They includes - int, short, long, byte.
These are the value without any decimal.
Int is default type of integer. if any value is out of range in integer literal then compiler will consider that value as int and generates an error.
To represent any value in long L/l will be used at the end.
There are 4 integer Literals type i.e. Decimal, Octal, Hexadecimal, Binary(added in java 7)
- Decimal Literals has base 10 i.e from 0 to 9.
Example:
int a = 111;
it will print 111
- Octal Literals has base 8 i.e. from 0 to 7.
Example:
int a = 0111;
i.e. 1*82+1*81+1*80 = = 64+8+1 = 73
- Hexadecimal Literals has base 16 from 0 to 9 and a to f.
Example:
int a = 0x111;
i.e. 1*162+1*161+1*160 = = 256+16+1 = 273
- Binary Literal has base 2 i.e. 0,1.
Example:
int a = 0b111;
i.e. 1*22+1*21+1*20 = 4+2+1 = 7
Floating Point Literal : They includes - float and double.
Default value of Floating Literals is double.
For assigning any value to float f / F will be used at the end.
Floating Point Literals stores values in two parts i.e. values and exponential.
Its representation will not allows with Octal and Binary.
Floating Point Notation:
- Standard Notation - for eg. 456.19
- Exponential/Scientific Notation - 45619E-2
- Hexadecimal Notation - 0x45619p0
Character Literals : It include - char.
There are five ways to assign values -
- Single character with single quote.
- ASCII values
- Unicode
- Escape Sequence
- Octal
- In single character values, only one character will be assigned to a variable,that must be enclosed with single quotes.
- ASCII value will returns the integer ASCII code. In java every value that enclosed with a single quote will have an integer value. ASCII values range lies in 0 - 255
- Escape Sequence is a combination to two characters. First character will be backslash ( \ ), which is used to print specific characters. they are also known as White Space Characters. i.e. \' , \'', \t, \n, \b, \f, \r, \\
- In Unicode, value is always started from \u followed by four hexa decimal digits. It is stands for Universal Code.Whenever compiler find Unicode in the program it will converts it to its corresponding value.
- In Octal, value is always started from backslash ( \ ).value must be enclosed with single quotes. Octal values range lies in \0 - \377.
In the above diagram: Decimal represents - ASCII values, Hex represents - Unicode value(in \u0020 format), Oct represents - Octal values, Character represents - Character values.
Boolean Literals : They includes only true and false values, that can be assigned only to compatible type. true, false values will always be in lower case because they are predefined values that cannot be changed.
String Literals : It include - str.
It is a collection of 0 and more characters in double quotes.They are represent as char array in java memory.
String also consider Escape sequence, Unicode, Octal values.
Example:
String s = "Welcome to my blog.";
- Null and Empty String :
No value assigned to string will be represent as Empty String.
Null value assigning to string will be represented as Null String.In this object will not be created and reference variable contains null value.
Example:
String s = ""; //Empty String.
String s1 = null; //Null String.
It is also a default value for String variable.
very impressive post,helpful to a much greater extent.
ReplyDelete