Operations

What if you wanted to do some math in your code? Or check if a certain condition is true? You can use operators!

There are multiple types of operations that you'd use throughout coding. Most of them modify or compare some sort of data.

Math Operations

These are used to do math expressions with data. Think of them as the different signs you'd see in a math class.

Operator
Purpose
Syntax

Addition

Adds two numbers.

a + b

Subtraction

Subtracts two numbers.

a - b

Multiplication

Multiplies two numbers.

a * b

Division

Divides two numbers.

a / b

Modulus

Divides two numbers, but gets the remainder.

a % b

Notice how these operators only affect numbers? There are a couple of exceptions. Firstly, you can use these on chars (this isn't necessary, you won't have to worry about it), and you can use the Addition modifier to add data to a String, which is called concatenation. This data could be any primitive or other Strings.

Example:

int numbers = 12345;
int char = 'A'
int string = "Hello!"
string += char;
string += numbers;
System.out.println(string);

Unary Operations

These are used to modify variables. These are quality-of-life operators that make expressions MUCH shorter than they have to be.

Unary Operator
Purpose
Example

+=

Adds n to the variable.

num += 3

-=

Subtracts n from the variable.

num -= 16

*=

Multiplies the variable by n.

num *= 3

/=

Divides the variable by n.

num /= 10

Logic Operators

Logic operators compare values or other expressions, which let you do logic, as the name suggests. All of these give you a Boolean from them.

Logic Operator
Syntax
Purpose
Example

== (Equals)

value == value

Checks if the two values are equal. (NOTE: DOES NOT WORK ON COMPOSITES- ALWAYS GIVES TRUE FOR THEM}

true == true // gives you true 1 == 1 // gives you true 6 == 9 // gives you false

!= (Not equals)

value != value

Returns if the two values are NOT equal.

true != false // gives you true 2 != 2 // gives you false

&& (And)

condition && condition

Checks if both of the conditions are true.

true && true // gives you true

2 == 4 // gives you false

|| (Or)

condition || condition

Gives you if one of the conditions is true.

1 == 2 || 3 > 2 // gives you true because 3 > 2

! (Not)

!condition

Gives you if the condition is false.

!(2 == 4) // gives you true !(3 == 3) // gives you false

Note: you can assign these conditions to a variable. For example:


local 

Last updated