The String class contains a contains method to check the @ symbol, and you can simply check for null with a != null call:
public static void main(String[] args) { String a = "should be false"; String b = "should be @true"; String c = null; System.out.println(checkString(a)); System.out.println(checkString(b)); System.out.println(checkString(c)); } static boolean checkString(String str) { return str != null && str.contains("@"); } Output:
false
true
false