Heart containing Coding Chica Java 101

Other Java Math / Logic Operators

TIP: References Quick List

We talked about adding two integer values in some prior posts. However, let’s discuss the other basic mathematical and logical operators Java supports, too.

Assignment Operator

When we want to set a variable’s value, we can assign it with the = sign.

For example:

    int myVariable = 10; 

This will set the value of 10 to the variable named myVariable.

Urnary (One Input) Operators

Java supports these basic operators that consume one input:

OperatorExample(s)Comments
+int value1 = +1;
int value2 = 1;
Positive values are assumed by default, even if the + is omitted.
int value3 = -1;Negates the value otherwise defined.
++int count = 0;
count ++;
++count ;
updateQuery.setString(count++, e.getKey());
updateQuery.setString(++count , e.getKey());
Adds 1 to the existing value and updates the variable with the same name.
Be careful if using this as part of a larger command, the location of the ++ compared to the variable name will change the value you get output from the command, as well.
int count = 0;
System.out.println(“First value = ” + count++);
System.out.println(“Second value = ” + ++count);
Will output
First value = 0
Second value = 2
Because the first use (count++) could be read as count’s value, then increment.
The second use (++count) could be read as increment, then count’s value.
However, if this is the only instruction in that command, the difference between the two is moot.
int value5 = 10;
value5–;
–value5;
Subtracts 1 from the existing value and updates the variable with the same name.
Just like the incrementing operator (++), the decrementing operator (–) can be used in a larger command, but the location of the — will influence whether the decrementing happens before or after the determination of what value will be returned.
!boolean booleanValue = false;
boolean invertedBooleanValue = !booleanValue;
Can be read as “not”. Inverts the boolean value provided to it. The value true will become false. The value false will become true.
Does not modify the original variable’s value, though.

Basic Mathematics Operators

Java supports the following operators when doing basic mathematics that consume two inputs:

OperatorExampleComments
+int value = 1 + 1;Adds the two numeric values provided to it.
int value = 2 – 1;Subtracts the second value from the first.
*int value = 2 * 3;Multiplies the two values provided to it.
/int value = 12 / 2;Divides the first value by the second value.
%int value = 11 % 2;Divides the first value by the second value and returns only the remainder.
This is quite useful when doing things like striping the rows of a table with different styles / background colors – based upon whether the row number is odd or even.
Another use might be trying to fill 3 different collections as equally as possible, no matter how many values you are provided.

More Complex Math Calculations

Java provides a Math utility class, with static methods that you can use to do some of the more complex calculations, such as:

  • Powers / exponents
  • Absolute values
  • Rounding
  • Determining the minimum or maximum value
  • Trigonometry functions, like sin, cos, tan

More Boolean Calculations

Java provides the following in a Boolean utility class as static methods, as well:

  • Logical and (both arguments must be true for the method to return true)
  • Logical or (either or both arguments may be true for the method to return true)
  • Logical xor, AKA exclusive or (one and only one of the arguments must be true for the method to return true)

Other Java Math / Logic Operators

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.