Introduction to Variables

In this lab, you will be introduced to variables that store primitive data types.

Variables
To Get Started ...

Download the jar file containing the program VarLab.jar. Follow the instructions that you learned in the NetBeans Lab to load this file into a project called VarLab. Here is a brief summary, in case you have forgotten:

  1. Depress the shift key and right-click on the link to save the file to your hard drive. Save the file in your csci/201 folder.
  2. Enter the following commands in your Linux session window to unjar the file:
    [user@mach dir] cd csci/201
    [user@mach dir] jar xfv VarLab.jar
    
    
  3. If netbeans is not already running, give the command to start NetBeans in your Linux session window.
    [user@mach dir] netbeans &
    
    
  4. Create a new NetBeans project called VarLab (Help).
  5. Mount the VarLab directory (Help). (In lab, always mount the directory containing the java file that you'll be working on.)
  6. Open the file in the NetBeans editor by double-clicking on it in the NetBeans explorer window.

Perhaps the best analogy for a variable is a bottle. Think of a bottle - you put stuff inside it, then you put a label on the outside so that you know what you put in it. Variables work this way. A variable is nothing more than a container which holds some data that you refer to by it's name.

Java has two types of variables: those that store primitive data types and reference variables. The only difference between them is what they hold: variables that store primitive data types hold data, reference variables hold the locations of objects in the heap. Reference variables are used when working with objects, so we will wait awhile to discuss them.

When a variable is created, it is located in an area of the computer's memory called the stack. You can think of the stack as just a collection of variables.

Creating Variables

To create a "primitive variable" (a variable that stores a primitive data type), you first specify the type of the variable you want to create, and then its name. Here are a couple of examples:

  int x;           //Creates a new variable of type 
                   //int called x

  double myVar;    //Creates a new variable of type 
                   //double called myVar

  boolean runFlag; //Creates a new variable of type 
                   //boolean called runFlag

For your reference, here is a list of all primitive data types that you can use when creating variables:

TypeWhat it Holds
voidDoesn't contain anything at all
booleanHolds either true or false
byteHolds an integer from -128 to 127
shortHolds an integer from -32,768 to 32,767
intHolds an integer from -2,147,483,648 to 2,147,483,647
longHolds an integer from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
floatHolds a decimal number - Single Precision
doubleHolds a more precise representation of a decimal number - Double Precision
charHolds one character (letter, number, punctuation, etc)

Using Variables

So now you know how to create a variable, but that's only half the battle. What good is creating a variable if you don't store anything in it? The assignment operator ( = ) is used to store data in a variable. Here are some examples:

  int x;                    //Create a variable called x

  x = 5;                    //Put 5 inside x

  boolean myVar = false;    //You can even do this when
                            //you create the variable

So what is happening here? When you created x, a location in the stack was set aside and labeled x. The next line told the computer to put 5 in the location called x. When you created myVar, the same kind of thing happened, except that false was stored in a temporary location in the CPU until the space was created for myVar. This is all illustrated in the following Stack N' Heap:


Stack N' Heap What's This? The Stack N' Heap simulates a trace program. It shows the code being executed, the contents of the stack, and the contents of the heap. To move through the code, use the >> button. This will take you one step through the code. To go back one step, use the << button.

Primative variables are shown in the stack with their names on the left side and values on the right. Reference variables do not have any value on the right, but instead have a yellow line drawn to show which object in the heap it points to. Reference variables with a NULL value don't show anything on the right.
Code
int x;
x = 5;
boolean myVar = false;
int y = x;
Stack
Heap
Temporary

Did you notice line 4? In java, as with most programming languages, you use variables where you normally would use what they contain. In this case, instead of saying int y = 5 , we replaced the integer 5 with x. The computer gets the value stored in x and places it in a temporary location in the CPU (i.e., a register). Then the assignment operator takes that value and stores it in y, just as if we had typed int y = 5. Cool, huh? The end result is y equals x - they store the same value.

Your Assignment ...

Open up the file VarLab.java in NetBeans. Compile and run this file. Looks fine right? Try entering a number (other than zero). Ooops! This is what computer scientists call a logic error. The program is syntactically correct and compiles, but does the wrong thing.

You job is to correct this program. Good luck!
Hint: You only need to modify one character...

Instructor Checkoff ...
Show your lab instructor your correctly running program.
Resources