.


Divya

Java literal


Literal:

A constant value Which can be assigned to a variable is called Literal.

int a = 10;
Here,

  • int - is data type.
  • a - is variable.
  • 10 - is literal.

There is three type of Literal in Java.

  1. Integer Literals
  2. Floating-point Literals
  3. Character and String Literals

Integral Literal

  • To Initialize data type like byte, short, int and long Integer literals is used.
  • It can be expressed as decimal, hexadecimal and binary number systems.

Floating Point Literal

  • To Initialize data type like float and double Floating-point Literals is used.
  • We can specify floating-point literal only in decimal form.we can’t specify it in octal and hexadecimal forms.

Example

class Floating point literal {
public static void main(String[] args) {
double d = 8.5;
float f = 9.5F;
double e = 8.667e2;
System.out.println(d);
System.out.println(f);
System.out.println(e);
}
}

Output :
8.5
9.5
866.7

Character And String Literal

We can specify char literal as single chara cter within single quetes

char ch='a';

char ch="a";

Example

class Character and string literal {
public static void main(String[] args) {
char a = 'f';
char b= '\n';
String c = "java";
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}

Output :
f
null
java



.