Welcome to pickSourcecode.com Login | Register    
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
java - The Equality and Relational Operators       Share
2007-10-14 |  codewarrier  | Viewed: 2410  |    0

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");
value1 != value2
value1 < value2
value1 <= value2

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.

&& 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 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:

class ConditionalDemo2 {

public static void main(String[] args){
int value1 = 1;
int value2 = 2;
int result;
boolean someCondition = true;
result = someCondition ? value1 : value2;



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.

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{}
obj1 instanceof Parent: true
obj1 instanceof Child: false
obj1 instanceof MyInterface: false
obj2 instanceof Parent: true
obj2 instanceof Child: true
obj2 instanceof MyInterface: true

Latest topics
help  Viewed: 457
The if statement   Viewed: 779
The switch statement  Viewed: 779
Looping - for statement in java  Viewed: 779
Classes in Java  Viewed: 779
Objects in java  Viewed: 779
Java - overloading methods  Viewed: 779
Java - The this keyword  Viewed: 780
Java - The stack class  Viewed: 780
Elements and Attributes of JNLP File  Viewed: 780


Submit comment's


User Comment's:

Submitted By:
Prof: Software
Tech: C ,Cpp
Send Mail: malathi.v

Related topics
java - The Equality and Relational Operators
java - Bitwise and Bit Shift Operators
java - The while and do-while Statements
java - The if-then and if-then-else Statements
INTERVIEW Questions in java packages and Interfaces
Exception Handling in java
Usage of finalize() method in Java
Elements and Attributes of JNLP File

Related References
java - display current date and time
how to upload a file and how to store it using socket?
sounds handling using java applets
find maximum of 2 nos. in java
find minimum of 2 nos. using conditional operator in java
a program to generate 5 random nos. between 1 to 100 in java
program to find sum and product of a given digit in java
program to find factorial of given no. in java
program to find fibonacci series of a given no in java
program to find sum of all integers greater than 100 in java
program to convert given no. of days into months and days. in java
a program to find average of consecutive n odd no. and even no in java
a revised version of hello world in java
java - number of days in a month
java - random circles drawing

Web site contents Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us