To see if a String is empty or null, we should write an if condition by,
- first, checking if the String object is null using the == operator.
- second, checking if the its empty using the isBlank() function from the String class.
import java.util.Arrays;
import java.util.stream.IntStream;
public class Example2 {
public static void main(String[] args) {
String name1 = "";
String name2 = "Sam";
String name3 = null;
String name4 = " ";
//Example 1:
if (name1 == null || name1.isBlank()) {
System.out.println("Name cannot be empty or null!");
} else {
System.out.println("Hello " + name1);
}
//Example 2:
if (name2 == null || name2.isBlank()) {
System.out.println("Name cannot be empty or null!");
} else {
System.out.println("Hello " + name2);
}
//Example 3:
if (name3 == null || name3.isBlank()) {
System.out.println("Name cannot be empty or null!");
} else {
System.out.println("Hello " + name3);
}
//Example 4:
if (name4 == null || name4.isBlank()) {
System.out.println("Name cannot be empty or null!");
} else {
System.out.println("Hello " + name4);
}
}
}
Output:
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!