Java string null check by != null or !str.equals(null)? [duplicate]
问题 This question already has answers here : Java null check why use == instead of .equals() (16 answers) Closed 7 years ago . What's the best way to check for not null values in java. String query ="abcd"; query != null vs !query.equals(null). which is better?why? 回答1: 1st one is better (and the only option ), because 2nd one will throw NPE , when your value is actually null . As simple as that. Try this out: String str = null; str.equals(null); // will throw `NPE`. So basically, the test which