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.
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.
* 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
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
Example 1 – Declare an int variable called salary and give it the value 32000. int salary; salary
= 32000;
int salary = 32000;
NAMING CONVENTION FOR VARIABLES
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
int x = 2.4; //This will be an error because 2.4 is not
an integer.
OUTPUTTING VARIABLES TO SCREEN
System.out.println(x);
System.out.println("The value of the variable x is " + x); EXAMPLE PROGRAM
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); } } |
|||||||||||||||||||||||||
|