Friday, March 6, 2009

4 - Variable and Constant

Variable Declaration
The syntax for attribute declaration is exactly the same with what we have done when we declare methods. The syntax is like this :
[modifiers] type name_of_variable or [modifiers] type name_of_variable = [value].
  1. This form : [modifiers] type name_of_variable is only for declaring a variable without giving the the initial value of the variable.
  2. The other form is : [modifiers] type name_of_variable = [value] is for declaring a variable and GIVING it the initial value.
  3. [] (square bracket means anything inside this bracket is optional). Modifiers can be any java keywords. The most common are private, public, and static that we have encounter before.

In terms of its visibility a variable can be local if it is declared inside a method, it can only be seen inside the method where it is declared. But there is a slight difference if we compare that with the rules listed above. The syntax for declaring such variable is very short :
  1. type name_of_variable if you only want to declare a variable without initializing a value for it.
  2. type name_of_variable = valueif you need to declare an initial value.
    In fact WE ARE NOT ALLOWED to place a modifier (such as public, private, etc) when declaring a local variable.
So what is the type. Type represents the data type that can be stored in a memory are reserved for that variable. There are several types that will be of our interest :
  1. byte : the range of value is from -128 to 127 (8 bits long)
  2. short : the range of value is from -32,768 to 32,767 (16 bits long)
  3. int : the range of value is -2,147,483,648 to 2,147,483,647 (32 bits long)
  4. long : the range of value is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (64 bits long).
  5. float : 32 bits floating point or real number point number.
  6. double : 64 bits floating point or real number point number.
  7. char : 8 bits long it holds a character value. At one point of time it store one only character.
  8. boolean : a variable of type boolean can only store a logical value true or false.
Before using any variables you have to declare them, then you can assign any value that matches to their type.
  • Examples for declaration of several variables without initialization :
float floatHeight;
int intStudentNumber;
char charGrade;

  • Examples for declaration of several variables WITH initialization :float
floatWeight = 100.24F;
int intMarbleCount = 20;
char charStudentGrade = 'A';
boolean boolIStillAlive = true;

A variable can only store one value at a time, but we can change it. We can change what is stored inside a variable by assigning a legal value to it. There are 4 ways to assign a value to a variable :

  • Assigning a value directly to a variable. Suppose a Grade of a student is F (fails he never studies) : charGrade = 'F';
  • The other way is by giving a value of another variable to one variable
charStudentGrade = 'F';
charGrade = charStudentGrade;

  • We can also assign the result of an arithmetic operation to a variable :
float floatUnitPrice = 100.12F;
float floatKgOrdered = 210.01F;
float floatTotalPrice = (floatUnitPrice * floatKgOrdered);

  • We can assign the result of a function execution to a variable. WE DO NOT HAVE ANY EXAMPLE FOR THIS YET.
Constant
Do we have another type of “variable” ? Yes it is called constant. A constant represents a value in a memory area that can not be changed at all. A constant is named with capital letters. Here is an example of a constant declaration :
final char TOP_GRADE = 'A';

A constant's value can not be changed forever and ever. Its value stays happily ever after.

Heap Memory and Stack Memory
Variables that are declared inside a method, known as local variables, are stored inside stack memory area, while attributes reside inside heap memory area. Heap memory stores any information about objects while they are needed by a program, while stack memory stores memory that are suitable for a short period of time.