When programming in Java, you will often come across the term "literals." In simple terms, literals are values that are directly written into your code. They represent fixed values and can be of various types, including integers, floating-point numbers, characters, booleans, and strings. In this article, we will explore different types of literals in Java and how to use them in your code.
Integer literals represent whole numbers and can be written in three different formats:
1. Decimal (base 10): These literals consist of a sequence of digits without any prefix. For example:
int myNumber = 42;
2. Binary (base 2): These literals are prefixed with 0b or 0B followed by a sequence of binary digits (0 or 1). For example:
int binaryNumber = 0b1010; // Represents the decimal value 10
3. Hexadecimal (base 16): These literals are prefixed with 0x or 0X followed by a sequence of hexadecimal digits (0-9, A-F, or a-f). For example:
int hexNumber = 0xFF; // Represents the decimal value 255
Floating-point literals represent decimal values with a fractional part. They can be written in two different formats:
1. Decimal: These literals consist of a sequence of digits, including a decimal point. For example:
double myDouble = 3.14;
2. Scientific notation: These literals use the letter E or e to represent the exponent of 10. For example:
double scientificNotation = 2.5e3; // Represents the decimal value 2500
3. Character Literals: Character literals represent single characters and are enclosed in single quotes ('). For example:
char myChar = 'A';
Special characters can be represented using escape sequences. Some common escape sequences include:
4. Boolean Literals: Boolean literals represent the values true or false. For example:
boolean myBoolean = true;
5. String Literals: String literals represent a sequence of characters and are enclosed in double quotes ("). For example:
String myString = "Hello, world!";
Strings can also be concatenated using the + operator. Here's an example:
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
System.out.println(fullName); // Output: John Doe
1. Write a program to calculate the area of a circle given its radius. (Use floating-point literals)
public class CircleArea {
public static void main(String[] args) {
double radius = 5.0;
double area = Math.PI * radius * radius;
System.out.println("Area of the circle: " + area);
}
}
Output
Area of the circle: 78.53981633974483
2. Write a program to convert a binary number to its decimal equivalent. (Use binary and integer literals)
public class BinaryToDecimal {
public static void main(String[] args) {
int binaryNumber = 0b10101;
int decimalNumber = 0;
int base = 1;
while (binaryNumber > 0) {
int lastDigit = binaryNumber % 10;
binaryNumber = binaryNumber / 10;
decimalNumber += lastDigit * base;
base *= 2;
}
System.out.println("Decimal equivalent: " + decimalNumber);
}
}
Output
Decimal equivalent: 21
In conclusion, literals in Java allow you to directly represent fixed values in your code. They come in different types, such as integers, floating-point numbers, characters, booleans, and strings. By understanding and utilizing literals effectively, you can write more expressive and meaningful code.
60 videos|37 docs|12 tests
|
|
Explore Courses for Software Development exam
|