Pages

Friday, March 22, 2019

String isEmpty method in java with example - Internal Implementation

String isEmpty method in java:

In this post, We will learn more about java.lang.String.isEmpty() method.

Step 1) What this method does or method description

Step 2) Syntax, Parameters, Return type
Step 3) Example 1:  program to check String is empty or not
Step 4) Example 2:  program to check if the string is null or "  " empty spaces
Step 5) How isEmpty works internally and implementation code.

Step 1: String isEmpty method description

Java String isEmpty() method checks whether a String is empty or not. This method returns true if the given string is empty, else it returns false. In other words you can say that this method returns true if the length of the string is 0.

This method is present in package java.lang.String and introduced isEmpty in java 1.6.



String isEmpty method in java


Step 2: Syntax

The following is the syntax for isEmpty method.

public boolean isEmpty()

Parameters:

Does not take any parameters.


Return type:

boolean. Returns true if length is 0, otherwise false.



Step 3: String isEmpty method example program 1:

In this example, We are taking three strings and checking is empty or not.

package examples.java.w3schools.string;

public class StringisBlankExample {
 public static void main(String[] args) {
  // String 1
  String string1 = "this string has value";

  // String 2
  String string2 = " ";

  // String 3 - blank string with no character.
  String string3 = "";

  // checking all strings are empty or not.
  System.out.println("string 1 is empty: " + string1.isEmpty());
  System.out.println("String 2 is empty: " + string2.isEmpty());
  System.out.println("String 3 is emtpy: " + string3.isEmpty());
 }
}


Output:

The above program produces the following output.


string 1 is empty: false
String 2 is empty: false
String 3 is emtpy: true


Here it has checked for all strings are empty or not. Last string is empty, so it returned true.


Step 4: String isEmpty method example program 2 to check if the String is null:


In this program, we will learn what will be the output if string is null.


package examples.java.w3schools.string;

public class StringisBlankExample {
 public static void main(String[] args) {
  // String 1 - null string - no value assigned
  String nullString = null;

  // String 2
  String valueString = "value string";

  // printing values
  System.out.println("nullString :: " + nullString);
  System.out.println("valueString :: " + valueString);

  // Use case : Using if condition.

  if (nullString == null || nullString.isEmpty()) {
   System.out.println("nullString is null or blank");
  } else {
   System.out.println("nullString is having some value");
  }

  if (valueString == null || valueString.isEmpty()) {
   System.out.println("valueString is null or blank");
  } else {
   System.out.println("valueString is having some value");
  }

 }
}


Output:

The above program produces the following output.

nullString :: null
valueString :: value string
nullString is null or blank
valueString is having some value


In the output, first 2 lines are values of the input strings. Next 2 lines are output of if conditions.
Always first if condition else block will not be executed because if condition checks for string is null. In this case, String is null. So, all executions of this program will print inside if condition statement.


If we do not put if condition and call directly isEmpty method then will get the NullPointerException. Please see the below code.

  String nullString = null;
  System.out.println(nullString.isEmpty());

Output:

Exception in thread "main" java.lang.NullPointerException
 at w3schools/examples.java.w3schools.string.StringisBlankExample.main(StringisBlankExample.java:7)


Step 5: How isEmpty works internally and implementation code

isEmpty internal code is pretty straight forward. Below is the it's internal implementation and how works internally.


   public boolean isEmpty() {
        return value.length == 0;
    }


Just checks the string length is 0. If length is 0 then return true otherwise false. This method does not check for blank spaces. 

In java 11, a few new methods are introduced in String class.

Java 11 new String methods with examples


Java 11 isBlank method ignores the white spaces. You can read more in detail all new methods of new String api.


Please post your questions in comment section. We will get you the right answers.

No comments:

Post a Comment

Please do not add any spam links in the comments section.