The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The equality and relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand.相等运算符和关系运算符确定一个操作数是大于、小于、等于还是不等于另一个操作数。The majority of these operators will probably look familiar to you as well.这些操作员中的大多数可能对您来说也很熟悉。Keep in mind that you must use "请记住,在测试两个基本值是否相等时,必须使用“==
", not "=
", when testing if two primitive values are equal.==
”,而不是“=
”。
== equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to
The following program, 以下程序ComparisonDemo
, tests the comparison operators:ComparisonDemo
测试比较运算符:
class ComparisonDemo { public static void main(String[] args){ int value1 = 1; int value2 = 2; if(value1 == value2) System.out.println("value1 == value2"); if(value1 != value2) System.out.println("value1 != value2"); if(value1 > value2) System.out.println("value1 > value2"); if(value1 < value2) System.out.println("value1 < value2"); if(value1 <= value2) System.out.println("value1 <= value2"); } }
Output:输出:
value1 != value2 value1 < value2 value1 <= value2
The &&
and ||
operators perform Conditional-AND and Conditional-OR operations on two boolean expressions.&&
和||
运算符对两个布尔表达式执行“条件与”和“条件或”运算。These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.这些运算符表现出“短路”行为,这意味着只有在需要时才计算第二个操作数。
&& Conditional-AND || Conditional-OR
The following program, 以下程序ConditionalDemo1
, tests these operators:ConditionalDemo1
测试这些运算符:
class ConditionalDemo1 { public static void main(String[] args){ int value1 = 1; int value2 = 2; if((value1 == 1) && (value2 == 2)) System.out.println("value1 is 1 AND value2 is 2"); if((value1 == 1) || (value2 == 1)) System.out.println("value1 is 1 OR value2 is 1"); } }
Another conditional operator is 另一个条件运算符是?:
, which can be thought of as shorthand for an if-then-else
statement (discussed in the Control Flow Statements section of this lesson).?:
,可以将其视为if-then-else
语句的简写(在本课程的控制流语句部分中讨论)。This operator is also known as the ternary operator because it uses three operands.此运算符也称为三值运算符,因为它使用三个操作数。In the following example, this operator should be read as: "If 在以下示例中,此运算符应理解为:“如果someCondition
is true
, assign the value of value1
to result
.someCondition
为true
,则将value1
的值指定给result
。Otherwise, assign the value of 否则,将value2
to result
."value2
的值指定给result
。”
The following program, 以下程序ConditionalDemo2
, tests the ?:
operator:ConditionalDemo2
测试?:
运算符:
class ConditionalDemo2 { public static void main(String[] args){ int value1 = 1; int value2 = 2; int result; boolean someCondition = true; result = someCondition ? value1 : value2; System.out.println(result); } }
Because 由于someCondition
is true, this program prints "1" to the screen.someCondition
为true
,此程序将在屏幕上打印“1”。Use the 如果使代码更具可读性,请使用?:
operator instead of an if-then-else
statement if it makes your code more readable; for example, when the expressions are compact and without side-effects (such as assignments).?:
运算符而不是if-then-else
语句;例如,当表达式紧凑且没有副作用(例如赋值)时。
instanceof
The instanceof
operator compares an object to a specified type.instanceof
运算符将对象与指定类型进行比较。You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.您可以使用它来测试对象是类的实例、子类的实例还是实现特定接口的类的实例。
The following program, 下面的程序InstanceofDemo
, defines a parent class (named Parent
), a simple interface (named MyInterface
), and a child class (named Child
) that inherits from the parent and implements the interface.InstanceofDemo
定义了一个父类(名为Parent
)、一个简单接口(名为MyInterface
)和一个从父类继承并实现该接口的子类(名为Child
)。
class InstanceofDemo { public static void main(String[] args) { Parent obj1 = new Parent(); Parent obj2 = new Child(); System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent)); System.out.println("obj1 instanceof Child: " + (obj1 instanceof Child)); System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof MyInterface)); System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent)); System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child)); System.out.println("obj2 instanceof MyInterface: " + (obj2 instanceof MyInterface)); } } class Parent {} class Child extends Parent implements MyInterface {} interface MyInterface {}
Output:输出:
obj1 instanceof Parent: true obj1 instanceof Child: false obj1 instanceof MyInterface: false obj2 instanceof Parent: true obj2 instanceof Child: true obj2 instanceof MyInterface: true
When using the 使用instanceof
operator, keep in mind that null
is not an instance of anything.instanceof
运算符时,请记住null
不是任何对象的实例。