Contact FutureLearn for Support
Skip main navigation
We use cookies to give you a better experience, if that’s ok you can close this message and carry on browsing. For more info read our cookies policy.
We use cookies to give you a better experience. Carry on browsing if you're happy with this, or read our cookies policy for more information.

Skip to 0 minutes and 13 secondsNumbers aren't just numbers. We know this. They refer to something else. For instance, when I was a kid, it was very important for me to know how many cakes I'd eaten because I could not live with my brother having more than me. In maths, we also started using numbers for different things. For instance, we could put a dot in the graph and having the x and the y value, suddenly, we would be able to create new worlds in graphs. We use this a lot in programming. We have variables. Variables, we give them names so that they become meaningful for us.

Skip to 0 minutes and 52 secondsFor instance, we will have a variable that denotes where is the ball on the screen in the x direction and where is the ball in the y direction. We have a name for this very variable and definitely know what it does. Yes, there's a number in the background, but the variable is the important part. We have many different data types to work with. Many different kind of numbers, many different kinds of things that we can use the variables for. We'll look at that later on.

Skip to 1 minute and 29 secondsHere in our programme, we actually have five variables. And the way that it's created-- well, let's start with the most important bit of the line. That's actually the end. So we got mBallX equals 0. What that means is that we are giving the variable that is called mBallX a value of 0.

Skip to 1 minute and 56 secondsSo now, we know where the x position of the ball will be. It will be at 0. Likewise, we're giving the mBallY a value of 0 as well. So the y position of the ball will be 0. Now I have decided that the name mBallY and mBallX makes sense. It would make sense to me. The small m is actually some naming convention that Android is giving us. Any variable like this should start with a small m. It stands for "member." It's not that important at the moment, but it's to do with how the programme is structured. So I'll follow this naming convention. Then "ball" denotes that we're talking about the ball. A ball on the screen.

Skip to 2 minutes and 53 secondsAnd x and y, well, it's kind of explanatory. It's to do with the position on the screen. The next one is more exciting or more interesting. mBallSpeedX and mBallSpeedY. This, for me-- and I've decided this-- is the speed in x and y direction as pixels per second. So I'm setting up how fast the ball should move. And in this, given 0 and 0, of course the ball will be stationary-- the beginning of the running of the programme. "Float." Well, "float" is telling the system what kind of variable we'll be storing. Because we can store many different types. And "float," well, that is the closest we get to a real number in Java. It's almost a real number. Not quite.

Skip to 3 minutes and 56 secondsThere are some differences. And you can go here and read up on them if you're especially interested in it. But it is basically a real number. A number with a dot in it, if you like. "Private" is relating to an advanced feature called Object Oriented Programming. We will not cover that here in this course but once you finish the course, you should go and read up on it. Or if you feel like finding out more about what that is, you can also go talk about it and ask questions to the community in the advanced sections of the course, of course.

Skip to 4 minutes and 40 secondsIn the code, for instance, in this part of the code where we are setting up before we start running, we can then change these values. Notice that I'm just setting the speed of x and y to 0 again here before we start. If I wanted to initialise the speeds to something else, this should happen here. But I'm then changing the mBallX, the position of x and the mBallY y to be in the middle of the screen. mCanvasWidth and Height are values that I have created elsewhere in the code. It's set when the screen size is known. And therefore, we can always use mCanvasWidth and mCanvasHeight when we need to know the width and the height of the screen.

Skip to 5 minutes and 37 secondsSo here, because I'm dividing it by two, we will put the ball in the middle of the screen. It has now changed from being 0 to being the middle of the screen. When looking at code like this, it can be hard to figure out what is happening when we are just looking at it. And there's actually a way of looking at it in a practical way to see what is happening while the programme is running. It's called the Debugger, using the Debug. Now if you can't get this to work, it's not a big problem. Because sometimes there can be issues with emulators, for instance, and setting up to the Debugger.

Skip to 6 minutes and 21 secondsAnd it can be very hard to get that fixed if it doesn't work because it's bound to be issues with the setup of your system. But try it out. If it works, it's quite neat feature. I'll show you how it works here. So for the Debugger to work, we will need to set a breakpoint, a breakpoint of where we want to stop the code. So whenever the code is running, and it does that step by step, you will take it line at a time. Well, when it comes to the line where you've set the breakpoint, the Debugger will stop and you can actually go in and see what the value of all the variables is.

Skip to 7 minutes and 0 secondsSo let's put one here before we start. You do that by double clicking. And instead of running it using the Run As, we will use this one-- the little bug here, Debug app. So let's just press that and see what happens. I have an emulator set up.

Skip to 7 minutes and 24 secondsAnd I will then start it in the emulator.

Skip to 7 minutes and 33 secondsThe warning always comes up and it's OK. It's a warning that is there displayed while the system in the background is trying to set up a connection to the emulator via Debugging. So here we have the application. And you can see the ball is in 0.0, the first setting that I've given it. And when I press it the first time, when I press the screen, it would actually go in to set up beginning. And we then expect it to stop because we have a breakpoint. And it does. I was lucky. And when we go in, we can see here that all the variables that are available at the moment. So you can see, as expected, mBallX and Y is 0.

Skip to 8 minutes and 18 secondsThe speeds-- but we can only see Y at the moment. The system works in a way where it only shows the things that it believes is interesting for us. We can actually go in here under this and find mBallSpeedX and Y. This is actually the place where all variables that are used will be found. As you can see, there's a lot. This is to do with all the code I have created and all the code that the address system is working with as well. It's big. So it's best to just step this here for the time being. But at the moment, as you can see, the canvas width is the 240.

Skip to 9 minutes and 7 secondsSo when we have run this line, we expect that mBallX will change to 120. If we press F8, that is saying let's do the next line but don't stop running, just do the next line and we'll see what the result is. So F8 and there we have it. mBallX is 120. And we do the next one, the height, let's see. Yes, we get 160. So this is a way of going in and look at and see how the variables are changing according to the rules that we've set in. So if there's something where you don't really understand what the code is doing, sometimes makes sense to set a breakpoint and try the Debug. The Debugger does slow down the emulator.

Skip to 10 minutes and 6 secondsSo it's not as nice as running it. So still run it if you want to just test things. But if there are certain things that you want to see, certain values, setting a breakpoint makes sense. Over here, you control your application module Debugging. And I'll just shut it down.

Skip to 10 minutes and 28 secondsAnd this is at the core of what you could do with the Debugger. Go in, interact with the application, set breakpoints and it will then break whenever we need to break. And you can follow and see how the different lines interact with the data that is available in the application. There are more advanced data structures than floats. We have some that are simple-- also integers, strings, boolean. But we also have some that are very, very advanced. And Bitmap is actually an example of that. Bitmap is the data structure that is used to display an image on the screen. And in this instance, Bitmap is the ball.

Skip to 11 minutes and 26 secondsI'll show you this because we can actually do some very cool things using this variable. Let's go to the On Draw. If go in here in the On Draw, what I want you to focus on is this line. In the Draw Bitmap, in Canvas the Draw Bitmap-- Canvas being yet another one of these very advanced variables-- in the Draw Bitmap, what we're doing here is saying, let's draw the Bitmap called mBall. And then we say which position we want to draw it in. And if you notice, first of all, I'm using the mBallX. That was expected because that's, after all, where the x position should be of the ball.

Skip to 12 minutes and 7 secondsBut then I'm minusing the mBall.getWidth, the width of the ball, the actual Bitmap and divide it by 2. The reason for that is because in Bitmap, the 0.0 position of the x and y, is actually not in the middle. It's at the top left corner. So therefore, to make it seem as if it's in the middle of the ball, I have to move it. Don't worry if you don't get that at this point. We will look much more on the operator side of this later on. It just shows that there's something really powerful here; that through developing code, we can create our own types. mB mBall is created by Google. Sorry, the Bitmap has been created by Google.

Skip to 13 minutes and 7 secondsAnd likewise, the game that I'm creating here is actually its own type. And we could do similar things with it if we make a finished product out of it. It's always been a fascinating thing for me that through these small steps that we are taking in the code, really small steps, we can create something that becomes well, complex. Something that it's not easy to achieve.

Introduction to data types and variables

Variables are like containers. These containers are used for storing data.

In this video, you will learn more about variables and how they are used, as well as the types of data that can be stored in them. You will also use them to make changes to your game.

You might also like to download the debugging help sheet, which provides some handy hints for debugging your code.

Note: If you are finding this step difficult we suggest that you take a look at our course FAQs. If, however, your problem still persists you should move on to the next step. You can then return to this step in a couple of weeks when you are more familiar with the environment.

The two web pages referred to in the video are:

Share this video:

This video is from the free online course:

Begin Programming: Build Your First Mobile Game

University of Reading