Java Literals
www.igiftidea.com
In Java, a literal is a fixed value that appears directly in the code of a program. Literals represent values such as numbers, characters, strings, and boolean values.
Here are some examples of Java literals:
- Integer literals - An integer literal is a whole number without a decimal point. For example:
int age = 20;
- Floating-point literals - A floating-point literal is a number with a decimal point. For example:
double pi = 3.14;
- Boolean literals - A boolean literal represents a true or false value. For example:
boolean isMale = true;
- Character literals - A character literal represents a single character enclosed in single quotes. For example:
char letter = 'a';
- String literals - A string literal represents a sequence of characters enclosed in double quotes. For example:
String message = "Hello, World!";
- Null literals - A null literal represents a reference that does not refer to any object. For example:
String name = null;
In Java, literals are used to initialize variables, as operands in expressions, and as arguments to methods. Literals are often used in combination with variables to perform various operations and computations in a program.