Welcome to collectivesolver - Programming & Software Q&A with Examples. A website you can trust. All programs tested and work.

Contact: aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Survey Junkie is the #1 place online for your audience to earn rewards for their opinion.

Managed VPS Hosting
Affordable. Secure. Your Own Virtual Private Hosting. Faster than AWS and Rackspace

Bonsai Boy of New York Bonsai Trees and Accessories
Liquid Web - Highest Level of Performance and Security with Managed Dedicated Servers

Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

21,340 questions

28,142 answers

573 users

How to check if a string is empty or null in Java

1 Answer

0 votes
public class MyClass {
    public static String isEmptyOrNull(String str) {
        if (str == null) {
            return "null";
        } else if (str.isEmpty()) {
                    return "empty";
                } else {
                    return "not null or empty";
    public static void main(String args[]) {
        String str1 = "";
        String str2 = null;
        String str3 = "  ";

        System.out.println("str1 is: " + isEmptyOrNull(str1));

        System.out.println("str2 is: " + isEmptyOrNull(str2));

        System.out.println("str3 is: " + isEmptyOrNull(str3));
str1 is: empty
str2 is: null
str3 is: not null or empty



answered 6 days ago by avibootz