So now that we have learned about variables we should learn what we can do with them by using operators in Java.
Now before we get started lets lay down some terminology:
- An Operator is a symbol that specifies an operation to be performed.
- An Operand is the object(s) that an operation is performed upon.
So in an example operation of 1 + 2
the operator would be the ‘+‘ symbol which specifies that we would be performing addition. The numbers 1 and 2 would be our operands, the things being added.
Arithmetic operators
Most of these should be familiar to those of you who remember your elementary school math
Operator | Name | Example Usage |
---|---|---|
+ | Addition | 2 + 2 // 4 |
– | Subtraction | 4 – 2 // 2 |
* | Multiplication | 2 * 2 // 4 |
/ | Division | 8 / 4 // 2 |
% | Modulus | 11 % 4 // 3 |
Example Usage:
int sum = 2 + 2;
System.out.println(sum); // 4
An additional note here is that the Addition operator can also be used to add Strings together in something called “concatenation.”
String helloWorld = "Hello" + " " + "World!";
System.out.println(helloWorld) // Hello World!
One operator that requires slightly more explanation is that “Modulus” operator, which returns the remainder of a division.
int modulus = 11 % 4
System.out.println(modulus ); // 2
Floating Point
In Java if we divide two integers then the result will be an integer. This results in losing the remainder of the division unless we take steps to convert to a data type that is capable of storing “floating point numbers.” I’m not going to go into too much detail on what a “floating point number” is for the purposes of this Java series, but I will say that there is an inherent difficulty to storing decimal fractions in binary which requires something like floating point arithmetic to approximate. If you are interested in reading more about floating point numbers you can check out floating-point-gui.de.
// Will return 3
15 / 4
// Will return 3.75
15.0 / 4.0;
// Will return 3.75
(double) 15 / 4
// Will return 0.30000000000000004
0.1 + 0.2
Assignment Operators
So you’ve already seen the assignment operator a number of times already, you just didn’t know the term.
Operator | Name |
---|---|
= | Assignment |
The assignment operator is used to assign values to variables. But you can also combine this with the arithmetic operators from above!
int x = 2;
x += 3;
System.out.println(x); // 5
Equality and Comparison Operators
Operator | Name |
---|---|
== | Equal To |
!= | Not Equal To |
> | Greater Than |
>= | Greater Than or Equal To |
< | Less Than |
<= | Less Than or Equal To |
These operators will return a boolean variable based on the result of the comparison.
int x = 2;
int y = 3;
boolean isXGreaterThanEqualToY = x >= y;
System.out.println(isXGreaterThanEqualToY); // false
Unary Operators
“Unary” operators refer to the fact that they only take one “operand” whereas many of the operators we have discussed so far are “binary” operators which take two.
Operator | Name |
---|---|
+ | Positive Value |
– | Negative Value |
++ | Increment |
— | Decrement |
There’s another distinction that can be made with the increment / decrement operators. They can be placed either before the operand (prefix) or after (postfix,) and this will effect when the increment / decrement occurs. So if we were to take out our example from above and tweak it slightly we might end up with:
int x = 2;
int y = 3;
boolean isXPrefixGreaterThanEqualToY = ++x >= y;
System.out.println(isXPrefixGreaterThanEqualToY); // true
x = 2;
boolean isXPostfixGreaterThanEqualToY = x++ >= y;
System.out.println(isXPostfixGreaterThanEqualToY); // false
Logical / Conditional Operators
Logical in this case refers to boolean logic. In Java this can be most succinctly explained with:
Operator | Name |
---|---|
&& | AND |
|| | OR |
! | NOT |
boolean orCondition = false || false || true;
System.out.println(orCondition ); // true
Now you have a good idea of how to use operators in Java!