Java Literals


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:

  1. Integer literals - An integer literal is a whole number without a decimal point. For example:
int age = 20;
  1. Floating-point literals - A floating-point literal is a number with a decimal point. For example:
double pi = 3.14;
  1. Boolean literals - A boolean literal represents a true or false value. For example:
boolean isMale = true;
  1. Character literals - A character literal represents a single character enclosed in single quotes. For example:
char letter = 'a';
  1. String literals - A string literal represents a sequence of characters enclosed in double quotes. For example:
String message = "Hello, World!";
  1. 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.