Storing and using data with variables

We can think of a variable as a named storage box. We choose a name, perhaps variableA. These names are like our programmer's window into the memory of the user's Android device.

Variables are values in memory ready to be used or altered when necessary by using their name.

Computer memory has a highly complex system of addressing that, fortunately, we do not need to interact with. Java variables allow us to devise our own convenient names for all the data we need our program to work with. The DVM will handle all the technicalities to interact with the operating system and the operating system will in turn interact with the physical memory.

So, we can think of our Android device's memory as a huge warehouse just waiting for us to add our variables to it. When we assign names to our variables, they are stored in the warehouse ready for when we need them. When we use our variable's name, the device knows exactly what we are referring to. We can then tell it to do things such as the following:

  • Assign a value to variableA
  • Add variableA to variableB
  • Test the value of variableB and take an action based on the result
  • And more, as we will soon see

In a typical app, we might have a variable named unreadMessages; perhaps to hold the number of unread messages the user has. We could add to it when a new message arrives, take away from it when the user reads a message, and show it to the user somewhere in the app layout so they know how many unread messages they have.

Situations that might arise could include the following:

  • User gets 3 new messages, so add 3 to the value of unreadMessages.
  • User logs into the app, so use Toast to display a message along with the value stored in unreadMessages.
  • User sees that a bunch of the messages are from someone she doesn't like and deletes 6 messages. We could then subtract 6 from unreadMessages.

These are arbitrary examples of names for variables, and if you don't use any of the characters or keywords that Java restricts, you can actually call your variables whatever you like.

In practice,however, it is best to adopt a naming convention so that your variable names will be consistent. In this book, we will use a loose convention of variable names starting with a lowercase letter. When there is more than one word in the variable's name, the second word will begin with an uppercase letter. This is called camel casing.

Here are some examples of camel casing:

unreadMessages

contactName

isFriend

Before we look at some real Java code with some variables, we need to first look at the types of variables we can create and use.

Types of variables

It is not hard to imagine that even a simple app will have quite a few variables. In the previous section, we introduced the unreadMessages variable as a hypothetical example. What if the app has a list of contacts and needs to remember each of their names? We might then need variables for each contact.

And what about when an app needs to know whether a contact is also a friend, or just a regular contact? We might need code that tests for friend status and then adds messages from that contact into an appropriate folder so the user knows whether they were messages from a friend or not.

Another common requirement in a computer program, including Android apps, is the right or wrong test. Computer programs represent right or wrong calculations using true or false.

To cover these and many other types of data you might want to store or manipulate, Java has types.

Primitive types

There are many types of variables, and we can even invent our own types as well. But, for now, we will look at the most used built-in Java types. And, to be fair, they cover just about every situation which we are likely to run into for a while. Some examples are the best way to explain types.

We have already discussed the hypothetical unreadMessages variable. This variable is, of course, a number, so we have to tell the Java compiler this by giving it an appropriate type.

On the other hand, the hypothetical contactName will, of course, hold the characters that make up a contact's name.

The type that holds a regular number is called an int, and the type that holds name-like data is called a String. And if we try and store a contact name – perhaps "Ada Lovelace" – in an int such as unreadMessages – meant for numbers – we will certainly run into trouble, as we can see from the next screenshot:

Primitive types

As we can see, Java was designed to make it impossible for such errors to make it into a running program.

Here are the main types of variables in Java:

  • int: The int type is for storing integers, or whole numbers. This type uses 32 pieces (bits) of memory and can therefore store values with a size a little in excess of 2 billion, including negative values too.
  • long: As the name suggests, long data types can be used when even larger numbers are needed. A long uses 64 bits of memory and can store numbers up to 9,223,372,036,854,775,807. Perhaps surprisingly, there are uses for long variables, but the point is if a smaller variable will do, we should use it because our program will use less memory.

    Note

    You might be wondering when you might use numbers of this size. The obvious examples would be math or science applications that do complex calculations, but another use might be for timing. When you time how long something takes, the Java Date class uses the number of milliseconds since January 1, 1970. A millisecond is one thousandth of a second, so there are quite a few of them since 1970.

  • float: This is for floating point numbers. That is, numbers in which there is precision beyond the decimal point. As the fractional part of a number takes memory space just as the whole number part does, the range of a number possible in a float is therefore decreased compared to non-floating-point numbers. So, unless our variable will use the extra precision, float would not be our data type of choice.
  • double: When the precision in a float is not enough, we have double.
  • boolean: We will be using plenty of Booleans throughout the book. The boolean variable type can be either true or false; nothing else. Booleans answer questions such as the following:
    • Is the contact a friend?
    • Are there any new messages?
    • Are two examples for Boolean enough?
  • char: Stores a single alphanumeric character in a char. It's not going to change the world on its own, but could be useful if we put lots of them together.

    Tip

    I have kept this discussion of data types to a practical level that is useful in the context of this book. If you are interested in how a data type's value is stored and why the limits are what they are, then have a look on the Oracle Java tutorials site here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html . Note that you do not need any more information than we have already discussed to continue with this book.

As we just learned, each type of data that we might want to store will need a specific amount of memory. For this reason, we must let the Java compiler know the type of the variable before we begin to use it.

The preceding variables are known as the primitive types. They use predefined amounts of memory and so, using our warehouse storage analogy, fit into predefined sizes of storage box.

As the "primitive" label suggests, they are not as sophisticated as reference types.

Reference types

You might have noticed that we didn't cover the String variable type, which we previously used to introduce the concept of variables that hold alpha-numeric data such as a contact's name.

Strings

Strings are one of a special type of variable known as a reference type. They quite simply refer to a place in memory wherein storage of the variable begins but the reference type itself does not define a specific amount of memory. The reason for this is straightforward.

It's because we don't always know how much data will need to be stored in it until the program is run.

We can think of Strings and other reference types as continually expanding and contracting storage boxes. So, won't one of these String reference types bump into another variable eventually?

As we are thinking about the device's memory as a huge warehouse full of racks of labeled storage boxes, then you can think of the DVM as a super-efficient forklift truck driver that puts the different types of storage boxes in the most appropriate place.

And, if it becomes necessary, the DVM will quickly move stuff around in a fraction of a second to avoid collisions. Also, when required, Dalvik, the fork truck driver, will even vaporize any unnecessary storage boxes.

This all happens at the same time as constantly unloading new storage boxes of all types and placing them in the best place for that type of variable. Dalvik keeps reference variables in a different part of the warehouse to the primitive variables. We will learn more details about this in Chapter 12, The Stack, the Heap and the Garbage Collector.

Strings can be used to store any keyboard character. Like a char, but of almost any length. Anything from a contact's name to an entire book can be stored in a single String. We will be using Strings regularly, including in this chapter.

There are a couple more reference types that we will explore as well.

Arrays

Arrays are a way to store lots of variables of the same type ready for quick and efficient access. We will look at arrays in Chapter 15, Arrays, ArrayList, Map and Random Numbers.

Think of an array as an aisle in our warehouse with all the variables of a certain type lined up in a precise order. Arrays are reference types, so Dalvik keeps these in the same part of the warehouse as Strings. We might, for example, use an array to store dozens of contacts in.

Classes

The other reference type is the class that we have already discussed but not explained properly. We will be getting familiar with classes in Chapter 10, Object-Oriented Programming.

Now we know that each type of data that we might want to store will require an amount of memory. Hence, we must let the Java compiler know the type of the variable before we begin to use it. We do this with a variable declaration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.170.17