×

Java String isEmpty() method

Java String isEmpty() method checks whether current String is empty or not.

Syntax:

public boolean isEmpty()

Returns

It returns true, if length of String is 0 otherwise false.

Java String isEmpty() method example 1

     public class JavaStringIsEmptyEx1
    { 
       public static void main(String args[])
      { 
    String s1="TutorialAndExample"; 
    String s2="";
    String s3=s2.concat("");
    System.out.println(s1.isEmpty()); 
    System.out.println(s2.isEmpty()); 
     System.out.println(s3.isEmpty()); 
       }
    }

Output:

false
true
true

Java String isEmpty() method example 2

    public class JavaStringIsEmptyEx2
    { 
        public static void main(String[] args)
        { 
            String s1="TutorialAndExample"; 
            String s2="";          
            // Either length is zero or isEmpty is true 
            if(s1.length()==0 || s1.isEmpty()) 
                System.out.println("String s1 is empty"); 
            else System.out.println("s1");       
            if(s2.length()==0 || s2.isEmpty()) 
                System.out.println("String s2 is empty"); 
            else
            System.out.println(s2); 
        } 
    }

Output:

s1
String s2 is empty

Java String isEmpty() method example 3

    public class JavaStringIsEmptyEx3
    { 
        public static void main(String[] args)
        { 
            String first_Name="Nitish"; 
            String last_Name="";                       
            if(first_Name.length()==0 || first_Name.isEmpty()) 
                System.out.println("Please provide First Name "); 
            else System.out.println("First Name : "+first_Name);       
            if(last_Name.length()==0 || last_Name.isEmpty()) 
                System.out.println("Please provide last Name"); 
            else
            System.out.println("Last Name : "+last_Name); 
        } 
    }

Output:

First Name : Nitish
Please provide last Name

Related Topics

Snake and Ladder Problem in Java

Find the smallest number of dice throws necessary to reach the destination or last cell from the source or first cell on a snake and ladder board. Essentially, the player...

6 minutes read.

Interleaving string in Java

If the string Str3 contains all of the characters from Str1 and Str2, it is considered interleaving Str1 and Str2. Keep in mind that the order of all characters in...

5 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

Java Vs C++

Java Vs C++ Java and C++ both are Object Oriented Programming languages. Both languages are popular for competitive programming. C++ is used by many coders who have just started learning programming...

4 minutes read.

Dangling Else problem in Java

A language interpretation uncertainty is the hanging other issue. The following two types of condition executed code are both possible in programming: 1. if-then-else form 2. if-then form When dealing with the nested...

3 minutes read.

Java String toLowerCase() methods

Java String toLowerCase() method is used to convert all the characters of the String into lower case. Syntax: public String toLowerCase()              public String toLowerCase(Locale locale) Returns: It returns Lower...

1 minute read.

Java extend multiple classes

In Java, what does extend mean? One of several Java inheritance keywords is extended, meaning we pass all or most of the Parent class's characteristics through to the Child class. The...

3 minutes read.

Java BLOB

The two data types used in Java to store binary and big-character objects are BLOB and CLOB. In contrast to other types of data like float, int, double, etc., it...

4 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.

Maximum length of string in java

In java, String can act as a data type and a class. The string can be defined as the collection of characters that are enclosed with double quotes(“ “). The...

3 minutes read.

Palindrome Partitioning problem in Java

In this article, you will be acknowledged about partitioning of the palindrome. It can be done in many ways. This article makes sure every method is discussed. Palindrome If a string remains...

6 minutes read.

Set Matrix Zeros in Java

In coding round interviews, it is frequently asked as the most significant challenge. an m*n matrix is provided. Set the entire column and row of the matrix to 0 if any...

6 minutes read.

Java Return Keyword

The return keyword in Java is used to end a method's execution. the caller receives the return, followed by the appropriate value. The return type of the method, such as...

3 minutes read.

Java String isEmpty() method

Java String isEmpty() method checks whether current String is empty or not. Syntax: public boolean isEmpty() Returns It returns true, if length of String is 0 otherwise false. Java String isEmpty() method example 1    ...

1 minute read.

How to Convert char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.

Pernicious Number in Java

If the number of 1s in a number appearing in the binary representation is the prime number, such a number is known as a pernicious number. A pernicious number always corresponds to...

4 minutes read.

Pangram Program in Java

If a string comprises all alphabet letters from A to Z or from a to z without regard to case, it is referred to as a pangram. Some examples of pangram...

3 minutes read.

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

2 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.