Data Types - Numeric

Things To Do First

Things To Do Later

About Types

In Java (and other programming languages) we need to be specific about the kind of data we're using. Before we get into programming with Java, we need to learn about these different kinds of data and how they're used in your program code.

In Java there are two sets of types: primitive data types and object types (these are sometimes called reference types, also). Primitive types are probably the most familiar to you - they mostly include numbers and characters. Object types would only be familiar to you if you've done some object-oriented programming before. In an object-oriented language, programs are designed around classes and objects. For example, if you wanted to write a program for Sheridan that allowed students to register for courses, the generic objects might include Student, Course, Registration Form, and Fee Payment. Classes are the templates or definitions of objects in a program. For example, you might code a Student class. From this Student class, your program can create objects for each specific student that wants to register for a course. In this case, the type of data being used is of the Student object type.

In this session, we will focus on the numeric primitive data types. Most of these are numbers, but there are a couple of others, as you will see.

Numeric Data Types

There are two categories of numeric types in Java: integers and floating point numbers. Integers are what you might commonly know as "whole numbers". Integers can be positive or negative, but they have no fractional or decimal portion. Integers are often used for counting things because we generally count with whole numbers (e.g. 1, 2, 3, 4...). Integers should only be used for data when you know there is no way for it to contain a fractional or decimal portion. For example, you wouldn't use an integer for the price of a product, even if the price were ten dollars (10 is an integer); if the price went up by 50 cents, you'd have a price of 10.50 (10.5 can't be represented as an integer value).

Floating point numbers may have a fractional or decimal portion, so sometimes they are referred to as "decimal numbers". In programming we find this confusing, because when referring to numbers, the term "decimal" refers to the number system. The number system we use in every-day life is the decimal number system (or "base 10" number system). In technology, you might also use the binary number system, hexadecimal number system, or octal number system. Therefore, when referring to data that can (or could) contain decimal values, we prefer to call it a floating-point number, and not a decimal number.

Table 2.1 on page 45 of your textbook summarizes the different kinds of numeric data used in Java: short, byte, int, long, float, and double. Each type has a specific range of values that it pertains to, and a storage size in bits or bytes. For example, the int data type can store integer numbers from -2,147,483,648 to +2,147,483,647 and takes up 4 bytes of storage space (The term "signed" in the last column indicates that the integer can be positive or negative. If a data type is listed as "unsigned" it means that it only holds positive numbers. Unsigned integers have a higher positive range. For example, a 2-byte signed integer value can have a range from -32,768 to 32,767, but an unsigned 2-byte integer value can have a range from 0 to 65,535.

Important!
Precision is a measure of how many digits you include in a number. For example, 3.14159 is less precise than 3.141592653589793.

Don't confuse precision with accuracy. Accuracy is a measure of how correct a value is. For example, saying we average about 35 students per class would be accurate, and saying we average 30 to 40 students would still be accurate, but less accurate than saying 35. Saying we average 100 students per class would not be accurate at all. Saying we average 32.5 students per class would be more precise than saying 32 or 33.

What does the term "floating-point" mean? When you look at Table 2.1, you'll see that there are two kinds of floating point numbers in Java: Single-precision floating-point (float), and double-precision floating point (double). You'll notice that the range of values for these two types are displayed in scientific notation. To understand the term "floating-point", we need to explore (and for some of you, review) scientific and exponential notation.

Scientific Notation

Let's start with a floating-point number such as 123.456. To convert this into scientific notation, you would move the decimal over enough spaces to ensure that there is only one digit to the left of the decimal. So 123.456 would become 1.23456. We moved the decimal point over 2 spaces to the left to arrive at the number 1.23456. However, 1.23456 doesn't have the same meaning or value as 123.456, so we have to indicate that we've moved the decimal to arrive at the smaller number. When you move the decimal to the left, you are really just dividing the number by some power of 10, so we would represent 123.456 in scientific notation as 1.23456 x 102 (said as "1.23456 times 10 to the power of 2").

We chose 102 because we moved the decimal point two positions to the left, which would be the same as dividing by 100. Therefore, the notation 1.23456 x 102 = 1.23456 x 100 = 123.456.

Note

Terminology

Given the value 1.23456 x 102, we use the following terms for each of the number's components:

  • 1.23456: coefficient
  • 10: base
  • 2: exponent

How would you convert the value .0023 to scientific notation? First we would have to move the decimal point 3 spaces to the right, turning the number into 2.3 (we drop the "leading zeros", which are 0's that appear to the left of the number). Moving the decimal point to the right instead of the left, we are in fact multiplying the number by a power of 10 (in this case, .0023 * 1000 = 2.3. Therefore, when we represent this as scientific notation, we have to use a negative exponent: 10-3. The value .0023 ends up being represented in scientific notation as 2.3 x 10-3.

Exercise:

For practice, try converting these to scientific notation:

  1. 34,982.58
  2. .004785
  3. .123467

Now try converting these values from scientific notation into regular floating-point numbers:

  1. 9.7824 x 103
  2. 2.222 x 107
  3. 1.987 x 10-2
  4. 5.5 x 10-4

[solutions]

Exponential Notation

Exponential notation is figured out the same way as scientific notation; it just has a different format or syntax. To represent a number in exponential notation, you just replace the "x 10" with the letter "e" or "E" and write the exponent in regular script instead of superscript. For example, to represent 123.456 or 1.23456 x 102 in exponential notation, you would write 1.23456e2. To represent .0023 or 2.3 x 10-3 you would write 2.3e-3

Exercise:

Convert the following into exponential notation:

  1. 34,982.58
  2. .004785
  3. .123467

[solutions]

So what does all this have to do with the term "floating-point"? When the computer stores a floating-point number, it converts it first into a binary form of scientific notation. It uses some of the storage space for the the sign (+ or -), some for the exponent, and the rest for the coefficient. The coefficient is the portion of the number that appears before the "e" or "E" in exponential notation, or the x 10 in scientific notation. For example, in the number 2.3 x 10-3 and 2.3e-3, 2.3 is the coefficient and -3 is the exponent. The computer has to convert a regular decimal number like 123.456 into this special notation before storing it, and since the decimal point is being moved, or "floating", we call it a floating-point number.

The number of bytes a floating-point number requires is an indication of its precision. A single-precision floating-point number in Java is 4 bytes. The first 23 bits are used to store the coefficient, the next 8 bits are used to store the exponent, and the last bit is used to store the sign of the number. A double-precision floating-point number takes up 8 bytes of space. The first 52 bits are for the coefficient, the next 11 bits are for the exponent, and the last bit is for the sign of the number. Since the amount of space allowed for the coefficient is greater in a double-precision number, you can store a number with greater precision in a double type than you can in a float type. If you were going to store the value for PI (3.14159265....) in Java, you would get more digits after the decimal point if you stored it as a double, than if you stored it as a float.

Data Type Description # bits for
Coefficient
# bits for
Exponent
# bits for
Sign
floatSingle-precision floating-point 2381
doubleDouble-Precision floating-point 52111

About Strings and Characters

We'll cover the primitive type char (character) and the object type String in a later session but for now, it's important to note that String literals are always enclosed in double-quotes ("") and char literals are always enclosed in single-quotes (''). Char type values also can contain only one single character, except for a few exceptions that you'll learn about later. So, for example:

'a' and '1' are characters
"a" and "1" are Strings
1 is an integer
1.0 is a floating point

Exercise

For each of the following values, identify whether or not the value is a string, character, integer number, floating point number, or invalid.

  1. "Programming is fun." [Solution]
  2. "PROG10082" [Solution]
  3. 10082 [Solution]
  4. "10082" [Solution]
  5. 10,082 [Solution]
  6. "$5.95" [Solution]
  7. '2' [Solution]
  8. 2 [Solution]
  9. 2.99991 [Solution]
  10. '\t\n' [Solution]
  11. "\t\n" [Solution]

Null Values for Numerics

One last thing to cover about data in Java is null values. For numeric types, this is pretty straight-forward - the null value is 0. For example, a customer might have an outstanding balance of 0, or you might have 0 students in a class.

You don't have to worry about writing 0 for integers and 0.0 for floating-points, although many programmers do this often.

Note
For a great book about the philosophy and story of the value 0, check out Zero: A Biography of a Dangerous Idea by Charles Seife.