# Tutorials for Java ,Core java ,Advance java ,oracle Durga sir Harikrishna Murali Sir

Tutorials for Java ,Core java ,Advance java ,oracle ,HTML, CSS, JavaScript, Java and other programming language tutorial Learn Java tutorial for beginners with examples in eclipse online. Java tutorial point for core java, servlet, jsp, struts, spring, for learning web technologies online. Content includes tutorials and references relating to HTML, CSS, JavaScript, PHP, AngularJS, SQL, Bootstrap, Node.js, and jQuery this is a website like w3school tutorialspoint etc

Core Java (33)

# Literals In Java

### Literals In Java

A literal represents a constant value which can be assigned to the variables

### Integral Literal

We can specify an integral literal in the following ways.

### Decimal literals:

allowed digits are 0 to 9    Ex: int x = 10; Octal literals: allowed digits are 0 to 7 but here literal value should be prefixed with 0(zero)    Ex: int x = 010;

the allowed digits are 0 to 9, A- F (Both lower, Upper case) literals should be prefixed with 0x or oX  Ex: int x = 0x10;

Ex:  class Test {

public static void main(String arg[])   {

int x = 10;
int y = 010;
int z = 0x10;

System.out.println(x + "..." + y + "..." + z);
} }

O/P:- 10…8…1

Except decimal, octal, hexadecimal there is no other way to represents constant values for the integral datatype.

By default every integral lateral is of int datatype we can specify explicitly. An integral literal is of long type by suffixing with l or L.

Ex:
10 --int value.
10l -- long value.

long l = 10l;
int i = 10l;

C.E: possible loss of precision found : long     Required:int

There is no way to specify explicitly an integral literal is of type byte and short. If the integral literal is with in the range of byte then the JVM by default treats it as byte literal. Similarly short literal also.

### Floating – point literals

By default floating-point literals are double type we can specify explicitly as float type by suffixing with ‘f’ or ‘F’.

### Which of the following are valid declarations

A. float f = 10.5; (Wrong) C.E possible loss of precision
B. float f = 10.5f;  (Right)
C. double d = 10.5;  (Right)
D. double d = 10.5f;  (Right)
E. double d = 10.5D;  (Right)

we can specify explicitly a floating point literal of double type by suffixing with d or D. we can also represent float-point literals by using scientific notation.

Ex:

double d = 10e23;

int i = 10e23;  C.E possible loss of precision found : double         required : int.

Floating point literals can be specified only in decimal form. i.e we can’t use octal and hexa decimal representation for floating point literals.

Ex:   Double d = 0x123.456;
C.E: Malformed floating-point literal.

### Which of the following are valid declarations

A. float f = 123.456;  (Wrong)
B. float f = 0x123.456F; (Right)
C. float f = 0x123; (Right)
D. float f = 1.2e36;  (Wrong)
E. double d = 1.2e36; (Right)

Boolean Literals The only allowed values for boolean datatype are true, false.

Which of the following are valid declarations

1. boolean b = true;  (Right)
2. boolean b = FALSE;   (Wrong)
3. boolean b = 0;   (Wrong)

### character literal

A char literal can be represented as a single character with in single quotes.

Ex:
char ch = 'a';  (Right)
char ch = 'ab';  (Wrong)           C.E: unclosed character literal.
char ch = a;  (Wrong)

we can represent a  char literal by using it’s Unicode value. For the allowed Unicode values are 0 to 65535.
Ex:

char ch = 97;
System.out.println(ch); - O/P: a

char ch = 65535;
char ch = 65536; - C.E : possible loss of precision found : int        required :char

we can represent a char literal by using Unicode representation which is nothing but     \uxxxx’

Ex:

char ch = '\u0061'
System.out.println(ch); - O/P:a
char ch = '\ubeef';  (Right)

char ch = '\uface'; (Right)
char ch = '\iface';   (Wrong)
char ch = '\uface';    (Wrong)

we can also represent a char literal by using escape character.

Ex:

char ch = '\b';  (Right)
char ch = '\n'; (Right)
char ch = '\l';  (Wrong)

The following is the list of all possible escape characters in java.

\b- backspace
\n - new line
\r - carriage return
\f -formfeed
\t - horizontal tab
\’ - single quote
\”- double quote
\\ -  back slash

### Which of the following char declarations are valid?

char ch = 'c'; (Right)
char ch = 'abc';   (Wrong)
char ch = a ;   (Wrong)
char ch = 65123; (Right)
char ch = 65537;  (Wrong)
char ch = \uabcd;   (Wrong)
char ch = '\uanuska';  (Wrong)
char ch = '\ubeef'; (Right)
char ch = '\r'; (Right)
char ch = '\t';(Right)
char ch = '\d';    (Wrong)

### String literal

A sequence of character with in double quotes is String literal.

String s = "Durga";(Right)
String s = 'software'; (Wrong)
String s = 'a';    (Wrong)