0% found this document useful (0 votes)
6 views

3.core Java Literals

Core Java Literals

Uploaded by

ms.madhu27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

3.core Java Literals

Core Java Literals

Uploaded by

ms.madhu27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Leela Soft Core Java (J2SE) Madhusudhan

Literals in Java:
• In programming, a literal represents a value directly in the source code.
• Java provides five types of literals.

Types of Literals:
• Integer Literals
• Floating point Literals
• Character Literals
• Boolean Literals
• String Literals

Integer Literals:
• Represent whole numbers.
• Can be in binary (base 2), octal (base 8), decimal (base 10) and hexadecimal (base 16)
format.

Number System:
1. Binary (allowed from JDK 7)
2. Octal
3. Decimal
4. Hexadecimal

Decimal:
Allowed digits: 0-9, not start with 0

Octal:
Allowed digits: 0-7, starts with 0

Hexadecimal:
Allowed digits: 0-9, a-f or A-F
Prefix with: 0x or 0X
Here a/A = 10, b/B = 11, …

Binary:
Allowed digits: 0, 1
Prefix with: 0b or 0B

Example:
class Test
{
public static void main(String[] args)
{
int x = 123; //decimal
int y = 012; //octal

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

int z = 0xA; //hexadecimal


int a = 0Xa; //hexadecimal
int b = 0b1010; //binary

System.out.println("Decimal : "+x);
System.out.println("Octal : "+y);
System.out.println("HexaDecimal : "+z);
System.out.println("HexaDecimal : "+a);
System.out.println("Binary : "+b);
}
}

Floating Point Literals:


• Represent decimal numbers with fractional parts.
• Can be specified in decimal or scientific notation.

Decimal Form: 123.325


Exponential Form: 12.3e34, 12.3e+34, 12.3e-34

Example:
class Test {
public static void main(String[] args) {
double x = 12.3;
double y = 12.3e3; // 12.3*10**3
double z = 12.3e+3; // 12.3*10**3
double a = 12.3e-3; // 12.3/10**3

System.out.println("Decimal : " + x);


System.out.println("Exponential : " + y);
System.out.println("Exponential : " + z);
System.out.println("Exponential : " + a);
}
}

Character Literal:
• Represent single characters enclosed in single quotes.
• Can be a character literal or an escape sequence

class Test {
public static void main(String[] args) {
char x = 'N'; // Single Character

System.out.println("Single Char : " + x);


}
}

Example:

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

class Test {
public static void main(String[] args) {
char x1 = 'N'; // Single Character
char x2 = '/'; // Single Character
char x3 = '"'; // Single Character
char x4 = '\\'; // Single Character
char x5 = '\''; // Single Character

System.out.println("Single Char : " + x1);


System.out.println("Single Char : " + x2);
System.out.println("Single Char : " + x3);
System.out.println("Single Char : " + x4);
System.out.println("Single Char : " + x5);
}
}

Boolean Literals:
• True or False value representation.

Example:
class Test {
public static void main(String[] args) {
boolean x1 = true;
boolean x2 = false;

System.out.println("Boolean Type : " + x1);


System.out.println("Boolean Type : " + x2);
}
}

String Literals:
• Enclosed with in the double quotes.

Example:
class Test {
public static void main(String[] args) {
String x1 = "Java";
String x2 = "Python";

System.out.println("String Type : " + x1);


System.out.println("String Type : " + x2);
}
}

Example:
class Test {
public static void main(String[] args) {

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

String x1 = "Java'";
String x2 = "Python\"\"";
String x3 = "Python\\";

System.out.println("String Type : " + x1);


System.out.println("String Type : " + x2);
System.out.println("String Type : " + x3);
}
}

Escape sequence Characters


Example:
class Test {
public static void main(String[] args) {
String x1 = "Java\bJava";
String x2 = "\n";
String x3 = "Java\tPython";
String x4 = "Java\rPython";
String x5 = "Java\fPython";
String x6 = "\'";
String x7 = "\"";
String x8 = "\\";

System.out.println("String Type : " + x1);


System.out.println("String Type : " + x2);
System.out.println("String Type : " + x3);
System.out.println("String Type : " + x4);
System.out.println("String Type : " + x5);
System.out.println("String Type : " + x6);
System.out.println("String Type : " + x7);
System.out.println("String Type : " + x8);
}
}

[email protected] Cell: 78 42 66 47 66

You might also like