Java Operators
Estimated time to read: 2 minutes
Learning Objectives¶
- Become familiar with the Java language syntax
- Understanding the built-in operators
- Write simple Java expressions
- Understand branching (if, case) and looping (for, while) statements
Java Operators¶
Operators perform mathematical and logical operations in Java Comparisons, less than, equal to, etc
Note
Compare objects use methods not operators
The syntax is straight forward and much of it is basic mathematics
One of the most important operators in Java, or a least one of the most used, is the dot (.) operators allows access to the data or methods of an object. For example, myEmployee.GetDepartment
.
Java's Operators¶
Operator Type | Symbols |
---|---|
Assignment | = |
Mathematical | + (Addition), - (Subtraction), * (Multiplication), / (Divison), % (Modulus / Remainders) |
Unary | ++ (Increment), -- (Decrement) See note* |
Relational | < (Less than), == (Comparison), > (Greater than), <= (Less than or equal), >= (Greater than or equal), != (Not equal) |
Logical | && (And), || (Or), ! (Not / Negate) |
Logical and Bitwise | & (And), | (Or), ~ (Compliment), ^ (Exclusive OR), << (Shift left), >> (Shift Right with sign bit), >>> (Shift right without sign bit (Unsigned)) |
Dot | . (For accessing members, attributes and methods for the instance of the particular object. ) |
!!! note! These can go before or after a variable
++i will increment by 1 --i will decrement by 1
To the left of the variable. ++i, first it will increment i, then the resulting expression will be the increased value. To the right of the variable, i++, first it uses the current value of i, then it gets incremented.
This is know as pre and post increment / decrement