Java

TOPIC 06 – VARIABLES

 

 

LESSON NOTE

 

 

VARIABLES

 

In Math class, when solving a problem, you often use letters to represent unknown values.  These letters are called variables.

 

In programming, variables are the same.  They are a symbolic name to which you can store a value.

 

However, in programming, instead of using a single letter to represent a variable, we often use a complete word.

 

LIMITED STORAGE

 

Unlike in the world of math, computer variables do not have an unlimited range.  They are limited by the amount of memory storage that we provide for them. 

 

EXAMPLE

 

Integers in Java are given 4 bytes (or 32 bits) of memory.  If a person were to be very very patient and tried to list all combination possibilities for a 32-bit number consisting of zeros and ones, it would probably start like this…

 

 

It turns out that there are over 4 billion possible combinations of zeros and ones.  While that is a lot, it is not infinite.

 

BONUS INFO

 

The first bit of a number is usually used to specify whether the number is positive or negative.  All other bits represent the number itself. 

 

So for a 32-bit number, we have 31 bits used for the number.  Doing 231, we get 2 147 483 648.  And that happens to be the maximum number (positive or negative) that can be stored in a Java integer variable.

 

DIFFERENT TYPES OF VARIABLES

 

Sometimes, there are variables that are likely to store very big numbers.  At other times, there are variables that we know will only contain small numbers. 

 

For example, a variable that has to store the amount of seconds in a year will have a large number.  On the other hand, a variable that keeps track of the amount of errors that Mr. Campeau makes in his lifetime will obviously contain a tiny number!  (haha)

 

The larger the numbers, the more memory is required to store the numbers.  To increase efficiency, Java allows the user to create different types of variables that will meet the needs of the information that is to be stored.

 

JAVA’S VARIABLE TYPES

 

In programming, a data type is referred to as a variable type.  The most commonly used variable or data types are int (integers) and double (real numbers). 

 

Here is a listing of several of Java’s data types along with their range and the amount of memory they take up.

 

Data type

Memory

Range

float

4 bytes

1.40129846432481707e-45 to 3.40282346638528860e+38 *

double

8 bytes

4.94065645841246544e-324 to 1.79769313486231570e+308 *

byte

1 byte

-128 to127 (whole numbers)

short

2 bytes

-32768 to 32767 (whole numbers)

int

4 bytes

-2147483648 to 2147483647 (whole numbers)

long

8 bytes

-9223372036854775808 to 9223372036854775807 (whole numbers)

boolean

1 bit

true, false

* positive or negative

 

WHY HAVE DIFFERENT TYPES OF VARIABLES?

 

The main reason for having different types of variables is efficiency.  If you are working with whole numbers that never exceed 127, then you can store your information in a single byte of memory (byte variable) as opposed to 4 bytes of memory (int variable).

 

That said, for our purpose, we will almost always use either int or double variables.

 

DECLARING VARIABLES

 

  • Before using a variable in a program, it must be declared.  This allows for the correct amount of memory space to be set aside for that variable.

 

  • We declare a variable by writing:

 

                        varType varName;

 

Example 1 – Declare an int variable called grade.

 

            int grade;


 

Example 2 – Declare a double variable called tax.

 

                        double tax;

 

WHAT HAPPENS IN MEMORY

 

After declaring a variable, its required memory space is reserved and set aside for your program.  The image below shows both variables above in memory.  Note that the image is a little over simplified because in reality, the tax variable would take up twice the space of the grade variable.

 

 

ASSIGNING VALUES TO VARIABLES

 

  • To assign a value to a variables, we use the “=” symbol.

 

Example 1 – Declare an int variable called salary and give it the value 32000.

 

                        int salary;

          salary = 32000;

 

  • We can also declare a variable and assign it a value on the same line. 

 

            int salary = 32000;

 

  • The first time a variable is given a value, it is called initializing the variable.  Before a variable can be used in a program, it has to be initialized.

 

  • The image below shows how the value is stored at the location where the salary variable refers to.

 

 

NAMING CONVENTION FOR VARIABLES

 

  • Rules for choosing variable names:
    • Must begin with a lower case letter (not a number)
    • Can contain any letters and numbers
    • Cannot contain spaces
    • Should have interior words starting with a capital letter

 

Example – Which variable names follow the naming rules?

 

a) numberOfStudents                                 (good)

 

b) numberofstudents                                   (bad – rule 4)

 

c) NUMBEROFSTUDENTS                       (bad – rule 1)

 

d) number of students                                 (bad – rule 3)

 

 

MATCHING TYPES

 

  • It is important that the left side and the right side of an assignment statement be of the same type.  Otherwise, the Java compiler will give you an error.

    Example:

 

int x = 2.4;   //This will be an error because 2.4 is not an integer.

 

  • Later we will see that there are some exceptions to consider.

 


OUTPUTTING VARIABLES TO SCREEN

  • You can output a variable x to screen by using:

 

System.out.println(x);

 

  • You can combine text (known as Strings) and variables together by using the + symbol:

 

System.out.println("The value of the variable x is " + x);

 

EXAMPLE PROGRAM

 

  • The following program will output the sum of the variables a and b.

 

public class Sum

{

   public static void main(String[] args)

   {

      int a = 10;

      int b = 20;

      int sum = a + b;

      System.out.println("The sum of a and b is " + sum);

   }

}