Mathemagics! |
So now we're going to do some basic maths and a bit more stuff with variables. Nothing very hard really. There's also quite a lot of shorthand stuff, which we'll do as we go along. First, let's add a value to
x.
class Hello { public static void main(String[] args) { //System.out.println("Hello, world!"); int x = 2; x = x + 3; System.out.println("The value of x is " + x); } }
Compile and run this new program. It should say The value of x is 5. To be absolutely clear: we take the value of
xand add3. Then we put this new value (5) back intoxusing the=symbol (or operator). Remember that using=in this way is called assignment.OK, cool. So let's use some other operators to do subtraction, addition and division. To multiply we use
*and to divide we use/.
class Hello { public static void main(String[] args) { //System.out.println("Hello, world!"); int x = 2; x = x + 3; System.out.println("The value of x is " + x); x = x - 1; System.out.println("The value of x is " + x); x = x * 3; System.out.println("The value of x is " + x); x = x / 7; System.out.println("The value of x is " + x); } }Let's try it:
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello The value of x is 5 The value of x is 4 The value of x is 12 The value of x is 1 [nurmes]btg:I hope this all makes sense. The last result should surprise you - twelve divided by seven is roughly 1.71428 on my calculator. However we said that
xis anint, in other words a whole number. So why isn't it rounded up to two? Well, when using the/operator with whole numbers, the result is always rounded down. Got that? Good.So for our next mission, we are going to learn some shorthand. Notice with something like
x = x + 2, we are taking the value ofx, doing something to it and putting it back where we found it. This isn't so bad forx, but if our variable had a longer name then we might be typingsupercalifragilistic = supercalifragilistic + 2. That would suck. So we have a shorter way of doing these things. It's easier to demonstrate than explain:
class Hello { public static void main(String[] args) { //System.out.println("Hello, world!"); int x = 2; x += 3; System.out.println("The value of x is " + x); x -= 1; System.out.println("The value of x is " + x); x *= 3; System.out.println("The value of x is " + x); x /= 7; System.out.println("The value of x is " + x); } }As you can see, the program does exactly the same thing:
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello The value of x is 5 The value of x is 4 The value of x is 12 The value of x is 1 [nurmes]btg:This is also covered in Lecture Note 7, Section 7.1. We're going to learn another special shorthand in Java, so let's remove some lines from our program and change some:
class Hello { public static void main(String[] args) { //System.out.println("Hello, world!"); int x = 2; x += 3; System.out.println("The value of x is " + x); System.out.println("The value of x is " + x++); System.out.println("The value of x is " + x); } }Make sure you put
x++and notxin the second one. Now if we think ofxas meaning "get the value of x", thenx++means "get the value of x, and then add one to x". Let's run the program and see what I mean:
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello The value of x is 5 The value of x is 5 The value of x is 6 [nurmes]btg:Duh? Where did
xchange from5to6? Well, the line with thex++added one tox. So how come that line printed out The value of x is 5? That's the whole point. I'll repeat what I said:x++means "get the value of x, and then add one to x". We get the value ofx(to print out - the value at this stage is 5, so we print a 5), and then add one to it. So nobody sees that we've added one until the third time, when we print out The value of x is 6.If that was totally over your head, don't worry as I'll explain it again in a different way before the end of the lesson. Now let's try a very small change to the program:
class Hello { public static void main(String[] args) { //System.out.println("Hello, world!"); int x = 2; x += 3; System.out.println("The value of x is " + x); System.out.println("The value of x is " + ++x); System.out.println("The value of x is " + x); } }Now the operator
++xis slightly different tox++. It means something like "add one to x, and then get the value of x". So we add one (makingxequal to6) and then get the value of x (which is now6) and print it out. Look:
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello The value of x is 5 The value of x is 6 The value of x is 6 [nurmes]btg:Let's do two more operators from the same family. Here's the first one in action:
class Hello { public static void main(String[] args) { //System.out.println("Hello, world!"); int x = 2; x += 3; System.out.println("The value of x is " + x); System.out.println("The value of x is " + x--); System.out.println("The value of x is " + x); } }
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello The value of x is 5 The value of x is 5 The value of x is 4 [nurmes]btg:Can you see that
x--is very much likex++? Let's do the last one (you can probably guess what it is...):
class Hello { public static void main(String[] args) { //System.out.println("Hello, world!"); int x = 2; x += 3; System.out.println("The value of x is " + x); System.out.println("The value of x is " + --x); System.out.println("The value of x is " + x); } }
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello The value of x is 5 The value of x is 4 The value of x is 4 [nurmes]btg:OK, if my explanations suck then how about this for a way of understanding what these operators do:
Using "clever" operators Equivalent System.out.println("The value of x is " + x++);System.out.println("The value of x is " + x); x = x + 1;System.out.println("The value of x is " + x--);System.out.println("The value of x is " + x); x = x - 1;System.out.println("The value of x is " + ++x);x = x + 1; System.out.println("The value of x is " + x);System.out.println("The value of x is " + --x);x = x - 1; System.out.println("The value of x is " + x);The techincal jargon for these is incrementing (adding one) and decrementing (subtracting one).
x++andx--are known as postfix operators (meaning that they come afterx).++xand--xare known as prefix operators (meaning that they come beforex). These are covered in Lecture Note 6, Sections 6.3 and 6.4.1.Well done, you've learned a whole big heap of operators including the last four which are quite hard. The next lesson is a really important thing about programming, and you won't get far without it, but it's not very difficult, so breathe easy, get a coffee and chill out. Then let's play Shooting Stars!.
Too patronising? Too complex? Typing error? Offended by traffic cones?
Got a question or something I should add? Send an email to ben_golding@yahoo.co.uk !
visits to this site
The contents of this site are copyright of Ben Golding