Operators - JAVA: Easy Java Programming for Beginners, Your Step-By-Step Guide to Learning Java Programming (2015)

JAVA: Easy Java Programming for Beginners, Your Step-By-Step Guide to Learning Java Programming (2015)

Chapter 6. Operators

This chapter will describe the available operators that you can add to your lines of code as you program more complex scenarios. Operators are mainly used to control, modify and compare data in a Java language environment.

Since the beginning of this book, we have already been using the equal sign (=) or the assignment operator, which works by assigning a compatible value into a variable. In addition, arithmetic operators are used to control the value that will be assigned to a variable. If you can remember the mathematical operators you have learned in school, they will be the same operators you will need to perform mathematical computation in Java programming. Gaining the knowledge on how to use operators is an important requirement. All you need to understand are the symbols for each operator and their functions.

Arithmetic Operators

These are the most basic form of operators apart from the assignment operator.

Additive Operator (+) – Returns the sum of two values

Subtractive Operator (-) – Returns the difference of two values

Multiplicative Operator (*) – Returns the product of two values

Divisive Operator (/) – Returns the quotient of two values

Remainder Operator (%) – Returns the remainder of two values being divided

Increment/Decrement Operators

These operators increase or decrease the value of the variable by 1.

Increment (++) - Increase the value by 1

Decrement (--) - Decrease the value by 1

They can also be used as a prefix or preincrement operator where “++” is placed before the variable. This means that 1 is added to the variable’s value before it is being used in any other part of the program (the value will be adjusted immediately when used). When “++” is placed after the variable then it becomes a postfix or postincrement operator. This time, 1 is added to the variable’s value after the variable is used in any other part of the program (the value will be returned first before it is adjusted).

Example:

MyVariableName = 23;

MyVariableName++; - returned value is still 23

++MyVariableName; - returned value is adjusted to 24

Logical Operators

These operators, also called comparison operators, permit a degree of flow control to your program by comparing two values or set specific conditions. A Boolean value can be returned, depending on the two values compared, that will determine whether a certain block of code will be executed or not. This is the most basic form of logic in a computer program.

Is Equal to (==) - Checks if two values are equal

Is Not Equal to (!=) - Checks if two values are not equal

Is Greater than (>) - Checks if the value to the left of the operator is greater than

the value to its right

Is Less than (<) - Checks if the value to the left of the operator is less than the

value to its right

Is Greater than or Equal (>=) - Checks if the value to the left of the operator is greater than

or equal to the value to its right

Is Less than or Equal (<=) - Checks if the value to the left of the operator is less than or

equal to the value to its right

Logical AND (&&) - Checks if both values are true

Logical OR (||) - Checks if at least one of the values is true

Bit Wise Operators

These operators manipulate variables at the bit level in a primitive yet fast way. Since they analyze binary numerals, which are the smallest unit of addressable memory, they process execution in the quickest way possible.

And (&) - A 2 equal-length binary operator that performs with the logic operator

“AND” and multiplies both elements with bits

Not (~) - A unary operator that performs with the goal of logical negation and

complements the binary value of ones

Or (|) - An operator that takes 2-bit patterns whose lengths are equal and

performs with the logic operator “OR” (results are either 0 or 1)

Xor (^) - An operator that takes 2-bit patterns whose lengths are equal (results are

expected to be defined with a similar digit)

Now that you have learned how to manipulate operators, you will be more adept in understanding and automating more complex Java programs. Another programming feature that will be introduced in the next chapter is flow control, which demonstrates non-sequential lines of codes.