touque.ca > Education Commons > Java > Resources

Datatypes & Operators

Primitive datatypes

The names of each of the following primitive datatypes are all keywords:

You might like to read these sections of the variables tutorial:

  1. Primitive Data Types
  2. Arrays
  3. Summary of Variables

and attempt the Questions and Exercises.

Operators

Read these sections of the Operators tutorial:

  1. Assignment, Arithmetic, and Unary Operators
  2. Equality, Relational, and Conditional Operators
  3. omit Bitwise and Bit Shift Operators
  4. Summary of Operators

and attempt the Questions and Exercises.

Floating-point comparisons

This section is drawn from Horstmann, 2002, pp. 192–193.

Since floating-point numbers have limited precision, it’s common practice to determine the equivalence of two such numbers by comparing their difference to a very small value called ε (lowercase epsilon), as in this sample program. However, the question arises: how small should ε be?

A common answer is 10-14, which could be rendered in code as final double EPSILON = 1E-14. But for very large numbers, a non-significant difference could easily be larger than 10-14.

To solve such problems, we should calculate the difference of the two numbers, determine what portion of the larger number this difference is, and compare that proportion to ε. Here’s one such implementation:

Math.abs(x - y) / Math.max(Math.abs(x), Math.abs(y)) <= EPSILON;

Of course, this implementation requires that both x and y are non-zero. If one of them is zero, all we can do is test whether the absolute value of the other is larger than ε:

// If x is non-zero:
Math.abs(x) <= EPSILON;
// otherwise:
Math.abs(y) <= EPSILON;

You might like to read:


Horstmann, C. (2002). Big Java. John Wiley & Sons, Inc. ISBN 0-471-40248-6.

touque.ca > Education Commons > Java > Resources

[This page last updated 2020-12-23 at 12h13 Toronto local time.]