×

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

Java String join() method

Java String join() method returns a joined String with given delimiter Syntax: public static String join(CharSequence delimeter,charSequence...elements) public static String join(CharSequence delimeter,Iterable<?extens charSequence>elements) Parameters: delimiter: char value to be added with each element elements: char value...

1 minute read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

Series Program in Java

Series Program in Java The series program in Java is written to print the mathematical series such as the Fibonacci series, Pell series, etc. A few of the renowned series are...

12 minutes read.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

Java Import Keyword

Java's import keyword used in the code that follows the import statement, a Java class is declared. Once a Java class is declared, it is possible to use the class...

6 minutes read.

Java Boolean getBoolean() Method

The getBoolean() method of Java Boolean class returns true if the specified system property is not null and is equal to the String ‘true’, else the result returned is false. Syntax public...

1 minute read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.

Java DatagramSocket and Java DatagramPacket

Datagrams TCP/IP style networking specifies a serialized, predictable, and reliable stream of data in the form of a packet. Servers and clients communicate through a reliable channel, such as TCP socket, have a dedicated...

6 minutes read.

Java Swing

Java Swing is the extension of Abstract Windows Toolkit (AWT). Any component designed in Swing will appear the same on any platform. Problems/Disadvantage of Abstract Windows Toolkit (AWT): As we know that...

27 minutes read.

Java Read File to String

There are different ways to deal with forming and examining a text record. This is normal while dealing with various applications. There are different ways to deal with looking at a...

6 minutes read.

List all files in a Directory in Java

The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some...

3 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 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 minutes read.

Difference between Static and Instance Methods in Java

Static Method in Java The static technique has a place with the class as opposed to the object of the course. These are intended to be divided between every one of...

10 minutes read.

nth Prime Number in Java

A number is considered prime if only divisible by one and itself. Prime numbers include, for example, 2, 3, 5, 7, 11, etc. In looking at it another way, a...

5 minutes read.

Java Thread Creation

Java provides the two ways to create a Thread: Implementing the Runnable interface.Extending the Thread class. Implementing Runnable interface The easiest way of creating a thread is to make a class that implements...

5 minutes read.

Java Binary Operators

In this article, we are going to discuss about the binary operators in Java and their operations. Also, an example program will be discussed along with the operations. These are...

6 minutes read.

Java Base64 Encoding and Decoding

Introduction to Encoding and Decoding Encoding is the process of putting the sequence of characters like letters, numbers, punctuations, and other symbols into a specialised format for the efficient transmission or...

11 minutes read.

Java Clone Array

We frequently need to copy an array to back up its original components. We have a few unique numbers and strings, including Armstrong numbers, palindrome numbers, and palindrome strings. To...

4 minutes read.

Untouchable Number in Java

If a number N cannot be divided properly by any positive number, it is said to be an untouchable number. Additionally known as nonaliquot numbers. The sequence is A005114 from...

3 minutes read.