Variables in Java

Home | Graphical Version | Table of Contents | Next Chapter, "Arrays" | Previous Chapter, "Introduction to Java"



Variables are a concept that are not new to most programmers. Without them, programming languages would be useless. If you have a knowledge of C++ variables, peruse this document to find out more about Java's variables. If you do not have any experience with variables, read this very carefully; it will be very helpfull.
  1. About Variables
  2. Using Variables
  3. Declaring Variables
  4. Data Types
  5. Operations With Variables


About Variables

When writing a program, you need a place to store information. What if you wanted to create a program that asked for a number from the user, performed an operation on it, repeated the operation several times, and then returned the value. Where would you store the number that the user inputs; where would you store the number while it is being changed? Obviously this creates a programming challenge. Fortunately, the solution is very simple. VARIABLES!!!

Using Variables

What are variables? They are information storage. They are often described as "mailboxes." Each little mailbox has a name, and they each store one piece of information. These pieces of information are usually a number. Each variable can store one number, but variables can also store characters (variables can not store strings in Java). You can change the contents of a variable by giving it new contents, or doing something to its old contents. Setting a variable equal to something is very simple. Here is the code.

MyVariable = 5;

Now, the variable named "MyVariable," holds the number five. As I said before, variables can be set equal to anything.

MySecondVariable = MyVariable;

Now, the variable named "MySecondVariable," is equal to whatever "MyVariable," was equal to. However, if the contents of "MyVariable" changes, the contents of "MySecondVariable" will not change.

Declaring Variables

Now, where do these variables come from? Actually, you create them by declaring them in you code. Variable declarations can be at the beginning of a class (before the methods) or inside of a method. This will make more sense later when you learn about scope (mouthwash?). Declaring a variable is very simple, here is a variable declaration.

int MyVariable;

Variables can also be set to a value in the same statement as the declaration.

int MyVariable = 5;

You can even declare multiple variables in the same statement.

int MyVariable, MySecondVariable, MyThirdVariable;

MyVariable, is obviously the name of the variable. When naming variables, it is a good idea to keep the names short because you will be typing them often. Also, some words are reserved by Java and you may not use them as variable names. These are called keywords, and they include if, else, goto (not used in Java), and other words that Java uses. Generally, if you are unsure if it is a keyword, you should not use it as a variable name. Also it is a good idea to precede all of your variable names with a letter to avoid problems. For example, if you are making an applet called "Evil Space Monkeys from Mars," you could precede all of your variable names with M for monkeys or Mars. You could also use one of your initials, or a random letter.

Data Types

Now, I will answer the question I know you are all wondering about. What the heck is "int?" Good question. "Int" is the type of data that can be stored in the variable. In this case, it is an integer. MyVariable may only store whole numbers, no fractions or decimals. There are several types of data that you may declare your variable to hold. Here is a list.

DeclareNameDescriptionMin. ValueMax. Value
booleanBooleanHolds two values: "true" and "false." Boolean values are not 1 or 0 as in C++. They must be set to "true" or "false", and they are not interchangable with integers.n/an/a
byteByteThe byte type holds one byte of data and is always an integer-128127
shortShort IntegerThe short integer type holds 16-bits of data and is always an integer.-32,76832,767
intIntegerThe integer type hold 32-bits of data and is always an integer (Surprise, Surprise!).-2,147,483,6482,147,483,647
longLong IntegerThe long integer type holds 64-bits of data and is always an integer.-1.845*10^191.845*10^19
floatSingle-PrecisionThe single-precision data type is a floating point variable that occupies 32-bits of space.-3.4*10^383.4*10^38
doubleDouble-PrecisionThe double-precision data type is a very large floating point variable that occupies 64-bits of space.-1.7*10^3081.7*10^308
charCharacterThe character type holds one character (letter).n/an/a


All of these data types are designed for specific tasks. Looking at all of these data types, you might think that Java should just use double-precision variables and characters, but there is a reason for all of these data types. If you want a variable that will hold one of two possible values, the double-precision variable would be too much. Double-precision variables take up a lot of space in memory. Two bytes may not seem like a lot, but using arrays (explained later) you can have hundreds of variables in your program. When you start doing this, the variables can really stack up and take up more space. Generally, you should always use the smallest variable that will get the job done. Also, you may have noticed that many of the variables are integers. Integers are not decimals. For example, 3, 8, -32, and 2948 are integers, and 4.3, 23.8, -12.6, and 1/2 are not integers.

Operations With Variables

All of this is great, but it is meaningless if you can not manipulate the the data inside of the variables. When setting what a variable is equal to, you can use addition, subtraction, multiplication, and parantheses. Here is an example.

MyVar = (MyVar2 + 5) / 4;

That sets the variable "MyVar" equal to the variable "MyVar2" plus five and divided by two. I have too use the parentheses because Java uses order of operations; if I didn't add the parentheses, I would end up width "MyVar2" plus 2.5. It is a good idea to use parentheses if you are unsure of how Java will treat your code.
Now, what if you want to increment the value of MyVar by one. With early programming languages, you would have to use code like this.

MyVar = MyVar + 1;

This will work fine in Java, but it is a pain. Fortunately, the people at Sun were nice enough to include the increment and decrement operaters which are really clever features (that they stole from the cleverly named C++). Here is what that code would look like with the increment operator.

MyVar++;

Isn't that simpler. The decrement operator works the same way, but it is two subtraction symbols (--) and it decreases the variable by one.
Now, what if you want to increase MyVar by 8? Good question. You just use the += operator.

MyVar += 8; //This increases MyVar by 8.
MyVar -= 8; //This decreases MyVar by 8.
MyVar *= 8; //This multiplies MyVar by 8.
MyVar /= 8; //This divides MyVar by 8.
MyVar += MyVar2 * 8; //This adds MyVar2 times 8 to MyVar.

Just remember that the operator comes before the equal sign. "MyVar =+ 8;" make "MyVar" equal to positive 8, not "MyVar + 8."


Now, you have all of the variable experience that you need, and you are ready for the next chapter. Congratulations! So, grab a cup of cofee (preferably a very large one) and head on to our next chapter.



Back to Top | Home | Table of Contents | Next Chapter, "Arrays" | Previous Chapter, Introduction to Java