- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Literals
In Java, literals represent fixed values that are directly written in the code. Here's an overview:
Types of Literals:
- Numeric Literals: Represent numeric values such as integers, floating-point numbers, and characters.
- String Literals: Represent sequences of characters enclosed in double quotes (").
- Boolean Literals: Represent boolean values true and false.
- Null Literal: Represents a null reference.
Numeric Literals:
- Integer Literals: Whole numbers without decimal points.
- Floating-Point Literals: Numbers with decimal points or in exponential notation (e.g., 3.14, 2.5e3).
- Character Literals: Single characters enclosed in single quotes ('A', '5').
String Literals:
- Represent sequences of characters enclosed in double quotes ("Hello, Java!").
Boolean Literals:
- Represent boolean values true and false.
Null Literal:
- Represents a reference that does not point to any object (null).
Example
public class LiteralsExample {
public static void main(String[] args) {
// Numeric literals
int age = 25;
double pi = 3.14;
char grade = 'A';
// String literal
String message = "Hello, Java!";
// Boolean literals
boolean isJavaFun = true;
boolean isLearning = false;
// Null literal
String name = null;
System.out.println("Age: " + age);
System.out.println("PI: " + pi);
System.out.println("Grade: " + grade);
System.out.println("Message: " + message);
System.out.println("Is Java Fun? " + isJavaFun);
System.out.println("Is Learning? " + isLearning);
System.out.println("Name: " + name);
}
}
Summary
Java literals represent fixed values directly written in the code. They include numeric literals (integers, floating-point numbers, characters), string literals, boolean literals, and the null literal. Understanding literals is fundamental for initializing variables and specifying constants in Java programs.