<< Chapter < Page Chapter >> Page >

Analogous to different types of data

So, why am I telling you this? I am telling you this to illustrate the concept that different types of currency are roughly analogous todifferent data types in programming.

Purchasing transactions were type sensitive

In particular, because there were three different types of currency involved, the differences in the types had to be taken into account in anypurchasing transaction to determine the price in that particular currency. In other words, the purchasing process was sensitive to thetype of currency being used for the purchase (type sensitive) .

Different types of data

Type-sensitive programming languages deal with different types of data. Some data types such at type int involve whole numbers only (no fractional parts are allowed) .

Other data types such as double involve numbers with fractional parts.

Some data types conceptually have nothing to do with numeric values, but deal only with the concept of true or false ( boolean ) or with the concept of the letters of the alphabet and the punctuation characters ( char ) .

Type specification

For every different type of data used with a particular programming language, there is a specification somewhere that defines two importantcharacteristics of the type:

  1. What is the set of all possible data values that can be stored in an instance of the type (we will learn some other names for instance later) ?
  2. Once you have an instance of the type, what are the operations that you canperform on that instance alone, or in combination with other instances?

What do I mean by an instance of a type?

Think of the type specification as being analogous to the plan or blueprint for a model airplane. Assume that you build three modelairplanes from the same set of plans. You will have created three instances of the plans.

We might say that an instance is the physical manifestation of a plan or a type.

Using mixed types

Somewhat secondary to the specifications for the different types, but also extremely important, is a set of rules that define whathappens when you perform an operation involving mixed types (such as making a purchase using some yen currency in combination with some dollar currency) .

The short data type

For example, in addition to the integer type int , there is a data type in Java known as short . The short type is also an integer type.

If you have an instance of the short type, the set of all possible values that you can store in that instance is the setof all the whole numbers ranging from -32,768 to +32,767.

This constitutes a set of 65,536 different values, including the value zero. No other value can be stored in an instance of the type short . For example, you cannot store the value 35,000 in an instance of the type short in Java. If you need to store that value, you will need to use some type other than short .

Kind of like an odometer

This is somewhat analogous to the odometer in your car (the thing that records how many miles the car has been driven) . For example, depending on the make and model of car, there is aspecified set of values that can appear in the odometer. The value thatappears in the odometer depends on how many miles your car has been driven.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask