Previous | Home | Next |
Literals in Java are a sequence of characters (digits, letters, and other characters) that represent constant values to be stored in variables. In Java programming language there are some special type of literals that represent numbers, characters, strings and boolean values. A literal represent a constant value which we cant change . A literal is a value specified in the source code it is not determined at run time.
- Java integers are stored internally as binary numbers, but can appear in source programs as ordinary decimals. Java integer literals are by default constant of int type.
- 5.23, -5.23, 5.4f, 5.4F, 5.4d, 5.4D, 1.2E-03 are floating point literals where f or F is for single precision (float ), d or D is for double precision , 5.23 is a standard notation and 1.2E-03 is in exponent notation (scientific notation ) where 1.2 is mantissa and -03 is exponent.
- 'a', 'A', '\n', '\141', '\u0061' are character literals in which '\n' is an escape sequence.'\141' is octal code for character 'a' and '\u0061' is hexa-decimal code for character 'a'. Java characters are sixteen bit Unicode characters, ranging from 0 to 65535.
- "matrix " is a string literal. String is a group of characters. in java string there is no line continuation escape sequence as there in other language. So string must begin and end on the same line, in java string are of object type ."\" this is in quotes"\". In Java a string is not a basic data type, rather it is an object. There are few methods provided in Java to combine strings, We represent string literals as:
- true, false are boolean literal .true and false are not eqial to 1 or 0 in java. we can't convert a boolean value to integer and vice-versa.
Types of Literals
5,-5 , 0x5A, 012,5l, 5L are integer literals in which 5 ,-5 are decimal ,0x5A is a hexadecimal, 012 is a octal, l or L is for long.
Java integer literals are by default constant of int type,
Java integer literals can be sequences of decimal digits, optionally preceded by a minus sign for negative integers.
Example :
15 -1253
Java integer literals cannot contain any blanks or commas.
Example :
45 12 // error occurs no space between integers 45,12 // no comma between the numbers
There should be no blank space between the minus sign (-) and the number for negative integers.
Example :
- 1253 // error occurs no space between - sign and number
class Num { public static void main(String[] args) { int decimal = 100; // 100 decimal. int octal = 0144; // decimal 100 represented in octal base. int hex = 0x64; // decimal 100 represented in hexadecimal base. int bin = 0b1100100; // decimal 100 represented in binary base System.out.println(decimal); // Prints '100' System.out.println(octal); // Prints '100' System.out.println(hex); // Prints '100' System.out.println(bin); // Prints '100' } }
Example :
class Flow { public static void main(String args[]) { float a=0.000000000000000000123f; System.out.print(a); } }
output :
1.23E-19
Example :
class A { public static void main(String args[]) { char ch='a'; if(ch>=97 && ch<=122) { ch=(char)(ch-32); } System.out.println(ch); } }
output :
A
String str = "How are you?";
Example :
char[] a = { 'h', 'e', 'l', 'l', 'o', '.'}; String helloStr = new String(a); System.out.println(helloStr);
Example :
public class A { public static void main(String[] args) { boolean Sign = true; System.out.println(Sign); } }
Previous | Home | Next |