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.
This method is present in package java.lang.String and introduced isEmpty in java 1.6.
The following is the syntax for isEmpty method.
public boolean isEmpty()
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());
}
}
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.
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");
}
}
}
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());
Exception in thread "main" java.lang.NullPointerException
at w3schools/examples.java.w3schools.string.StringisBlankExample.main(StringisBlankExample.java:7)
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.