Java Literals - The Coding Shala
Home >> Learn Java >> Java Literals
Java literals are constant values that we assigned to the variables called literals or constant. For example -
Any Whole number value is an integer literal. For example 1,2, 3, 4, 32, etc all are integer literals. In java decimal values(base 10), octal(base 8) and hexadecimal(base 16) can be used as integer literals.
In Java, Octal values are denoted by a leading zero and range is 0 to (base 8). For example 05, 06, 045, etc. are octal literals and 09 will give an error it's not octal(9 is not in range) or decimal.
In Java Decimal literals cannot have a leading zero and range is 0 to 9(base 10). For example 10, 25, 78 etc.
In Java, Hexadecimal literals start with leading zero-x (0x or 0X) and the range of a hexadecimal digit is 0 to 15(base 16). For 10 to 15 we use A to F(or a to f) like 10 is represented by A, 11 is B and 15 is F. Examples of hexadecimal literals are 0x023A, 0X09C, etc.
If literals start with 0b or 0B then it is binary literals and allowed digits are 0 and 1. For examples 0b1111,0B1001 etc.
In Java, integer literals create an int value. Whenever a literal value is assigned to a byte or short no error will generate if a literal value is within the range of the type. To assign integer literal to a long variable we have to append 'L' or 'l' to the literal. For example, 2327462373286l, 0x2248ADL, etc. we can also assign an integer literal to char within its range like char a = 67;
Floating-point numbers(real numbers) have decimal values with a fractional component. In Java, floating-point literals to double precision. To specify a float literal we have to append 'F' or 'f' to the literal. We can also specify a double literal by appending 'D' or 'd' to the literals. For examples-
Boolean literals can have two values true and false. In Java, the value true is not equal to 1 and value false is not equal to the 0. Boolean literals can only be assigned to the variables declared as a boolean data type. For example -
In Java, characters are represented by Unicode. A literal character is represented inside a pair of a single quote like char a = 'A' or can be converted into integers and we can use integer operators on that, such as addition and subtraction operators. For char data type we can specify literals in the following ways -
We can specify literals to char as a single character inside a pair of a single quote.
we can specify char literal as integral literal within its range(0 to 65535). These integral literals can be specified in decimal, octal, and hexadecimal forms.
We can specify char literals in Unicode representation. To represent in Unicode we use '\uxxxx'. Here xxxx represents 4 hexadecimal numbers.
The escape character can be specified as char literals. The following is Character Escape Sequence -
Any sequence of characters within double quotes is treated as String literals. Examples of string literals are -
The following Java Program will explain literals -
Point to Remember: We can use underScore(_) in numeric Literals. If our number contains many digits, we can use an underscore character to separate digits.
Other Posts You May Like