Week 2

Table of Contents

Primitive Data Types in Java
Operators in Java
Classes and the Math Class
Packages in Java
Variable Scope in Java
Looping Structures in Java
Conditional Structures in Java

Operators in Java

Operators are used to perform operations on variables and values.  They are often used in mathematical expressions, string manipulations, value assignments or alterations, and conditional statements.

Arithmetic Operators

Operator Name Description Example
+ Addition Adds together two values x + y
- Subtraction Subtracts one value from another x - y
* Multiplication Multiplies two values x * y
/ Division Divides one value by another x / y
% Modulus Returns the division remainder x % y
++ Increment Increases the value of a variable by 1 ++x
-- Decrement Decreases the value of a variable by 1 --x

Assignment Operators

Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
&= x &= 3 x = x & 3
|= x |= 3 x = x | 3
^= x ^= 3 x = x ^ 3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3

Comparison Operators

Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Logical Operators

Operator Name Description Example
&& AND Returns TRUE if both statements are true x < 5 && x < 10
|| OR Returns TRUE if one of the statements are true x < 5 || x < 4
! NOT Reverse the result.  false becomes true and true becomes false !(x < 5 && x < 10)