Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Literals

Literals in Java are constant values that can be assigned to variables or used as arguments for method calls. They represent a fixed value in the source code. This tutorial will cover different types of literals in Java, including integer, floating-point, character, string, and boolean literals.

  • Integer literals:

Integer literals represent whole numbers without a decimal point. They can be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation. By default, integer literals are of type int, but you can specify long literals by appending the letter 'L' or 'l'.

int decimalLiteral = 42;
int hexadecimalLiteral = 0x2A;
int octalLiteral = 052;
int binaryLiteral = 0b101010;
long longLiteral = 10000000000L;
  • Floating-point literals:

Floating-point literals represent real numbers with a decimal point. They can be expressed using decimal or scientific notation. By default, floating-point literals are of type double, but you can specify float literals by appending the letter 'F' or 'f'.

double decimalLiteral = 3.14;
double scientificNotationLiteral = 1.23e-4; // Equivalent to 1.23 * 10^(-4)
float floatLiteral = 0.5F;
  • Character literals:

Character literals represent single characters enclosed in single quotes. They can be any Unicode character, including escape sequences for special characters.

char letter = 'A';
char digit = '9';
char unicodeChar = '\u0041'; // Equivalent to 'A'
char newLine = '\n';
  • String literals:

String literals represent sequences of characters enclosed in double quotes. They are instances of the java.lang.String class, which is immutable.

String hello = "Hello, world!";
String emptyString = "";
  • Boolean literals:

Boolean literals represent the truth values true and false.

boolean isTrue = true;
boolean isFalse = false;
  • Null literal:

The null literal represents a reference that does not point to any object. It can be assigned to any reference type (classes, interfaces, arrays) but not to primitive types.

String noString = null;

This tutorial covered different types of literals in Java, including integer, floating-point, character, string, and boolean literals. Literals are used to represent constant values in your program, and understanding their syntax and usage is essential for writing clear and concise code.

  1. Different types of literals in Java

    In Java, literals represent constant values that can be assigned to variables.

    int integerLiteral = 42;
    double floatingPointLiteral = 3.14;
    char characterLiteral = 'A';
    boolean booleanLiteral = true;
    
  2. Numeric literals in Java programming

    Numeric literals can be integers or floating-point numbers.

    int intLiteral = 42;
    long longLiteral = 123456789L;
    float floatLiteral = 3.14F;
    double doubleLiteral = 2.71828;
    
  3. Character literals and string literals in Java

    char charLiteral = 'A';
    String stringLiteral = "Hello, World!";
    
  4. Boolean literals in Java and their usage

    boolean trueLiteral = true;
    boolean falseLiteral = false;
    
  5. Escape sequences in string literals in Java

    Escape sequences allow special characters in strings.

    String escapeSequence = "This is a line break.\nAnd this is a tab\t.";
    
  6. Floating-point literals in Java

    float floatLiteral = 3.14F;
    double doubleLiteral = 2.71828;
    
  7. Hexadecimal and binary literals in Java

    int hexLiteral = 0x1A; // Hexadecimal
    int binaryLiteral = 0b101010; // Binary
    
  8. Literals in Java vs. variables and constants

    final int CONSTANT_VALUE = 42; // Constant using final keyword
    int variableValue = CONSTANT_VALUE; // Using variable