print
Advertisment
Advertisment

Literals

Any constant value which can be assigned to the variable is called literal.

we have 4 types of literals available in java

  1. Integral Literals
  2. Floating point Literals
  3. Charector literals
  4. Boolean literals
  5. Object literals/Non primitive literals

Integral literals

For the integral data types (byte, short, int and long) we can specify literal value in the following ways.
  1. Decimal
  2. Octal
  3. Binary
  4. Hexa Decimal

Decimal literals:

Allowed digits are 0 to 9, It is default literal for integral data type. & it should write normal digits like dacimal

Example

	
	class Test{
		public static void main(String[] args) {
			int number=204;  //204 is a literal and its a dacimal type literal.
			System.out.println(number);
		}
	}
	

Output :

	
	
	204
	

Octal literals:

Allowed digits are 0 to 7 & it should be prefixed with 0.

Example

	
	class Test{
		public static void main(String[] args) {
			int number=0154;  //204 is a literal and its a dacimal type literal.
			System.out.println(number);
		}
	}
	

Output :

	
	
	108
	

Hexa Decimal literals:

  1. Allowed digits are 0 to 9,A to F
  2. For the extra digits we can use both upper case and lower case characters
  3. This is one of very few areas where java is not case sensitive
  4. Literal value should be prefixed with 0x (or) 0X

Example

	
	class Test{
		public static void main(String[] args) {
			int number=0x154;  //204 is a literal and its a dacimal type literal.
			System.out.println(number);
		}
	}
	

Output :

	
	
	340
	

Binary literals:

Allowed digits only 0 & 1 & it should be prefixed with 0b.

Example

	
	class Test{
		public static void main(String[] args) {
			int number=0b101;  //204 is a literal and its a dacimal type literal.
			System.out.println(number);
		}
	}
	

Output :

	
	
	5
	
Advertisment

Floating point literals

Floating point literal is by default double type but we can specify explicitly as float type by suffixing with f or F.

Example:

	
	class Test{
		public static void main(String[] args) {
			double number1=0x123;
			double number2=123.456;
			System.out.println(number1);
			System.out.println(number2);
		}
	}
	

Output :

	
	
	291.0
	123.456
	

Note: We can specify floating point literal only in decimal form and we can't specify in octal and hexadecimal forms.

Example:

	
	class Test{
		public static void main(String[] args) {
			double number2=0x123.456;// malformed floating point literal
			double number2=0b101.456;//';' expected
			System.out.println(number2);
		}
	}
	

Output :

	
	
	error: malformed floating point literal
	                        double number2=0x123.456;
	                                       ^
	error: ';' expected
	                        double number2=0b101.456;
	                                            ^
	2 errors
	
Advertisment

Character Literal

A char literal can be represented as single character within single quotes.

Note: We can specify a char literal as integral literal which represents Unicode of that character.
We can specify that integral literal either in decimal or octal or hexadecimal form but allowed values range is 0 to 65535.

Example:

	
	class Test{
		public static void main(String[] args) {
			char ch1 = 'A';
			char ch2 = 44;
			System.out.println(ch1);
			System.out.println(ch2);
		}
	}
	

Output :

	
	
	A
	,	
	

Note: We can use Binary Octal and Hexa decimal also insted of dacimal number

Example:

	
	class Test{
	public static void main(String[] args) {
			char ch1 = 0x0041;
			System.out.println(ch1);
		}
	}
	

Output :

	
	
	A	
	

Note: We can represent a char literal by Unicode representation which is nothing but ‘\uxxxx' (4 digit hexa-decimal number)
Every escape character in java acts as a char literal.

Example:

	
	class Test{
		public static void main(String[] args) {
			char ch1='\u0061';
			char ch2='\n'; // it will work as a charector wich is changin the line
			char ch3='\u0071';

				System.out.print(ch1);
				System.out.print(ch2);
				System.out.print(ch3);
		}
	}
	//as you can see we are not changin line using println method th ch2 variables literal is responsible to change the line;
	

Output :

	
	
	A	
	

Boolean Literal

The only allowed values for the boolean type are true (or) false & only lower case is valid.

Example:

	
	class Test{
		public static void main(String[] args) {
			boolean b1 = true;
			boolean b2 = false;
				System.out.println(b1);
				System.out.println(b2);
		}
	}
	

Output :

	
	
	true
	false	
	

Note: if we try for the uppercase literals then we will get compile time error

Example:

	
	class Test{
	public static void main(String[] args) {
			char ch1 = 0x0041;
			System.out.println(ch1);
		}
	}
	

Output :

	
	
error: cannot find symbol
                boolean b1 = True;
                             ^
  symbol:   variable True
  location: class Test
error: cannot find symbol
                boolean b2 = False;
                             ^
  symbol:   variable False
  location: class Test
2 errors
	

Note: true & false are also known as a reserved literals in java.


String literals

Any sequence of characters with in double quotes is treated as String literal.

Example:

	
	class Test{
		public static void main(String[] args) {
			String st  = "Hi I am a String literals";// this is a String literal
				System.out.println(st);
		}
	}
	

Output :

	
	
	Hi I am a String literals
	
Advertisment
Advertisment
arrow_upward