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

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
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