<< Chapter < Page Chapter >> Page >

Other data types involve numbers with fractional parts. We generally refer to these types as floating-point types, because a decimal point can float back and forth, separating the whole-number part from the fractional part. (After a while, we get lazy and refer to these simply as floating types.)

An example of integer and floating-point types

The scenario in Figure 1 illustrates integer and floating-point types.

Figure 1 . Calculating the number of cans of paint to purchase.

Consider the problem of determining the number of cans of paint that must be purchased in order to paint all 15 tables in a restaurant.

The number of tables is an integer type. We don't want to paint 14.6 tables or 15.7 tables. We want to paint exactly 15 tables. (We don't want to have a fractional part of a table left unpainted.)

We know that one can of paint will cover 3.6 tables. The number of tables that can be painted with one can of paint is a floating-point value because it contains a whole-number part and a fractional part.

A little arithmetic tells us that 4.167 cans of paint will be required to paint all 15 tables. This is also a floating-point value, because it has a whole-number part and a fractional part.

However, the man at the hardware store is unwilling to sell us 4.167 cans of paint. He requires us to specify the number of cans of paint as an integer value. In this case, we will need to purchase 5 cans of paint in order to have enough paint available to paint all 15 tables, with a little paint left over.

Other types of data

Although all data in a computer is stored in numeric format, some data types conceptually have nothing to do with numeric values, but deal only with the concept of true or false or with the concept of the letters of the alphabet and the punctuation characters. I will have more to say about these types later.

Type specifications

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

  1. The set of all possible data values that can be stored in an instance of the type. (We will learn more about the concept of an instance shortly.)
  2. The operations that you can perform on that instance alone, or in combination with other instances. (For example, operations include addition, subtraction, multiplication, division, etc.)

What do I mean by instance ?

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

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

An example, the short data type

There is a data type in Java, C++, and C# known as short . If you have an instance of the short type in Java, the set of all possible values that you can store in that instance is the set of all whole-numbers ranging from -32,768 to +32,767.This constitutes a set of 65,536 different values, including the value zero.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Ap computer science a, clarification of the java subset. OpenStax CNX. Oct 03, 2015 Download for free at https://legacy.cnx.org/content/col11279/1.5
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Ap computer science a, clarification of the java subset' conversation and receive update notifications?

Ask