Short answer: you can't.
Long answer: you can with workarounds, which effectively will be what checking if-else for null is.
Some already provided their answers, i'll give you a tip (not really an answer, since it actually does the opposite):
Java 8 introduced generic class: Optional
It internally holds a reference to an object or null.
What it gives you is that whenever you would try to invoke a method on the object it holds, to get to the actual object you need to call get() method, which will more likely remind you about checking for existence.
Just a small sample:
package whatever;
import java.util.Optional;
public class Main(){
public static void main(String[] args) {
Optional<String> a = Optional.empty();
Optional<String> b = Optional.ofNullable(notReallyAString);
Optional<String> c = Optional.of("John Paul II");
if(a.isPresent()) System.out.println("Empty optional, you shouldnt be able to see this");
if(b.isPresent()) System.out.println("Optional with a null, shouldnt be able to see this");
if(c.isPresent()) System.out.println(c.get());
}
}
Note however, it is not supposed to be a general-purpose-any-value-volder, you'll more likely only want to use Optional only as a return type of a method to say to the client, that it might be indeed null.
Remember, that trying to get (by get() method) the null element inside the Optional instance will result the NoSuchElementException immediately (but it's still unchecked exception)
strto an empty string, i.e.static String str = "";Then you only have to check whether it's empty ;)