1/21
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Operators
Allow us to perform operations on variables and values.
Arithmetic Operators
Assignment Operators
Compound Assignment Operators
Comparison Operators
Logical Operators
Increment and Decrement Operators
Java operator categories
Arithmetic Operators
Used for mathematical calculations.
Addition (+)
Subtraction (-)
Multiplication (*)
Division (/)
Modulus (%)
Arithmetic Operators is used for mathematical calculations, including _______, _______, _______, _______, and _______.
public class Main{
public static void main(String[] args){
int a = 12;
int b = 90;
System.out.println("Addition: " + (a + b));
System.out.println("Modulus: " + (a % b));
}
}OUTPUT:
10
Example of Arithmetic Operators
Assignment Operators
Used to assign values to variables. The = operator is the basic assignment operator.
public class Main{
public static void main{
int x = 5;
x += 4; //Equivalent to x=x+4
System.out.print(x);
}
}OUTPUT:
9
Example of Compound Assignment Operators
Comparison Operators
Used to compare values, returning a boolean result (true or false).
Equal to (==)
Not equal to (-=)
Greater than (>)
Less than (<)
Greater than or Equal to (>=)
Less than pr Equal to (<=)
Comparison operators that are used are ______, _______, _______, ______, _______, _______.
public class Main{
public static void main(String[] args){
int age = 29;
System.out.print(age < 29);
}
}OUTPUT:
False
Example of comparison operators
Logical Operators
Used to combine multiple conditions, returning a boolean result.
AND (&&)
OR (||)
NOT (!)
Logical operator uses different operator including _______, _______, and _______.
AND (&&)
Returns true if both conditions are true.
OR (||)
Returns true if at least one condition is true.
NOT (!)
Inverts the boolean value.
public class main{
public static void main(String[] args){
int allowance = 2000;
boolean ily = true;
System.out.print(allowance == 2000 && ily);
}
}OUTPUT:
true
Example of logical operators
Increment and Decrement Operators
Used to increase or decrease a variable by 1.
Prefix (++x)
Increments the value before use in an expression
Postfix (x++)
Increments the value after use in an expression.
public class Main{
public static void main(String[] args){
int x = 3;
System.out.println(--x);
System.out.println(x++);
}
}OUTPUT:
2
2 (count 3)
Example of increment and decrement
Compound Assignment Operator
Shorthand way to update a variable's value by performing an operation on it like addition or multiplication and then assigning the result back to that same variable.
public class Main{
public static void main(String[] args){
int x = 3;
System.out.println("Initial Value: " + x);
}
}OUTPUT:
Initial Value: 3
Example of Assignment Operator.