Movatterモバイル変換


[0]ホーム

URL:


Documentation

The Java™ Tutorials
Language Basics
Variables
Primitive Data Types
Arrays
Summary of Variables
Questions and Exercises
Operators
Assignment, Arithmetic, and Unary Operators
Equality, Relational, and Conditional Operators
Bitwise and Bit Shift Operators
Summary of Operators
Questions and Exercises
Expressions, Statements, and Blocks
Questions and Exercises
Control Flow Statements
The if-then and if-then-else Statements
The switch Statement
The while and do-while Statements
The for Statement
Branching Statements
Summary of Control Flow Statements
Questions and Exercises
Trail: Learning the Java Language
Lesson: Language Basics
Section: Operators
Home Page >Learning the Java Language >Language Basics
« Previous • Trail • Next »

The Java Tutorials have been written for 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.
SeeDev.java for updated tutorials taking advantage of the latest releases.
SeeJava Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
SeeJDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

Equality, Relational, and Conditional Operators

The Equality and Relational Operators

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:

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 != value2value1 <  value2value1 <= value2

The Conditional Operators

The&& and|| operators performConditional-AND andConditional-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:

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 anif-then-else statement (discussed in theControl Flow Statements section of this lesson). This operator is also known as theternary operator because it uses three operands. In the following example, this operator should be read as: "IfsomeCondition istrue, assign the value ofvalue1 toresult. Otherwise, assign the value ofvalue2 toresult."

The following program,ConditionalDemo2, tests the?: operator:

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);    }}

BecausesomeCondition is true, this program prints "1" to the screen. Use the?: operator instead of anif-then-else statement if it makes your code more readable; for example, when the expressions are compact and without side-effects (such as assignments).

The Type Comparison Operator instanceof

Theinstanceof operator compares an object to a specified type. 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 (namedParent), a simple interface (namedMyInterface), and a child class (namedChild) that inherits from the parent and implements the interface.

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: trueobj1 instanceof Child: falseobj1 instanceof MyInterface: falseobj2 instanceof Parent: trueobj2 instanceof Child: trueobj2 instanceof MyInterface: true

When using theinstanceof operator, keep in mind thatnull is not an instance of anything.

« PreviousTrailNext »

About Oracle |Contact Us |Legal Notices |Terms of Use |Your Privacy Rights

Copyright © 1995, 2024 Oracle and/or its affiliates. All rights reserved.

Previous page: Assignment, Arithmetic, and Unary Operators
Next page: Bitwise and Bit Shift Operators

[8]ページ先頭

©2009-2025 Movatter.jp