# Literals in Java

Literal is representation of value, for example 23,23,345 are numeric literal. To represent constant value literals are used.

### 1. Integer literal

Number without decimal are integer literals. If a value ends with 'L' or 'l' then it is treated as long literal. byte, short, int and long can be assigned integer literals. Decimal number system is used mostly but in java literal can also be from different number system.

• Binary Number System :

To assign a value in binary form to a variable, precede the value by 0b.

```int a = 0b1100; // 12 will be assigned to variable a
```

• Octal Number System :

To assign a value in octal number system, precede the value by 0.

```int a = 012; // 10 will be assigned to variable a
```

To assign a value in Hexa number system, precede the value by 0x.

```int a = 0x12; // 18 will be assigned to variable a
```

Printing values in different number system

```//file:literal.java
//www.jgyan.com
class literal
{
public static void main(String arg[])
{
int a=121;
System.out.println("binary of 121"+Integer.toString(a,2));
System.out.println("binary of 121"+Integer.toString(a,8));
System.out.println("binary of 121"+Integer.toString(a,16));
}
}
/*Output:
binary of 121=1111001
binary of 121=171
binary of 121=79
*/
```

### 2. Floating point literals

By default, floating point literals are double data type. To make any value for float variable 'f' need to be placed after literal. 1.23f represent float literal and 1.23 represent double literal (Default). For double 'd' or 'D' can be used to specify it explicitly.

```  float a = 4.3f;
double d=34.34;
double e=43.2222d;
double 2=4.32222e1;
```

Floating point literal can be expressed in scientific notation(E or e), in single precision by using 'f' (32bit) or in double precision by using 'd'.

### 3. String and char literals

Java has unicode in use. String literal can be any sequence of unicode characters. Every character take 2 bytes in java. Unicode character can be used directly in code by using unicode escape sequence '\u0061'(a).

Program written below using unicode escape sequence. Before parsing the program in tokens compiler first replace unicode with it's respective character.

```  //file:literal1.java
//www.jgyan.com
class literal
{
public static void main(String arg[])
{
int \u0061=121;// this is equivalent to int a=121;
System.out.println(a);
}
}
```