# Datatypes & Operators

## Primitive datatypes

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

**byte:**8-bit signed integer ranging from -128 to 127 inclusive; default: 0**short:**16-bit integer ranging from -32 768 to 32 767 inclusive; default: 0**int:**32-bit integer ranging from -2 147 483 648 to 2 147 483 647 inclusive; default: 0**long:**64-bit integer ranging from -9 223 372 036 854 775 808 to 9 223 372 036 854 775 807 inclusive; default: 0L**float:**single-precision 32-bit floating point values following the IEEE 754 standard; default 0.0f**double:**double-precision 64-bit floating point values following the IEEE 754 standard; default 0.0d**boolean:**the logical values`true`

and`false`

; default:`false`

**char:**a single 16-bit Unicode character ranging from`'\u0000'`

to`'\uFFFF'`

; default`'\u0000'`

(NUL); see also Unicode character table

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

- Primitive Data Types
- Arrays
- Summary of Variables

and attempt the Questions and Exercises.

## Operators

Read these sections of the Operators tutorial:

- Assignment, Arithmetic, and Unary Operators
- Equality, Relational, and Conditional Operators
*omit*Bitwise and Bit Shift Operators- 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:

- What Every Computer Scientist Should Know About Floating-Point Arithmetic.
- The Floating Point Guide: Comparison
- Mathematics with Java

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.]

© 2007–2021 Hersch, Bear & Company Limited. All rights reserved. “Grammar Authority,” “grammarauthority.com,” “touque.ca,” and “Canada’s Thinking Cap” are trademarks of Hersch, Bear & Company Limited. All other trademarks and copyrights are the property of their respective owners.

Privacy Policy | Licence to Copy | How to cite | Contact us