java的三元表达式的使用
public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
public static void main(String[] args) { User user = new User(); String name = user.getName()==null ? "--- ---":user.getName(); System.out.println(name); }
意思是:user的属性name等于null的话,String name的值就为
--- ---
不然就是user.getName()