Kodekraftt
/Blog
/Check If A String Is Empty Or Null In Java Definition Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Check if a String is Empty or Null in Java

In Java, checking whether a string is null or empty is extremely important. Many runtime errors occur when developers try to access methods on a null string.

Understanding the difference between null and an empty string helps you write safer and bug-free programs.

Difference Between Null and Empty String

  • null → The string does not refer to any object.
  • Empty string ("") → The string exists but contains no characters.

Why Proper Checking is Important

Calling methods like length() or isEmpty() on a null string will cause a NullPointerException. Therefore, the null check must always come first.

How the Logic Works

  1. Check if the string is null.
  2. If not null, check if its length is zero.
  3. Decide whether the string is null, empty, or valid.

Example 1

Input: null

Result: String is Null

Example 2

Input: ""

Result: String is Empty

Example 3

Input: "Java"

Result: String is Not Empty

Java Program to Check if String is Null or Empty


import java.util.Scanner;

public class CheckNullOrEmpty {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a string (type nothing for empty): ");
        String str = sc.nextLine();

        if (str == null) {
            System.out.println("String is Null");
        } 
        else if (str.length() == 0) {
            System.out.println("String is Empty");
        } 
        else {
            System.out.println("String is Not Empty");
        }
    }
}

Sample Output


Enter a string (type nothing for empty):
String is Empty

Enter a string (type nothing for empty): Hello
String is Not Empty

Important Notes

  • Always check for null before checking length.
  • An empty string still occupies memory.
  • Blank strings (spaces only) are different from empty strings.

Practice Challenges

  1. Modify the program to detect blank strings (only spaces).
  2. Check string emptiness without using length().
  3. Write a utility method that safely checks null and empty values.

Mastering null and empty string checks is essential for writing robust Java applications.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.

Reverse a String in Java — Definition, Examples and Program

Learn how to reverse a string in Java using a simple and efficient approach. This blog explains the logic step by step with examples, a Java program, and practice challenges.