Literals in java define the actual value we are using for variables, constants or to perform any operation.
There are seven literals in java in which one literals is introduced from java 7 i.e binary literals. They are as follows:-
1> Boolean literals                  2> Character literals
3> String literals                      4> Integral Literals
5> Floating literals                   6> Null literals
7> Binary literals ( from java 7)

Lets discuss more in details about these literals one by one.

Boolean Literals 

For boolean literals there are two possible values to be used with the variables or the constants are true or false. In java we can't use " 0 " as a " false " value and " 1 " as a " true " value because in java any numeral value is considered as int type, that means 0 and 1 doesn't return any true or false value. But the same was true in case of other programming language like C where " 0 " return the " false " value and "1 " return the " true " value. This make java different from other programming language like C or C++ .
In short integral value can not be used as boolean in java and the default value of boolean is " false ".

Character literals

Character literals in java represents the representation of single character used within the single quote and these literals is used where char data type is used. There should be only one character available inside the single quote and must not be empty.

For ex-   char  c = 'A' ; // valid statement

We can also used ASCII with the char variable like,

char c = 65;
When we compiler the above statement the compiler convert it into the following statement,
char c='A';

So from the above statement we can conclude that ASCII value can be used with char variable which will be again internally converted to its respective character by the compiler.

Note: ASCII range is from 0-255 ( 8 bit ) but previously it was 0-127 ( 7 bit ) .

String literals

String is the collection of character or sequence of character which must be enclosed the double quotes. In java any value is within double quote treated as a string value.
String is a class defined in java programming language.
The default value of any string literal is NULL . String variable is a reference type whose size is 8 byte and will store the reference of the value defined as a string type.

For ex-  String str = " Java ". 

In the above statement str is a string variable of reference type which store the reference of the " Java " which is a string value. Storing reference means it will store the integral value where the value of the string variable is stored. But don't misunderstood that it is a address. It never be a address and hence just a system generated integral value . Java never deal in address with the user, but internally uses pointer and addresses.

Integral literals 

These literals represents the integral value and will be used in the following four data types,
byte,  short, int and long.
As per the size and data type used,  the value for the variable will be stored.
By default the ' type '  for integral literal will be int type and the default value is zero(0).
If you are using value of the variable as a long type then we have to append ' l ' or ' L ' at the end of the value.
for ex - long x = 12398765540987L;
Integral literals are represented in following three forms:-
1> Binary literals ( from java 7)
2> Octal literal
3> Decimal Literal.
4> Hexadecimal literals

Binary literals

It is the new concept introduced in java with the release of java 7. From java 7 we can be able to represent binary value to the integral literals with the use of 0 and 1.
To represent the data in binary the value must start with either ob or oB.

Octal literals

It is concept for representing the literal value in the octal form with the use of digits from 0 to 7.
To represent the data in octal from the value must start with a zero (0).
For ex-
int  x= 010;
010---> represented in octal form and represent 8 in decimal.
10----> it the decimal representation of the integral literals.

Decimal literals

It is the concept of representing decimal value to the variable with the use of 0 to 9.
for ex- int x =  10;

Hexadecimal literals

It the concept of representing the value in the hexadecimal format with the use of 0-9 , a/A to f/F .
To represent the hexadecimal number it must be start with 0x/0X.
for ex-
int x = 0x41; // 0x41 represent 65 in decimal .

Floating literals

These literals represents the numerical value can be used with two floating data type , they are float and double.

By default the it is double but if we are using any floating literal with float type then we have to explicitly mention ' f / F ' at the end of value.
for ex -
float x = 12.34f ; // Here it is float type 
float x = 12.34 ; // Here it is by default double type


Null literals

Null is a default value for any reference type. If the value of the reference type is null then it indicate no reference/address are available in the variable.

for ex-
String str=NULL; // indicate str dose not contain any reference but contain NULL.

3 comments:

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic