Literals are a value which you can assign to the variable or a constant.
There are five types of literals and are listed below:
It can be assigned to one of the integer data types; there are three types of integer literals.
Decimal integer literals: Base 10 [0-9]
Octal integer literals (starts with 0) [0-7]
Hexadecimal integer literals (0-9, A-F)
int a=123;
int a=0123;
(starting with zero)
int a=0x123A;
(starting with zero & x)
It can be assigned to floating point variables declared with float or double datatypes.
There are two types of representation:
Decimal pointer presentation
Exponential representation
double d16 = 99.99;
double d12 = 99.99e-5; // 99.99e-4
double d15 = 9.9E3; // (9.9*10*10*10)
It is a single character which is enclosed between single quotes ‘ ’
‘a’ ‘+’ ‘ ’
‘ ’ // invalid (because of space)
char ch1='a';
Each character which is enclosed in single quotation marks will have integer equivalent’s value as per ASCII character set as shown in the example below:
‘a’ = 97, ‘b’ = 98, ------------- , ’y’ = 121, ’z’ =122
‘A’ = 65, ‘B’ = 66, ------------- , ‘Y’ = 89, ‘Z’ = 90
‘0’ = 48, ‘1’ = 49, ------------- , ‘8’ = 56, ‘9’ = 57
Java uses the character set UNICODE (Universal Code)
UNICODE character set takes two bytes of memory for each character and supports multi languages.
ASCII character set takes only one byte character and supports only English language.
There are two boolean literals:
True
False
Boolean literals can be assigned to the variables which are declared with boolean data type.
boolean b=true;
It is a collection of one or more characters enclosed between double quotation marks.
String literals can be assigned to reference variable of type String.
Example:
class Lab1 {
String s1 = "hello guys";
String s2 = "123 guys as d";
String s3 = " “;
public static void main(String as[]) {
int a = 10;
int $b = 20;
// int 1c=30;
// int ab=40;
// int 1=10;
System.out.println(-a);
System.out.println($b);
}
}
Output:
-10
20
Lab2.Java
package com.javabykiran.basics;
class Lab2 {
byte b; // variable & constants demo
short s; // 0
int i; // 0
long l; // 0
float f; //0.0
double d; // 0.0
char ch; // for 1 character’s blankspace
boolean b1; // false
void show1() {
System.out.println("variables & constants demo");
System.out.println(b);
System.out.println(d);
System.out.println(s);
System.out.println(ch);
System.out.println(i);
System.out.println(b1);
System.out.println(l);
System.out.println(f1);
System.out.println(f);
// System.out.println (x);x cannot resolved to a variable
// x=x+1
}
void show2() {
System.out.println("integer literal demo");
int a = 12;
int b = 10;
int c = 0x12a; // integer literal demo
Byte d = 012;
System.out.println(a); // 12
System.out.println(b); // 10
System.out.println(c); // 298
System.out.println(d); // 10
}
void show3() {
System.out.println("floating point literal demo");
float f1 = 99.97f;
double d1 = 9.9e-9;
double d2 = 9.9E+9;
System.out.println(f1); // 99.97
System.out.println(d1); // 9.9E-9
System.out.println(d2); // 9.9E9
}
void show4() {
System.out.println("character literal demo");
char ch1 = 'A';
int x1 = 'A';
// char ch2 = ''; // error invalid character constant
//int x2 = '' ; // error
char ch3 = 'I';
int x3 = 'I';
System.out.println(ch1); // A
System.out.println(x1); //65
// System.out.println (ch2); // error +
// System.out.println (x2); // error +
System.out.println(ch3); // I
System.out.println(x3); // 73
}
void show5() {
System.out.println("Boolean literal demo");
boolean bb1 = false;
boolean bb2 = true;
System.out.println(bb1); // false
System.out.println(bb2); // true
}
void show6() {
System.out.println("string literal demo");
String ste1 = "";
String ste2 = "123 abc+";
String ste3 = "1";
System.out.println("ste1 " + ste1); // ste1- - "blank"
System.out.println(ste1.length());
System.out.println("ste2 = " + ste2); // ste2=123 abc +
System.out.println(ste2.length()); // 9
System.out.println("ste3=" + ste3); // ste = 1
System.out.println(ste3.length()); // 1
}
}
package com.javabykiran.basics;
class Lab2Test {
public static void main(String[] args){
Lab2 h = new Lab2();
h.show1();
h.show2();
h.show3();
h.show4();
h.show5();
h.show6();
}
}
Output:
variables & constants demo 0
0.0
0
Blank
0
false
0
10.9
0.0
integer literal demo
12
10
298
10
floating point literal demo
99.97
9.9E-9
9.9E9
character literal demo
A
65
I
73
Boolean literal demo
false
true
string literal demo
ste1
0
ste2 = 123
abc + 9
ste3 = 1
1