Wednesday, August 24, 2016

JAVA 5 - Manipulating variables through operators

Last time we looked at declaring variables.  But a variable isn't really much use unless we learn to do something with it - for that we need operators.


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.

Arithmetic operators

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.

Integer maths

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.

Double maths

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

So ...

(int) num3 / num2;

Returns 1.

(double) num1 / num3;

Returns 1.66666667.

Mixed arithmetic

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.

Strings

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";
String str3;

str3 = str1 + str2;

Returns "Hello World".


Assignment operators

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

int answer;

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;

Whereas,

answer -= 2;

Will decrease the value of answer by 2.

All these will work for double numbers too.

Finally,

answer++;

Is a method used for increasing an integer by 1.  You can use,

answer--;

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.




Extension material

Look for other types of basic operators here.


1 comment:

  1. Online casinos, the risk of the player.
    Gclub The website is open to live online casinos. Direct online transmission from the resort casino. Cambodia's leading betting house. Online gambling games are very popular nowadays. Because the use of online gambling services in addition to fun and reliable, you can also give money to you as well. Gambling online casino games that are more exhilarating. And access to the site without you need to install the list. And our online gambling web site also handles mobile betting by giving you a chance to win a simple online casino. Take it anywhere. Betting on the online gambling games will give you even more joy with us at any time. What kind of games do you enjoy? We also have many privileges for you. Betting online is easy, whether new or old, it's a pro bet. Guarantee all the fun that is available to players all the time with the site. Gclub มือถือ

    ReplyDelete