在Java中三目运算能用于字符串的判断吗?
问题描述:
三目运算除了用于赋值运算中还可以用于字符串吗?
答
三目运算类似
if cond:
do a
else:
do b
所以也可以用于字符串操作
答
理论上可以,三目运算符就是一个if else结构
答
能作为条件就可以运算
答
三目运算类似
if cond:
do a
else:
do b
所以也可以用于字符串操作
public static void main(String[] args) {
String str1 = "asd";
String str2 = str1.equals("asd") ? "bcd" : "def";
String str3 = str1.equals("asdf") ? "bcd" : "def";
System.out.println(str2);
System.out.println(str3);
}