×

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

Object class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM mostly generally...

6 minutes read.

Matrix Multiplication Program in Java

Matrix Multiplication Program in Java The matrix multiplication program in Java is the continuation of the matrix program in Java that we have already discussed earlier. In this section, we will...

3 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

Bifunction in java 8

A functional interface in Java is called BiFunction. It first appeared in Java 8. It can serve as the assigning target for a method reference or lambda expression. The java.util.function...

4 minutes read.

Get year from date in Java

The getYear() method of the Java date class returns a number calculated by deducting 1900 from the year that contains or starts with the instant in time represented by this...

4 minutes read.

How to Create a Generic List in Java?

Generics are types that have parameters. The goal is to make it possible for methods, classes, and interfaces to take type (Integer, String, etc., and user-defined types) as a parameter....

4 minutes read.

Java HashSet

HashSet implements the set interface. It uses the hash table to make the collection to store different data types. The hash set is the unordered collection of different data types....

6 minutes read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.

Java vs JavaScript

Java Vs JavaScript Java and JavaScript, both play an important role in the field of Computer Technology. Most people think JavaScript is a part of Java. But it is not entirely...

4 minutes read.

Libraries in Java

The Java Class Library (JCL) specifically is a set of dynamically loadable libraries that Java Virtual Machine (JVM) languages can call at any run time, which is fairly significant because...

6 minutes read.

Java Integer doubleValue() method

The doubleValue() method of Integer class returns a double value for this Integer after a widening primitive conversion. Syntax public double doubleValue() Parameters NA Specified by This method is specified by doubleValue in class Number Return Value This...

1 minute read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

Java Get File Size

There are three ways to get the file size in Java. Using Java InputOutput Using NewInputOutput Library Files.size() Method FileChannel.size() Method Using Apache Commons InputOutput Using Java InputOutput: The Java IO package offers the classes that deal...

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

Java Final Keyword

In Java, the last keyword is used to limit the user. The applications of the java final keyword have large range of usage in program development. Last can be: variablemethodclass A final...

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

Java StringWriter Class

The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer,...

3 minutes read.

Java Boolean Keyword

Boolean keyword In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn...

4 minutes read.

XNOR operator in Java

The opposite of the binary equivalent of XOR is given by XNOR. Truth table: XYXNOR001010100111 If the bits are the same, it returns 1, else it returns 0. Examples:  Input: 10 20 Output : 1 A Binary...

3 minutes read.

Read the XLSX file in Java

Excel files contain cells; reading an excel file in Java differs from reading a word file. JDK doesn't have a direct API that can read or write Word or Excel...

3 minutes read.