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.
More than one variable can be declared in the same line.
Two types of Data Types
Category | Types | Size |
Range or Usage |
Null value |
Integer | byte |
One byte |
-27 to 27 and zero | 0 |
Character | char | Two bytes | All 65536 UniCode Characters |
'\u0000' |
Fractional | float | Four bytes | For Single precision |
0.00 |
Logical | boolean | One bit | True or False |
false |
When assigning a value to a character, use of single quote is required.
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.
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:
Variables can be primitive or non-primitive data types. Variables are denoted by Identifiers.
Any identifier name cannot begin with a number.
Local Variables
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
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 ;
}
}
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
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:
Following up on our example, let's say that we have another Object of the Exercise class, 'test'.
Then we can write:
test.answer = 45;
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.
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.)
class Exercise
{
static final double waterPressure = 25.16;
public static void main(Stringp[] args)
{
System.out.println(waterPressure);
}
}