intro to java operators
Java
OPERATORS
Operators are symbols that perform
logical or mathematical functions on
operands such as variables, constants
and objects.
Unary operators require only one
operand. In Java, there at least four
unary operators:
negation(-), bitwise complement(~),
increment(++), and decrement(--)
Binary operators require two
operands.
Some examples are + for addition or
concatenation, - for subtraction, * for
multiplication, and / for division
Arithmetic operators
In Java are the same arithmetic operators taught to you when
you were elementary except for some shorthand operators
Arithmetic Operators
Operators Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
++ Increment
-- Decrement
Comparison/Relational Operators
All relational operators are binary operators
primarily because they indicate the relationship
among two operands.
Operators Samples Description
> a>b a is greater than b
>= a >= b a is greater than or equal to b
< a<b a is less than b
<= a <= b a is less than or equal to b
== a == b a is equal to b
!= a != b a is not equal to b
CONDITIONAL/LOGICAL OPERATORS
»Much like relational Operators Description
operators, the result of
using logical operators is a ! NOT
boolean value: || logical OR
either true or false. && logical AND
Truth tables will demonstrate the result of a logical
operation.
Operand1 Result
! true False
! false True
The NOT (!) operator
- negates the value of the operand
The OR (||) operator
Operand1 Operand2 Result
true || true True
true || false True
false || true True
false || false False
- returns true if at least one of its operands is true
The bitwise exclusive OR or XOR (^)
Operand1 Operand2 Result
true ^ true False
true ^ false True
false ^ true True
false ^ false False
- returns true when both operands have different
values. Otherwise, it will return false.
The AND (&&) operator
Operand1 Operand2 Result
true && true True
true && false False
false && true False
false && false False
- returns true only when both operands are true
Shorthand Operators
These shorthand operators include the assignment operator (=).
Operators Description
+= Assignment with Addition
-= Assignment with Subtraction
*= Assignment with Multiplication
/= Assignment with Division
%= Assignment with Modulo
Operator Precedence
Operator Precedence Table
( )
++ -- ! -
* / %
+ -
< <= > >=
== !=
^
&&
||
= *= /= += -=
Evaluate
Evaluate the given expressions where
a=2, b=4, and c=8
1) x= a++;
2) y= --b;
Self-Check
3) !(((++a)!=4)&&(--b==4))
4) (c++!=b)||(a++==b)
5) t= a+b*c/3-2;