The BASE for JAVA LEARNING

by PRO FE S S OR


SECTION III - Data Types, Variables, and Constants

Step 7: Data Types

Declaration of Variables

Data types are used in any programming language to declare the variables used. For example: int year; // here the variable year is of the integer type.

More than one variable can be declared in the same line. For example: int year, month; They can also be declated and initialized in the same line using the assignment Operator (to be discussed later). Example: int year=40, month=1;


Two types of Data Types

  • The primitive
  • The non-primitive Data Types.


PRIMITIVE DATA TYPES

Category

Types

Size

Range or Usage

Null value

Integer

byte 
short 
int 
long 

One byte 
Two bytes 
Four bytes 
Eight bytes 

-27 to 27 and zero
-215 to 215 and zero
-231 to 231 and zero
-263 to 263 and zero

     0

Character

char

Two bytes

All 65536 UniCode Characters

     '\u0000'

Fractional

float
double

Four bytes
Eight bytes

For Single precision
For Double precision

     0.00

Logical

boolean

One bit

True or False

     false

When assigning a value to a character, use of single quote is required.
Example: char letter = 'A';

Other examples under
Section 4, Step 11, the Assignment Operator.


NON-PRIMITIVE DATA TYPES

Non-Primitive Data Types as the name implies are those types that are not primitive. In essence, non-primitive types can be user defined, or they can be classes defined in the JAVA language.

For instance, "String" is a non-primitive type as it is a class defined in JAVA.

Arrays and classes as defined by the programmer can also be considered Non-Primitive Data Types.

When assigning a value to a String, use of double quotes is required.
Example: String name = "Professor V";

Other examples under Section 4, Step 11, the Assignment Operator.

Click here to go to the JAVA Mini-Course page


The BASE for JAVA LEARNING

by PRO FE S S OR


SECTION III - Data Types, Variables, and Constants

Step 8: Local and Class Variables

There are three kinds of Variables according to where they are declared in the program:
  • Local Variable
  • Class Variable
  • Instance Variable (Step 9)
Variables can be primitive or non-primitive data types. Variables are denoted by Identifiers. Any identifier name cannot begin with a number.


Local Variables Declared within a method. In the example below, year is a local variable (int: primitive data type). It is declared within the main method. Also greeting is a local variable (String: non-primitive data type), declared within the displayGreeting() method.

class Exercise
{
      public static void main(Stringp[] args)
      {
             int year ;
             year = 2002;
             System.out.println(year);
      }
      void displayGreeting()
      {
             String greeting="Welcome to our website..." ;
             System.out.println(greeting);
      }
}

Class Variables Declared outside a method, preceded by the keyword "static". Class variables can be used by a all methods of the same class. When referenced in another class, it should be preceded by the name of the class followed by a dot. Example: Exercise.month // Exercise is the class where the variable month is declared. If a class variable has the same name as a local variable within a method, it can only be differentiated by writing the name of the class preceding the name of the variable followed by a dot.

Say, in the class Exercise, we have a local variable "month" in a method, and there is a class variable also named "month". Then in order to avoid name conflict, the class variable in that method will be accessed as "Exercise.month", and the local variable is accessed as "month". This is only necessary when using the same name for both class and local variables in a given method.

In the example below, amount and name are both class variables.

Notice that both class variables can be accessed within any method. In this example we are not using any local variables.

class Exercise
{
      static int amount;
      static String name;
      public static void main(Stringp[] args)
      {
             name = "Pete";
             System.out.println(name);
      }
      void setBaseAmount()
      {
             amount=1000 ;
      }
}


Click here to go to the JAVA Mini-Course page


The BASE for JAVA LEARNING

by PRO FE S S OR


SECTION III - Data Types, Variables, and Constants

Step 9: Instance Variables

Now, that you have a pretty good idea of the difference between a class variable ('static') and local variable, let's look at what is an Instance Variable.
Instance Variables Instance variables are declared outside a method as class variables are. However, they ARE NOT preceded by the keyword "static". Instance variables are unique in the sense that they are accessed only directly by Instance methods (to be covered later) of the same class or by Objects of their class. (also to be covered later.) This means that a class method (to be covered later) can access an Instance variable, only by referencing an Object of that class (class Instantiation).

Let's say that we have the class Exercise, and an Instance variable 'answer'. In order to access 'answer' from any class method, it is necessary to create an Object of the class Exercise.

For sake of discussion, let's say that we have an Object of the Exercise class, 'quiz'. Then if we want to access 'answer' from within a class method of Exercise, we will write: quiz.answer (Notice the object is used as a reference). If we have more than one Object of a given class, the Instance variable can have a different value for each different Object.

Following up on our example, let's say that we have another Object of the Exercise class, 'test'. Then we can write:

quiz.answer = 56;
test.answer = 45;
Each Object has its own unique value for the Instance variables.

Notice that this is not the case with class variables (static), as the value stored in a class variable is the same for the class, and therefore the same for all Objects of that class.

Click here to go to the JAVA Mini-Course page


The BASE for JAVA LEARNING

by PRO FE S S OR


SECTION III - Data Types, Variables, and Constants

Step 10: Constants


CONSTANTS

Usually the identifiers used for constants are all UPPER CASE. (this is only a convention.)

In order to declare and define a constant we use the keyword 'final' preceding the data type and identifier. Example:

final int year = 20;

Constants are declared and initialized in the same line. And as the keyword 'final' implies, values cannot changed.

As variables, constants can be local, class or Instance. However, in most usages, constants are of the class ('static') kind, as their values are to be shared by every method of that class. Constants are also used in Interfaces. (to be covered later.)

See Example below:
class Exercise
{
      static final double waterPressure = 25.16;
      public static void main(Stringp[] args)
      {
             System.out.println(waterPressure);
      }
}


Click here to go to the JAVA Mini-Course page