Java Reference
In-Depth Information
Chapter 3
Classes and objects in Java
3.1 Introduction
In Java the “class” is paramount. Essentially all coding resides within class defi-
nitions. Here and in the following chapters we develop the concepts and coding
for classes and objects in Java.
For those new to object-oriented programming (OOP) the learning curve can
be rather steep because several concepts must be understood before the overall
picture comes into focus. The Web Course Supplements section for Chapter 3
offers additional introductory material to help get these concepts across.
Note that throughout the topic we use the terms object and instance inter-
changeably.
3.2 Custom data types
In Chapter 2 we discussed Java primitive data types such as int , float , and
boolean . Data of a given type means that memory is reserved for a value of
that type and that only operations specific to that type can act upon the data.
So, for example, a float value has 4 bytes of memory allocated for it with
the sign, exponent, and significand bits arranged according to the representation
discussed in Chapter 2. When an operation such as an addition or multiplication
occurs upon a float value, the JVM executes floating-point operations that carry
out the proper procedures for addition and multiplication with significands and
exponents. For integer addition and multiplication the JVM executes a different
set of operations unique to integer type data.
In conventional languages, you are stuck with only the data types that come
with the language. Languages like C, C
, and even modern versions of Fortran
permit the definition of data types called structures, but these are just conve-
nient ways to group related pieces of data together. Data structures have no
innate “behavior” associated with them. You cannot “add” two data structures
together.
Object-oriented programming, however, lets the programmer define new data
types that include data along with operations (or behavior) unique to that custom
++
57
Search WWH ::




Custom Search