Operators allow you to manipulate variables in lots of ways. They're in truth the powerhouse of your programming language. There are a whole load, but we're going to focus on just a few. To avoid this series spiralling, you're going to have to research some yourself - which is okay, as most programmers do just that. Looking for operators as they need them.
You're probably semi-familiar with these already - they're the operators which allow you to do arithmetic!
As a physicist, most of the early software I wrote was about simulating a problem in maths or physics, so using these was a must.
The operators +, -, / are probably familiar as add, minus, divide. Looking a little different from normal maths, * is used for multiplication.
In code, you can set out such mathematical statements as you need.
Let's start out by declaring two integers,
int num1 = 5;
int num2 = 3;
Once declared, we don't have to continuously use "int" anymore. So we can use,
num1 + num2;
This will return 8. They're added.
num1 - num 2;
This will return 2. They're subtracted.
num1 / num2;
Might surprise you. Remember these numbers can only have whole number values. It returns 1, because 3 goes into 5 only once. There is no decimal, and there is no rounding up.
To counter this, there is the % operator which is the modulus. This is "the remainder" if you remember back to maths at high school.
num1 % num2;
Returns 2. Once you divide 5 by 3, you have 2 left over.
num1 * num2;
Returns 15. Multiplied.
num1 / 2;
Returns 4. This is a reminder that you don't always have to use variables - whether using int, double or String. You can use fixed values (sometimes called literals) if you want.
This is the maths of decimal numbers.
double num3 = 5;
double num4 = 3
num3 + num4;
This will return 8.0. They're added.
num3 - num 4;
This will return 2.0. They're subtracted.
num3 / num4;
Will return 1.666666667. Decimal points!
num3 * num4;
Returns 15. Multiplied
Mixing data types
If you try mixing ints with doubles in an operation, weird things happen. You can override this by using a cast function. If you put
- (int) before a double number, it treats it as an integer
- (double) before an int number, it treats it as a double
(int) num3 / num2;
(double) num1 / num3;
Hopefully you've seen this before somewhere. But if you have a long calculation such as ...
7 - 2 * 3;
What's the answer? Computers apply multiplication and division before they apply addition and subtraction.
We tend to process from right to left so would expect an answer of 15.
A computer sees it as...
2 * 3 = 6
7 - 6 = 1
And answers 1.
You can use brackets to set orders. Things inside brackets are processed before things outside. So
(7 - 2) * 3;
Is 15. If this shocks you, you need to look this up.
Arithmetic doesn't seem to make much sense in terms of strings. However the + operator joins together (sometimes called concatonate) two Strings to make return a longer string made of both parts.
String str1 = "Hello ";
String str2 = "World";
str3 = str1 + str2;
Returns "Hello World".
Another key part of this, is assignment operators. We've used them before for initialisation - we use the name of a variable, the = sign and a value. The = operator sets the variable to the value we provided.
So if we've declared
Then if we do,
answer = 2 * 3;
It returns 6.
If we do the following,
answer = answer + 2;
It will increase the value of answer by 2 ... so now 8. But we could also do ...
answer += 2;
answer -= 2;
Will decrease the value of answer by 2.
All these will work for double numbers too.
Is a method used for increasing an integer by 1. You can use,
To reduce it by one.
See operators in action
I have an example on Github filled with operator actions for you to see at first hand. Find the code here.
Look for other types of basic operators here.