What is Literals in Java?
A Constant value which can be assign to the variable is called "Literal"
int x = 10;
here ,
int is Data Types / Keyword.
10 is Constant value.
x is name of variable/ identifier
Integral Literals:- for the integral datatypes (byte , short , int ,long) the following are various ways to specify literal value
1. Decimal Literal:
In a literal decimal literal allowed digit are 0 to 9
Example-
int x = 10;
2. Octal Literals:
- Allowed digit are 0 to 7
- literals value should be prefixed with '0' [Zeo]
- int x = 010;
3. Hexadecimal Literals;
- Allowed digit are 0 to 9, a to f & A to F.
- For the extra digit we can use both uppercase and lowercase this is one of very few places where Java is not case sensitive
- Literal value should be prefixed with ox or OX.
- int x = ox10;
- int x = OX10;
- These are the only possible ways to specify integral literal
class Test{
public static void main(String [] args){
int x= 10;
int y= 010;
int z= OX10;
system.out.println(x+ "..."+y+ "..."+z);
}
}
- There is no way to specify integral literal is of byte & short types explicity.
- if we are assigning integral to the byte variable & that integral literal is with in the range of byte then it treats as byte literal automatically , similarlly short literal also.
- byte b = 10;
- byte b = 130; // found :int || required:byte
Floating Point Literals:
- Every floating point literal is bydefoult double type & hence we can't assign to float variable .
- But we can specify explicity floating point literal is the float type by suffixing with 'F' or 'f'.
float f = 123.456; //this is required float but found double
float f = 123.456f;
double id = 123.456
- We can specify floating point literal explicity as double type 9 by suffixing with d or D.
- double d = 123.456D;
- We can specify floating point literal only in decimal form & We can't Specify in Octal & Hexa decimal form.
- double d = 123.456;
- double d = 0123.456;
- double d = ox123.456; //This is wrong
- We can assign integral literal directly to hte floating point datatypes & that integral literal can be specified either in deimal from or odtal form or hexa decimal form.
- But we can not assign floating point literal directly to the integral types.
- int i = 123.456; // this is wrong , found: double || required: int
- double d = 1.2e3;
- We can specify floating point literal even in Scientiefic form also [ exponetial form]
- double d = 1.2e3; s.o.pln(d) ; //1200.0
Boolean Literals:
The only possibles values for the boolean datatypes true/false.
In Java, a boolean variable can only hold two values: `true` or `false`. Assigning `0` to a boolean variable like `boolean b = 0;` is not valid and will result in a compilation error.
If you attempt to compile the following code:
boolean b = 0;
You would receive a compilation error similar to:
error: incompatible types: int cannot be converted to boolean
To assign a value to a boolean variable, you need to use either `true` or `false`. For example:
boolean b = true;
or
boolean b = false;
If you want to store an integer value in a variable, you should use an `int` or another appropriate numeric data type, not a boolean.
Question 1. Consider the following code:
Class Test
{
public static void main(String [] args)
{
while(1)
{
System.out.println("hello'');
}
}
}
A. hello will be print infinite numbers of times.
B. hello will be printed only once
C. Compilation fails
D. An exception is thrown at runtime
Ans. C
Explaination
while condition shlould be boolean type.The only allowed values for boolean data types are: true and false,where case is important . But we provided int type. hence we will get compile time error.
Error: incompatible types: int cannot be converted to boolean while(1)
Char Literals:-
A char literals can be represented as single character with in single codes
Example
char ch= 'a';
A char Literals can be represented as a integer literal which represent unicode of that characters.
We can specify integer literal either in decimal form or octal form or hexxa decimal form. But allowed range 0 to 65535
Ex.
char ch = 97;
S.o.pln(ch); //a is output
A char literal can be represented in unicode representation which is nothing but \Uxxx 4-digit hexa decimal no.
Example.
char ch = '\u0061';
S.o.pln(ch); // a
String s = "Java";
Immutability: String literals are immutable, which means their values cannot be changed once they are assigned. If you want to modify a string, you need to create a new string object.
Concatenation: String literals can be concatenated using the + operator, allowing you to combine multiple strings into a single string. Java provides the concat() method as an alternative for string concatenation.
Tags:
Java

