What are Operators?
Operators are ways to manipulate data into doing basic arithmetic. These operators include addition, subtraction, division, multiplication, and much more! In this lesson we will learn about different operators that can be used in java.
Addition/Subtraction
To Add two variables use, “+”, to subtract use, “-”. For example...
Java
int addition = 5+5;
//this will return the number 10 to 'addition'
System.out.println(addition);
//this will print out the number 10
int subtraction = 6-5;
//this will return the number 1 to ‘subtraction’
System.out.println(subtraction);
//this will print out the number 1
Multiplication/Division
To Multiply two variables use, “*”, to divide use, “/”. For example...
Java
int multiplication = 5*5;
//this will return the number 25
System.out.println(multiplication);
//this will print out the number 25
int division = 6/2;
//this will return the number 3
System.out.println(division);
//this will print out the number 1
Assignment
To assign one value to another value use “XVariableName = YVariableName”. This can only be used to assign one dataset to another dataset. For Example...
Java
int valueOne = 5*5;
//this will return the number 25
int valueTwo = 6/2;
//this will return the number 3
valueOne = valueTwo;
//this will return 3 to the valueOne integer
There are also assignment operators...
Java
int assignmentInt = 0;
//the line above will set initial value of the int to 0
assignmentInt++;
//The line above will add 1 to the int making it 1
assignmentInt--;
//The line above will subtract 1 to the value making it 0
assignmentInt +=5;
//The line above will add 5 to the value making it 5
assignmentInt -=2;
//The line above will subtract 2 to the value making it 3
assignmentInt /=3;
//The line above will divide 3 to the value making it 1
assignmentInt *=6;
//The line above will multiply 6 to the value making it 6