Java Reference
In-Depth Information
Note that the body (implementation or the “how” part) of the getRangeSum procedure has changed between
Listing 1-2 and Listing 1-3. However, the users of the getRangeSum procedure are not affected by this change at all
because the details of the implementation of this procedure were kept hidden from its users by using abstraction. If
you want to compute the sum of all integers between 10 and 20 using the version of the getRangeSum procedure as
shown in Listing 1-3, your old code (shown below) is still valid.
int s1 = getRangeSum(10, 20);
System.out.println(s1);
You have just seen one of the greatest benefits of abstraction, in which the implementation details of a program
(in this case, a procedure) can be changed without warranting any changes in the code that uses the program. This
benefit also gives you a chance to rewrite your program logic to improve performance in the future without affecting
other parts of the application.
I will consider two types of abstraction in this section:
Procedural abstraction
Data abstraction
Procedural Abstraction
Procedural abstraction lets you define a procedure, for example, getRangeSum , that you can use as an action or a task.
So far, in this section, I have been discussing procedural abstraction. Abstraction by parameterization and abstraction
by specification are two methods to achieve procedural abstraction as well as data abstraction.
Object-oriented programming is based on data abstraction. However, I need to discuss data type briefly before I
discuss data abstraction. A data type (or simply a type) is defined in terms of three components:
A set of values (or data objects)
A set of operations that can be applied to all values in the set
A data representation, which determines how the values are stored
Programming languages provide some predefined data types, which are known as built-in data types. They
also let programmers define their own data types, which are known as user-defined data types. A data type that
consists of an atomic and indivisible value, and that is defined without the help of any other data types, is known as a
primitive data type. For example, Java has built-in primitive data types such as int , float , boolean , char , etc. Three
components that define the int primitive data type in Java are as follows:
An
int data type consists of a set of all integers between -2147483648 and 2147483647.
Operations such as addition, subtraction, multiplication, division, comparison, and many
more are defined for the int data type.
A value of
int data type is represented in 32-bit memory in 2's compliment form.
All three components of the int data type are predefined by Java language. You cannot extend or redefine the
definition of the int data type as a programmer. You can give a name to a value of the int data type as
int n1;
The above statement states that n1 is a name (technically called an identifier) that can be associated with one
value from the set of values that defines values for int data type. For example, you can associate integer 26 to the
name n1 using an assignment statement as
n1 = 26;
 
Search WWH ::




Custom Search