×

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 Concatenation

Java String Concatenation Java programming provide a way to combine multiple strings into a single string. It is called as String Concatenation. There are different ways to concatenate two or more...

4 minutes read.

How to take String Input in Java

There are various ways to take String input in Java. In this section, we are going to discuss how to take String input in Java. There are following ways to...

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

Sealed Class in Java

What is a Sealed Class in Java? In programming, the two main issues that must be taken into account when creating an application are security and control flow. The use of...

5 minutes read.

Difference Between Java and PHP

The two most used programming languages are PHP and Java. Both of them have a lot of similarities and distinctions. Let's first grasp each of them individually before examining their...

3 minutes read.

Scanner in Java

Static way of Programming: When a variable can’t change its value during run time is called a Static way of programming. In this programming a variable is directly assigned to a...

4 minutes read.

Java TreeMap

TreeMap in Java with Example Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted...

7 minutes read.

Java Math atan2() Method

The atan2() method of Math class returns an angle theta from the conversion of rectangular coordinates to polar coordinates. Syntax: public static double atan2(double y, double x) Parameters: The parameter ‘y’ represents the ordinate...

3 minutes read.

HashMap Vs HashTable

HashMap HashMap is the basic implementation of the map interface in Java. HashMap stores the data in key and value pairs. Keys are used to access the value of the element. It...

5 minutes read.

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.

Even Odd Program in Java

Even Odd Program in Java The number that are completely divisible by 2 are even and the number that leaves remainder are odd numbers. For example, the numbers 2, 0, 4,...

5 minutes read.

Matrix Multiplication in Java

In Java, using the binary operator (*) we can perform matrix multiplication. A Matrix is a group of arrays. In the multiplication of matrices, the elements of each row are...

3 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Finding Odd Occurrence of a Number in Java

In this tutorial, we will learn how to find the odd occurrence of a number through a java program. Let us consider an array of non-negative integers. Here, except for one...

6 minutes read.

Powerful Number in Java

We will define a powerful number in this article and write Java programs to determine whether a given number is a powerful number or not. Java coding interviews and academic...

6 minutes read.

Getting Synchronized Set from Java HashSet

The synchronizedSet() technique for java.util.Collections class is utilized to return a synchronized (string safe) set supported by the predetermined set. To ensure sequential access, it is important that everything admittance...

4 minutes read.

Normal and Trace of a Matrix in Java

Normal of a matrix The square root of the total squares of each element in a matrix is known as the matrix's normal. Think of the following matrix as an example. Example: [[1,2,3]  ...

3 minutes read.

Java Generate UUID

What java Generate UUID? A 128-bit long value that is universally unique serves as the basis for the terms UUID (Universally Unique Identifier) and GUID (Globally Unique Identifier). Hexadecimal (octet) digits...

5 minutes read.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

4 minutes read.

Add numbers represented by Linked Lists in Java

For calculating the sum of the two numbers that are represented by a linked list, and then store the result in a new linked list. A linked list's head node...

7 minutes read.