Java String isEmpty() Method

Java String isEmpty Method

What is String?

In Java, A group of characters known as a string class are ones that a script interprets literally. A string is represented as a byte (or term) range information structure in order to preserve a number of components, frequently letters. The sequence may also designate additional types and arrangements of sequence (or array) data, such as more flexible arrays.

  • Numerous methods, including compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), and substring(), are available in the Java String class.

To know more about String Class in java read the complete article.

String isEmpty() Method:

The java string isEmpty() method of the String class will tell you whether the String is empty or not.The isEmpty() method of the Java String class determines if the supplied string is empty or not. Keep in mind that empty here denotes a string that has exactly zero characters.

  • The java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java.These methods help us to perform operations on String objects such as trimming, concatenating, converting, comparing, replacing strings etc.

The isEmpty() method will throw a NullPointerException if it is used on a null String.

Syntax:

public boolean isEmpty()

Returns:

Returns true if, and only if, length() is 0.

Let’s look at a string-related Java program where the Java String isEmpty() Method is used to perform an operation on the given string.

Example: Find out if a string is empty or not: Run

public class Main {
  public static void main(String[] args) {
    String myStr1 = "Hello PrepInsta";
    String myStr2 = "";
    System.out.println(myStr1.isEmpty());
    System.out.println(myStr2.isEmpty());
  }
}

Output

false
true

Example 2 :Find out if a string is empty or not:

Run
public class Main {
  public static void main(String[] args) {
    String myStr1 = "PrepInsta";
    String myStr2 = "Prep";
    System.out.println(myStr1.isEmpty());
    System.out.println(myStr2.isEmpty());
  }
}

Output

false
false

Example 3: Find out if a string is empty or not:.

Run
public class Main {
  public static void main(String[] args) {
    String myStr1 = "Prep";
    String myStr2 = "Prep";
    System.out.println(myStr1.isEmpty());
    System.out.println(myStr2.isEmpty());
  }
}

Output

false
false

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription