if(string.equals("")) { } How to check if the string is not null?
if(!string.equals("")) { } 212 Answers
Checking for null is done via if (string != null)
If you want to check if its null or empty - you'd need if (string != null && !string.isEmpty())
I prefer to use commons-lang StringUtils.isNotEmpty(..)
You can do it with the following code:
if (string != null) { } Checking for null is done by:
string != null Your example is actually checking for the empty string
You can combine the two like this:
if (string != null && !string.equals("")) { ... But null and empty are two different things
Nothing really new to add to the answers above, just wrapping it into a simple class. Commons-lang is quite all right but if all you need are these or maybe a few more helper functions, rolling your own simple class is the easiest approach, also keeping executable size down.
public class StringUtils { public static boolean isEmpty(String s) { return (s == null || s.isEmpty()); } public static boolean isNotEmpty(String s) { return !isEmpty(s); } } Use TextUtils Method.
TextUtils.isEmpty(str) : Returns true if the string is null or 0-length. Parameters: str the string to be examined Returns: true if str is null or zero length
if(TextUtils.isEmpty(str)){ // str is null or lenght is 0 } Source of TextUtils class
isEmpty Method :
public static boolean isEmpty(CharSequence str) { if (str == null || str.length() == 0) return true; else return false; } 1if(str != null && !str.isEmpty()) Be sure to use the parts of && in this order, because java will not proceed to evaluating the the second if the first part of && fails, thus ensuring you will not get a null pointer exception from str.isEmpty() if str is null.
Beware, it's only available since Java SE 1.6.
You have to check str.length() == 0 or str.equals("") on previous versions.
As everyone is saying, you'd have to check (string!=null), in objects you're testing the memory pointer.
because every object is identified by a memory pointer, you have to check your object for a null pointer before testing anything else, so:
(string!=null && !string.equals("")) is good
(!string.equals("") && string !=null) can give you a nullpointerexception.
if you don't care for trailing spaces you can always use trim() before equals() so " " and "" gives you the same result
The best way to check a String is :
import org.apache.commons.lang3.StringUtils; if(StringUtils.isNotBlank(string)){ .... } From the doc :
2isBlank(CharSequence cs) :
Checks if a CharSequence is empty (""), null or whitespace only.
You can use Predicate and its new method (since java 11) Predicate::not
You can write code to check if string is not null and not empty:
Predicate<String> notNull = Predicate.not(Objects::isNull); Predicate<String> notEmptyString = Predicate.not(String::isEmpty); Predicate<String> isNotEmpty = notNull.and(notEmptyString); Then you can test it:
System.out.println(isNotEmpty.test("")); // false System.out.println(isNotEmpty.test(null)); // false System.out.println(isNotEmpty.test("null")); // true A common way for testing null string in Java is with Optionals:
Optional.ofNullable(myString).orElse("value was null") Optional.ofNullable(myString).ifPresent(s -> System.out.println(s)); Optional.ofNullable(myString).orElseThrow(() -> new RuntimeException("value was null")); And to test if it is null or empty you can use Apache org.apache.commons.lang3 library that gives you the following methods:
StringUtils.isEmpty(String) / StringUtils.isNotEmpty(String): It tests if the String is null or empty (" " is not empty)StringUtils.isBlank(String) / StringUtils.isNotBlank(String): Same as isEmpty bt if the String is only whitespace it is considered blank
And applied to Optional you get:
Optional.ofNullable(myString).filter(StringUtils::isNotEmpty).orElse("value was null or empty"); 2if(string != null) or
if(string.length() == 0) or
if(("").equals(string)) 1u can try this
if(string != null) 1