40% DISCOUNT LIMITED SEATS
Java Full Stack + AI Industrial Training 2026
Expert MNC Mentors • Industrial Roadmap • 10 Seats Left
GitHub
Month 01: Lab Setup

Java & GitHub

  • Java 21 & IntelliJ AI Mastery
  • GitHub Team Industrial Flow
  • MNC Standard VS Code Setup
Logic
Month 02: Logic Lab

Logic Mastery

  • Complex DSA with MNC Experts
  • Telugu & English Logic Bridge
  • Daily Problem Solving Drills
Backend
Month 03: API Hub

Spring & SQL

  • Spring Boot 3 Microservices
  • Postman API Automated Testing
  • PostgreSQL DB Architecture
Frontend
Month 04: UI Factory

React & Tailwind

  • React 18 & AI Component Dev
  • Industrial Tailwind UI Library
  • Redux State Management
Cloud
Month 05: Deploy

AWS & Jira Hub

  • Cloud Deploy on Real AWS
  • Agile Scrum with Jira Tools
  • Jenkins CI/CD Pipeline
Success
Month 06: Result

Job Offer Letter

  • Tier-1 MNC Direct Referrals
  • Salary Negotiation Strategy
  • High CTC Placement Pipeline

Java Tutorial | Understanding STRING Literals, Null Literals & Underscore Use in Literals | S9

Yogi Siddeswara 0

Test Your Knowledge

Take this quiz to reinforce your understanding of String, Null, and Underscore literals in Java.


Introduction:

Welcome to this session on understanding Java String literals, Null literals, and the use of underscores in numeric literals. These are fundamental concepts that will enhance your coding skills and make your programs more readable.

String Literals:

String literals are sequences of characters enclosed within double quotes. They are used to represent constant text values in your Java programs.

  • Example: String greeting = "Hello, World!";

In the example above, "Hello, World!" is a string literal assigned to the variable greeting.

Null Literals:

The null literal is used in Java to represent the absence of an object or value. It’s often used to signify that a variable has not been initialized.

  • Example: String name = null;

Here, the variable name is explicitly set to null, meaning it doesn't reference any object.

Underscore in Numeric Literals:

Starting with Java 7, you can use underscores in numeric literals to make them more readable. The underscore is ignored by the compiler and does not affect the value of the number.

  • Example: int largeNumber = 1_000_000;

This technique is especially useful for representing large numbers. The variable largeNumber still holds the value 1000000.

Java Program Example:

Here’s a simple Java program that demonstrates the use of String literals, Null literals, and underscores in numeric literals:

 

public class LiteralExamples {
    public static void main(String[] args) {
        // String literal
        String greeting = "Hello, World!";
        
        // Null literal
        String name = null;
        
        // Underscore in numeric literals
        int largeNumber = 1_000_000;

        // Output values
        System.out.println("Greeting: " + greeting);
        System.out.println("Name: " + name);
        System.out.println("Large Number: " + largeNumber);
    }
}
                (code-box)

Explaining the Program:

  • String Literal: greeting holds the text "Hello, World!".
  • Null Literal: name is set to null, indicating it has no value.
  • Underscore in Literals: largeNumber is easier to read, but it still represents the number 1000000.

Program for String Literals

This program demonstrates the use of String literals in Java.

 

public class StringLiteralExample {
    public static void main(String[] args) {
        // String literals
        String greeting = "Hello, World!";
        String farewell = "Goodbye, World!";
        
        // Output the string literals
        System.out.println("Greeting: " + greeting);
        System.out.println("Farewell: " + farewell);
    }
}
        (code-box)

Program for Null Literals

This program shows how to use null literals in Java.

 

public class NullLiteralExample {
    public static void main(String[] args) {
        // Null literal
        String name = null;
        
        // Checking if the variable is null
        if (name == null) {
            System.out.println("The name variable is null.");
        } else {
            System.out.println("Name: " + name);
        }
    }
}
        (code-box)

Program for Using Underscore in Numeric Literals

This program demonstrates how to use underscores in numeric literals in Java.

 

public class UnderscoreInLiteralsExample {
    public static void main(String[] args) {
        // Using underscores in numeric literals
        int largeNumber = 1_000_000;
        double decimalNumber = 123_456.78_90;
        
        // Output the numeric literals
        System.out.println("Large Number: " + largeNumber);
        System.out.println("Decimal Number: " + decimalNumber);
    }
}
        (code-box)

Key Points to Remember:

  • String literals: Used for text values in Java.
  • Null literals: Indicate the absence of a value or reference.
  • Underscore in literals: Improves the readability of large numbers.

Conclusion

Understanding how to use String literals, Null literals, and underscores in numeric literals will make your Java code more readable and efficient. Practice these concepts in your own code to get a solid grasp of their usage.

Post a Comment

0 Comments