Literals in Java With Example

Write an Example program to illustrate what are the different kinds of literals in java.

Program or Solution

				
				
					

class Program

{

public static void main(String args[])

{

//variable declaration and definition

int num = 10;

double num_f = 12.5;

char ch = 'D';

String name = "Decode";

boolean b = true;

String s = "true";


//printing using println statement

System.out.println(num);

System.out.println(num_f);

System.out.println(ch);

System.out.println(name);

System.out.println(b);

System.out.println(s);


}

}


Program Explanation

Literals are the values which specified literally (directly) in the program. It is hard-coded. it remains same.

Here 10 is integer literal, "Decode" is String Literal, 12.5 is Float Literal, true is literal, "true" is string literal. 'D' is character literal.