什么!类型在这段代码中意味着什么?

时间:2016-03-19 10:54:18

标签: java operators

!type在以下代码段中的含义是什么?为什么要放!

String type = request.getParameter("tipo");
if (type == null) {
    out.print("ERROR: The field type wasn't selected<br>");
}
if (!type.equals("auto")
&&  !type.equals("trailer")
&&  !type.equals("motorcycle")) {
    out.print("ERROR: field error ("+type+")<br>");
}

有人可以向我解释这些代码,尤其是!type吗?

3 个答案:

答案 0 :(得分:2)

Type是String对象的一个​​实例,它的方法为String#equals(...),该方法返回boolean ...

”这是否定操作符并反转任何布尔值......

所以 !type.equals("auto")是一个boolean条件,因为比较String var和名称类型是否具有值“auto”。

答案 1 :(得分:1)

&#39;!&#39;是一个布尔运算符,它只是表示NOT(否定)。

所以!type.equals(&#34; auto&#34;)如果类型不是&#34; auto&#34;

将评估为true

答案 2 :(得分:1)

!不是,并且equals()方法返回boolean,这意味着它返回true和false然后!它会消极,它会成真,虚假和虚假,例如:

String text = "test";

Text.equals("test") returns true
And !text.equals("test") returns false

Text.equals("example") returns false
And !text.equals("test") returns true

或者只是在你的代码中,它意味着检查文本是否不等于......

相关问题