Like most languages, Java comes with several data types built into
it such as int,float,
boolean,char that describe integer, floating point, boolean
and character data values, respectively. In Java these are referred
to as the primitive data types.
Note: The primitive
types in Java are not classes. They are not represented internally
in a class format. The language chose this approach to reduce
overhead and speed up execution. However, conceptually, the analogy
of classes and types holds true.
Each type has its own
- Memory location to hold a data value
- Special operations that apply to that data type alone.
For example, an int
type variable has a value represented in memory according to
the rules for the int
type. It has operations, such as the ++,--
increment and decrement, that apply to it but not to some other
data types such as double.
With the type declaration we can create a datum, for instance,
of the int
type:
int
i=5; // create one "instance" of integer type
where the JVM allocates memory
for that datum and will use that datum only with the code that carries
out operations
for the int
type.
Similarly data for floating point (FP) types like float
and double
have special memory
representation and bytecodes to deal with FP exponents and significands.
The boolean
primitive type internal representation holds only the values
true
or false
(internally an integer value of 1 represents true
and 0 for false.)
Only logic operations can be applied to a boolean
value. (Unlike C/C++, in Java you cannot treat an integer as a boolean
.)
Create Your Own Types
But why be stuck with only the data types that happen to come
with the language. Object oriented languages allow you to create
whole new types with their own data (fields)
and with their own operations (methods)
that apply to that internal data.
Suppose that the designers of Java had left out the boolean fromits
selection of primitive data types. We could create one with a custom
boolean class. In our boolean class we could create a storage location
using an integer variable to hold a data value of 0 to represent
false and 1 to represent true. Similarly, we would
create several methods to carry out various logic operations.
(In the Chapter 1: Tech
section we use the example of a complex
number class. Fortran includes complex numbers in its set of
data types but Java does not so you can create a class for them.)
This code shows how we might create a boolean class:
public
class
MyBoolean
{
int i;
final
static int TRUE = 1;
final static int
FALSE = 0;
public MyBoolean
(int j)
{
i = j;
}
public void
set (int
j)
{
i = j;
}
public int isTrue
()
{
if
(i > 0) return
TRUE;
else return
FALSE;
}
public int
AND (MyBoolean b)
{
if
(i==TRUE && b.get()
== TRUE)
return
TRUE;
else return
FALSE;
}
public
int OR (MyBoolean b)
{
if
(i==TRUE || b.get() ==
TRUE)
return
TRUE;
else return
FALSE;
}
}
|
This field holds the boolean data value.
Create
constants to represent true and false values.
A constructor passes
an initial value.
This method
used to put a new value.
This method tests the data and returns 0 or 1 accordingly.
Performs the AND operation between the stored value and
that of another boolean value.
Performs the AND operation between the stored value and
that of another boolean value.
|
Then with the MyBoolean
class you can carry out boolean operations as shown below with
a method in some other class:
void
doExample (MyBoolean b1, MyBoolean
b2)
{
if
(b1.AND(b2)) doSomething
();
if
(b1.OR(b2))
doSomethingElse ();
MyBoolean b3 = new
MyBoolean (MyBoolean.TRUE);
doSomeMethod (b3);
}
|
Two boolean
values passed as arguments.
AND
operation between the two booleans.
OR operation
between the two booleans.
Create a boolean type value that's true.
Use the value in a method invocation.
|
Here the statement
MyBoolean
b3=new MyBoolean (MyBoolean.TRUE);
is equivalent to the primitive type declaration:
boolean
b3 = true;
Similarly, the invocation of the logic AND
logic method in this statement
if
(b1.AND(b2)) doSomething ();
is equivalent to
if
(b1 && b2)) doSomething ();
if b1 and
b2 were
boolean types.
Data Types, Classes, Objects,
and Beyond
Here we related classes to the primitive data types. This is not
a perfect analogy, of course. You do not define new machine level
(or virtual machine level in the case of Java) instructions for
the class. Instead, the class definition uses primitives to hold
the data and the operations are written at the source code level.
The principle, however, is the same. The class definition creates
a new custom data type with its own operational capabilities.
With the primitives you create instances of a type with a declaration
such as
float
x = 5.0f;
This allocates 4 bytes of memory to hold the value 5.0 in memory
and the data is labeled as belonging to the floating-point type
so that only legal float operations can act upon it.
Similarly, once a class definition is available, instances of the
class can be created. This means that memory is allocated for all
the data fields in the definition. (The code for the operations
defined in the class is not duplicated with each instance since
Java knows how to find the code when its needed.) A special method
called the constructor initializes the data values when the instance
is created. Just as you can create multiple variables to hold float
values, you can create multiple instances of a class such as the
complex number class. Such instances of the class are called objects.
So the mechanics of class types is somewhat more clumsy than for
primitive types. We need to use the new
operator and constructors to create an instance of a class type
and we must invoke a method to carry out the operations.
(In C++ one can redefine symbols to act according to the operand
classes but Java, unfortunately, does not provide for such overloading
of operators symbols.)
Despite these shortcomings, it will become increasingly clear that
the ability to create such custom data types brings great programming
capabilities and conceptual clarity over traditional procedural
code.
Note that as you gain experience with classes, you will gradually
think about them less as custom types and will start to visualized
class instances truely as objects in the sense that they represent
self-contained entities with particular capabilities (that is, fields
and methods). Furthermore, additional OOP techniques such as inheritance
will take classes far beyond what plain old primitive data types
can do.
References
& Web Resources
Latest update: Oct. 16, 2004
|