×

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

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 minutes read.

The final Keyword in Java

The final keyword is employed in several instances. Firstly, the non-access modifier final only applies to variables, methods, and classes. The final can be used in the following situations. Final Variables When...

6 minutes read.

Java Public Keyword

A Java access modifier is a public keyword. It can be applied to classes, constructors, methods, and variables. It is the type of access modifier that is least constrained. A...

4 minutes read.

Java Generate Random String

Java Generate Random String In this tutorial, we will learn about to generate random string in Java. Random generation strings mean any string will be generated, which does not follow any...

7 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.

Display Unique Rows in a Binary Matrix in Java

To solve this problem, we must first locate and display the distinct rows of a supplied binary matrix afterward. We will go through how to show distinct rows inside a...

12 minutes read.

Check the presence of Substring in a String in java

In java, the string can be treated as class and datatype. The string contains words and numbers but should be in double-quotes. Example: ” Omsairam” Substring The part of the string is called...

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.

How to generate random numbers in Java

Random numbers, also known as fake numbers, are actually a part of a very large sequence, so they are called random numbers. In a defined set of numbers, every number...

6 minutes read.

Literals in Java

In this article we acknowledge ourselves about the term literals in java, types of literals and an example to each of them. Literal Literal refers to any constant value that can be...

4 minutes read.

Java Integer lowestOneBit()

The lowestOneBit () method of Java Integer class returns an int value with at most a single one-bit, in the position of the lowest-order one-bit in the specified int value.  Syntax public...

2 minutes read.

DatabaseMetaData in Java

The Data about another data is called Meta data. The DatabaseMetaData interface has the meta data of the database present in the system. It consists of database product name, total...

2 minutes read.

TreeSet in Java

Java TreeSet with Example Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not...

8 minutes read.

Java Technologies List

Introducing Java technology is not necessary. Everyone across the globe is still in awe of Java's incredible capabilities for developing mobile apps and websites. Of course, you can be persuaded...

8 minutes read.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

Collections in Java

Collection framework is one of Java's most powerful subsystems. It is a set of classes and interfaces that is all-the-rage technology for superintending objects and a group of objects. In...

6 minutes read.

How to Create Immutable Classes in Java

Introduction Java is a programming language that is entirely object-oriented, and everything in it is seen as an object. And the blueprint or template of these objects are classes. Several classes...

3 minutes read.

Java Boolean toString() Method

The toString() method of Java Boolean class returns a String corresponding to this Boolean object. It returns a string value “true”, if the defined object is true else it returns...

2 minutes read.

Java Map Example

In Java, the Map is an interface that is used mainly to denote key and value pairs. The central concept and theme of this mapping in the java collection framework...

4 minutes read.