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. See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases. See JDK 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.

The following program, ComparisonDemo , tests the comparison operators:

The Conditional Operators

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.

The following program, ConditionalDemo1 , tests these operators:

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). 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 . Otherwise, assign the value of value2 to result ."

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

Because someCondition is true, this program prints "1" to the screen. 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).

The Type Comparison Operator instanceof

The instanceof 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 (named Parent ), a simple interface (named MyInterface ), and a child class (named Child ) that inherits from the parent and implements the interface.

When using the instanceof operator, keep in mind that null is not an instance of anything.

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

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

IMAGES

  1. Conditional Statements in Java (If-Else Statement)

    conditional assignment statement in java

  2. Conditional Statements in Java

    conditional assignment statement in java

  3. Conditional Statements in Java (If-Else Statement)

    conditional assignment statement in java

  4. Conditional Statements in Java (If-Else Statement)

    conditional assignment statement in java

  5. Java

    conditional assignment statement in java

  6. Java Conditional Statements

    conditional assignment statement in java

VIDEO

  1. conditional statements in java

  2. Conditional statements in Java programming

  3. Lesson 11 Conditional Statement in Java (Java Programming Full Course Af-soomaali )

  4. (Practical) Java Programs using Conditional Statement

  5. 9. Java Conditional Statements (Part1)

  6. Conditional Statements in JAVA

COMMENTS

  1. The if-then and if-then-else Statements (The Java™ Tutorials

    If a second statement is later added to the "then" clause, a common mistake would be forgetting to add the newly required braces. The compiler cannot catch this sort of error; you'll just get the wrong results. The if-then-else Statement. The if-then-else statement provides a secondary path of execution when an "if" clause evaluates to false.

  2. How To Write Conditional Statements in Java

    When you run this code in jshell, you will get the following output: Output. x ==> 1. y ==> 0. 1 is bigger than 0. The first two lines of the output confirm the values of x and y. Line 3 reads 1 is bigger than 0, which means that the conditional statement is true: x is bigger than y.

  3. Equality, Relational, and Conditional Operators (The Java

    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. See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.