Java Objects

You've probably heard or read that Java is a so-called object-oriented language. That means that Java has some special features to allow us to organise our program and data into objects. For now, we can think of things as follows:

An object is some data plus routines to manipulate that data, all packaged together.

Another way of thinking about things is that an object is a "thing with properties". OK, that last point will sound either weird or a statement of the obvious, but all will hopefully become clear in a moment...

To help us understand what an object is, we'll delve straight in and use one. We're going to write a program to simulate 20 dice rolls: that is, print out 20 random numbers between 1 and 6. To do so, we're going to create an object that will act as a random number generator. Once we've created it, then we will call one of its routines 20 times to generate 20 random numbers. The main part of the code looks as follows:

import java.util.Random;

Random ran = new Random();
for (int i = 0; i < 20; i++) {
  int dice = 1 + ran.nextInt(6);

The very first line (the one that begins with the word import) goes at the top of the program file. In fact, it goes just below the line beginning with the word package which most IDEs will create. The other lines go in the usual place, i.e. inside the curly braces { } after the line public static void main(String[] args) that the IDE will also have created.

Some of this will hopefully be familiar, and some of it won't. You should recognise the code for a for loop . The line with System.out.println for printing something out is also hopefully familiar, even though we still haven't really explained what System.out.println actually means.

Then, the first line of this code creates an object. As you can see, we use the new keyword, in a similar way to when we created an array. But this time we're creating an object1. We also have to specify the type of variable, just as before when we created variables of type int. But this time, our variable type is the type of object. The type or class of object is called Random. This is one of the many object classes built into the standard Java libraries2. (The import essentially tells Java that we're going to be using this class— notice that it has an extra part java.util which says which "package" or part of the libraries the class comes from.)

If you imagine "creating an object" is picking up a calculator, we now want to "push some buttons" on the calculator. For this, we need to call a method. This is the part of the program that says ran.nextInt(6). Here, we're taking the object that we created and called ran, and we're calling a routine (or method in Java jargon) that acts on that object. Notice that the method can take a parameter— in this case 6, the maximum random value— and returns a result— in this case, the random number generated. Notice an important piece of syntax: any parameters go inside normal brackets after the method name (which is nextInt() in this case).

Finally, we add 1 to the random number returned by the nextInt() method. The reason we do this is that nextInt() actually returns a number between 0 and one less than the number we supply, between 0 and 5 inclusive in this case), and to simulate a dice roll we actually need numbers between 1 and 6 inclusive.

Next: more arrays

Now we've introduced the very basics of objects, we're actually going to skip back to arrays temporarily, and look at some more examples of working with Java arrays.

1. Actually, a subtlety in Java is that arrays are also objects, but we'll pretend they're different things for now. Arrays are in any case "special".
2. In computing, a library is a set of "general" or "standard" routines— or, in the case of Java, classes— provided with a language, or sometimes, as an "extension" to the language.

If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.