Literals In Java: Types Of Literals In Java [With Examples] - UpGrad
Maybe your like
1. Incorrect Use of Underscores in Numeric Literals
Java allows underscores to improve readability in numeric literals. However, the placement of underscores must be correct to avoid errors.
Common Mistake: Placing underscores at the beginning, end, or next to a decimal point.
int number = _123456; // Incorrect: underscores cannot be placed at the beginning int number2 = 123_456_; // Incorrect: underscores cannot be at the end float pi = 3._14f; // Incorrect: underscores cannot be adjacent to the decimal pointExplanation: Java allows underscores only between digits to improve readability, but they cannot appear at the beginning, end, or next to a decimal point (e.g., 3._14f is invalid).
Correct Usage:
int number = 123_456; // Correct: underscores between digits float pi = 3.14_15f; // Correct: underscores placed in the middle long bigNumber = 1_000_000L; // Correct: large numbers can use underscoresExplanation: The underscores must be placed only between digits to make the number more readable (e.g., 1_000_000).
2. Confusing Decimal and Octal Literals (Before Java 7)
Before Java 7, numeric literals that began with 0 were interpreted as octal (base-8) values. This can cause unexpected results when working with numbers.
Common Mistake: Using a leading zero in a number, which is interpreted as an octal literal.
int octal = 0123; // Incorrect: Octal literal (before Java 7)Explanation: 0123 is treated as an octal literal (base 8), which gets converted to 83 in decimal. This could be confusing if you intended to use a decimal value.
Correct Usage:
int decimal = 123; // Correct: Decimal literal int octal = 0o123; // Correct: Java 7 and later - octal literals with prefix 0oExplanation: Starting a literal with 0o (or 0O) specifies it as an octal number in Java 7 and later. This avoids ambiguity.
3. Incorrect Use of Character Literals
Character literals must contain only one character. Multiple characters inside single quotes will result in a compilation error.
Common Mistake: Using more than one character inside single quotes.
char letter = 'AB'; // Incorrect: A character literal can only contain one characterExplanation:'AB' is invalid as a character literal because Java expects only one character inside single quotes.
Correct Usage:
char letter = 'A'; // Correct: Single character literal String message = "AB"; // Correct: Use double quotes for stringsExplanation: Use single quotes for a single character ('A'), and double quotes for a string ("AB").
4. Incorrect Suffix for Floating-Point Literals
By default, floating-point literals are treated as double. If you intend to use them as float, you must add the f or F suffix.
Common Mistake: Using a floating-point literal without the f suffix when assigning it to a float.
float pi = 3.14; // Incorrect: The literal is treated as doubleExplanation: 3.14 is treated as a double by default. Trying to assign it to a float without the f suffix results in a type mismatch.
Correct Usage:
float pi = 3.14f; // Correct: Add 'f' suffix to denote a float literalExplanation: The f suffix is required to explicitly specify a float literal.
5. Misunderstanding Boolean Literals
In Java, only true and false are valid boolean literals. Any other values, such as "1" or "yes", will result in errors.
Common Mistake: Assigning a non-boolean value to a boolean variable.
boolean isActive = "true"; // Incorrect: "true" is a string, not a booleanExplanation: "true" is a string, not a boolean. Java only accepts the lowercase literals true and false for booleans.
Correct Usage:
boolean isActive = true; // Correct: 'true' is the valid boolean literalExplanation: Use true or false as boolean literals in Java.
6. Using null with Primitive Types
null is a special literal used to represent the absence of an object reference. It cannot be assigned to primitive types like int, boolean, or double.
Common Mistake: Attempting to assign null to a primitive type.
int num = null; // Incorrect: `null` cannot be assigned to primitive typesExplanation: null can only be assigned to reference types (like String), not primitives like int, boolean, or char.
Correct Usage:
String name = null; // Correct: `null` can be assigned to reference typesExplanation: null is only valid for reference types such as String, Integer, or Object.
7. Inconsistent Use of Uppercase and Lowercase for Booleans
Java is case-sensitive, so only true and false (in lowercase) are valid boolean literals.
Common Mistake: Using True or False with uppercase letters.
boolean isTrue = True; // Incorrect: "True" is not a valid boolean literalExplanation: Java boolean literals must be in lowercase. True and False are invalid.
Correct Usage:
boolean isTrue = true; // Correct: boolean literals must be lowercaseExplanation: Always use true and false in lowercase to represent boolean values.
8. Using null with Non-Object Types
The null literal is used to represent the absence of an object reference and cannot be assigned to primitive types like int, boolean, or char.
Common Mistake: Using null with a primitive type.
int num = null; // Incorrect: `null` cannot be assigned to primitivesExplanation: null can only be used with reference types (like Integer, String), not primitive types.
Correct Usage:
Integer num = null; // Correct: `null` can be assigned to reference typesExplanation: null can be used with reference types, but not with primitive types like int.
By understanding and avoiding these common mistakes, you can ensure that you are using literals effectively in Java. Always remember to use the correct format for numeric literals, boolean values, character literals, and null.
Tag » What Is A Literal In Java
-
Literals In Java - GeeksforGeeks
-
Literals In Java - Javatpoint
-
What Are Literals In Java? - Tutorialspoint
-
Java Variables And Literals (With Examples) - Programiz
-
Literals In Java - Integral, Floating-Point, Char, String, Boolean
-
Literals - Wikibooks, Open Books For An Open World
-
What Is A Literal In Java - Linux Hint
-
Java Literals - Concept Every Java Programmer Must Know
-
Literals In Java
-
What Are Literals In Java: The Complete Guide - AppDividend
-
What Are Literals In Java? - Quora
-
Literals In Java - YouTube
-
Literal Expressions - The Java EE 6 Tutorial
-
Java Literals - What Is Literal In Java? Type Of Literal - Computer Notes