Introduction to Java : Data Types and Operators Part-4

Operators

Java also provides several operators that you can use to manipulate your variables.

Mathematical Operators

As in all languages, Java allows you to perform math functions. The following table outlines the available functions:

 

OperatorDescriptionExample
+Performs additionint x = 1;
int y = 2;
int z = x + y // z = 3
Performs subtractionint x = 2;
int y = 1;
int z = x – y // z = 1
*Performs multiplicationint x = 2;
int y = 2;
int z = x * y // z = 4
/Performs divisionint x = 4;
int y = 2;
int z = x / y // z = 2
%Gets the remainder (modulus)int x = 6;
int y = 4;
int z = x % y // z = 2

One thing to remember about mathematics in Java is that since variables are typed, you should be careful to match types or expect automatic casting! In other words, Java will return the result in a type that fits in the result regardless of what was used in the equation.

Assignment Operators

As was alluded to in the last section, variables utilize assignment operators. However, if you are working with a numerical type, you have more assignment operators than simply “=”. The following table outlines the other available assignment operators:

OperatorDescription
=Equality. Note, you can chain assignments such as:
x=y=x=0
+=Addition (x=1;x+=2; //x=3
-=Subtraction
/=Division. Don’t forget about automatic casting! If your division returns a decimal number, it will be chopped to fit its type.
*=Multiplication
%=Modulus
^=Bitwise XOR
&=Bitwise AND
|=Bitwise OR
<<=Left Shift
>>=Right Shift
>>>=Zero Fill Right Shift

Incrementation and Decrementation

Java also provides the standard increment and decrement operators with prefix and postfix notation. Remember that the prefix operator modifies the variable before is it acted upon whereas the postfix operator acts afterward. This is best seen by example:

Precedence

Finally, Java maintains the usual operator hierarchy. The precedence table is shown below:

PrecedenceOperatorAssociativity
1++, –, ~!, +, -, (unary), (typecast)R
2* / %L
3+, -, + (concatenation)L
4>>>, >>, <<L
5<, <=, >, >=L
6==, !=L
7&L
8^L
9|L
10&&L
11||L
12?:R
13=, *=, /=, %=, +=, -=, <<=, >>=, >>>=, &=, ^=, |=R

Flow Control

Some of the most powerful tools of Java programming are control structures. Control structures are used to create the basic logic that drives many of the routines used in your applications. These control structures use Boolean logic to imbue your script with the intelligence necessary to manage the diverse needs of the clients.

All control structures are divided into the control statement (which we will explain below) and the statement block. The statement block is simply a group of commands that are executed together. This block is grouped by enclosing the commands within curly braces ({}). For example, the following is a simple statement block.

Java will execute each statement in a statement block from beginning to end as a group. When, how, or if the script will execute the commands, however, is determined by the control statement.

if / else if / else

The most common control statement is the “if” test. The if test checks to see if some expression is true, and if so, executes the routines in the statement block. Java uses a simple binary comparison as a test of truth. If the result of some operation is true, the operation returns a one and the statement block is executed. If the result is false, it returns a zero, and the statement block is not executed. For example, consider the following code:

In this example, Java checks to see if the scalar variable name has the value of “Selena Sol”. If the patterns match, the matching operation will return true and the application will execute the print statement within the statement block. If Java discovers that name is not equal to “Selena Sol” however, the print will not be executed.

The if test also provides for alternatives: the “else” and the “else if” control statements. The else if alternative adds a second check for truth and the else alternative defines a final course of action for every case of failed if or else if tests. The following code snippet demonstrates the usage of if, else if, and else.

while

The while control statement allows you to loop through a control block until some condition is met.

for

The for loop is another excellent control statement tool. The basic syntax of a for loop follows:

The “initial condition” defines where the loop should begin. The “test” defines the logic of the loop by letting the script know the conditions that determine the application’s actions. The “incrementation” defines how the script should perform the loop.

For example, we might produce a visible countdown with the following for loop:

NOTE: To decrement, you use variableName–.To increment, you use variableName++.

Testing a Condition

We can use the ==, !=, >, >=, <, <= operators to test equality. Further, we can use the && and || operators to perform multiple tests. Consider the following:

The Switch Statement

However, Java does not stop there. Java introduces another control structure called the switch statement. The switch statement allows you to input a single variable and test for multiple values. Consider the following example:

Breaks

As you can see, Java also provides a break statement that allows you to break out of a loop.