<< Chapter < Page Chapter >> Page >

Illustration of relational operators

The program shown in Listing 2 illustrates the result of applying relational operators in Java. The output is shown in thecomments at the beginning of the program. Note that the program automatically displays true and false as a result of applying the relational operators.

Listing 2 . Illustration of relational operators.
/*File relat01.java Copyright 1997, R.G.Baldwin Illustrates relational operators.Output is The relational 6<5 is false The relational 6>5 is true *********************************************************/class relat01 { //define the controlling class public static void main(String[]args){ //main method System.out.println("The relational 6<5 is " +(6<5)); System.out.println("The relational 6>5 is " +(6>5)); }//end main}//End relat01 class.

Conditional operators

The relational operators are often combined with another set of operators (referred to as conditional or logical operators) to construct more complex expressions.

Java supports three such operators as shown in the following table.

Conditional or logical operators Operator Typical Use Returns true if&&Left&&Right Left and Right are both true || Left || Right Either Left or Right is true! ! Right Right is false

The operands shown in the table must be boolean types, or must have been created by the evaluation of an expression that returns a boolean type.

Left to right evaluation

An important characteristic of the behavior of the logical and andthe logical or operators is that the expressions are evaluated from left to right, and the evaluation of the expression is terminated as soon as the resultof evaluating the expression can be determined.

For example, in the following expression, if the variable a is less than the variable b , there is no need to evaluate the right operand of the || to determine that the result of evaluating the entire expression would be true . Therefore, evaluation will terminate as soon as the answer can be determined.

Left to right evaluation (a<b) || (c<d)

Don't confuse bitwise and with logical and

As discussed in the next section, the symbols shown below are the bitwise and and the bitwise or .

Bitwise and and bitwise or &bitwise and | bitwise or

One author states that in Java, the bitwise and operator can be used as a synonym for the logical and and the bitwise or can be used as a synonym for the logical inclusive or if both of the operands are boolean . (I recommend that you don't do that because it could cause confusion for someonereading your code.)

Note however that according to a different author, in this case, the evaluation of the expression is not terminated until all operands have beenevaluated, thus eliminating the possible advantage of the left-to-right evaluation.

Bitwise operators

Java provides a set of operators that perform actions on their operands one bit at a time as shown in the following table.

Bitwise operators Operator Typical Use Operation>>OpLeft>>Dist Shift bits of OpLeft right by Dist bits (signed)<<OpLeft<<Dist Shift bits of OpLeft left by Dist bits>>>OpLeft>>>Dist Shift bits of OpLeft right by Dist bits (unsigned)&OpLeft&OpRight Bitwise and of the two operands| OpLeft | OpRight Bitwise

Questions & Answers

calculate molarity of NaOH solution when 25.0ml of NaOH titrated with 27.2ml of 0.2m H2SO4
Gasin Reply
what's Thermochemistry
rhoda Reply
the study of the heat energy which is associated with chemical reactions
Kaddija
How was CH4 and o2 was able to produce (Co2)and (H2o
Edafe Reply
explain please
Victory
First twenty elements with their valences
Martine Reply
what is chemistry
asue Reply
what is atom
asue
what is the best way to define periodic table for jamb
Damilola Reply
what is the change of matter from one state to another
Elijah Reply
what is isolation of organic compounds
IKyernum Reply
what is atomic radius
ThankGod Reply
Read Chapter 6, section 5
Dr
Read Chapter 6, section 5
Kareem
Atomic radius is the radius of the atom and is also called the orbital radius
Kareem
atomic radius is the distance between the nucleus of an atom and its valence shell
Amos
Read Chapter 6, section 5
paulino
Bohr's model of the theory atom
Ayom Reply
is there a question?
Dr
when a gas is compressed why it becomes hot?
ATOMIC
It has no oxygen then
Goldyei
read the chapter on thermochemistry...the sections on "PV" work and the First Law of Thermodynamics should help..
Dr
Which element react with water
Mukthar Reply
Mgo
Ibeh
an increase in the pressure of a gas results in the decrease of its
Valentina Reply
definition of the periodic table
Cosmos Reply
What is the lkenes
Da Reply
what were atoms composed of?
Moses Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask