Home : Course Map : Chapter 2 : Sci & Eng :
More About Integers in Java
JavaTech
Course Map
Chapter 2

Introduction
Essentials
Structure

Keywords
Primitive Types
Comments
Literals
Expressions
Operators
Statements
Casts & Mixing
Strings
Console Output 
   Demo
Exercises

    Supplements
Conditional: if-else
Repetitions
Flow Control

Java vs C/C++
JVM Instructions 1

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

We provide some additional information here about integers, as well as stressing again some points made previously. This continues the discussion of integers in Chapter 2: Java : Primitive data types

Java offers 5 types of integer:

  • byte - 8-bit, signed
  • short - 16-bit, signed
  • int - 32-bit, signed
  • long - 64-bit, signed
  • char - 16-bit, can be treated as unsigned integer in some situations

The table showed the ranges of values of each type. The pages Chapter 2: Java : Casting & Mixing and Chapter 2: Tech : More Casting & Mixing discuss how the types can be converted from one to another.

Here are some other aspects of using integers:

  • No overflow and underflow warnings - unlike the case for the floating point types, there are no special values for the integer types that indicate that the maximum value has been exceeded or that a value has gone below the negative minimum values. Exceeding the maximum value will flip the value into the negative range while continuing past the minimum negative value will flip the value into the positive range.

  • Maximum and minimum values constants - the wrapper classes (to be discussed later) provide constants that give the maximum and minimum values for each type. These can can be useful for testing for overflows/underflows since, as discussed above, no exceptions are thrown in such cases.

    • byte
      • Byte.MIN_VALUE  -27 = -128
      • Byte.MAX_VALUE  27-1 = 127
    • short
      • short.MIN_VALUE  -215 = -32768
      • Short.MAX_VALUE  215-1 = 32767
    • int
      • Integer.MIN_VALUE  -231 = -2147483648
      • Integer.MAX_VALUE  231-1 = 2147483647
    • long
      • Long.MIN_VALUE  -263 = -9223372036854775808l
      • Long.MAX_VALUE  263-1 = 9223372036854775807l

  • Divide by zero errors - unlike the floating point types ( float and double), integer divide by zero leads to a run time exception that will stop the processing unless caught as discussed later.

 

References & Web Resources

 

Latest update: Oct. 15, 2004

            Tech
Arithmetic Ops
Math Class
More on Integers
FP : Overview
FP : Java  
  
Demo 1
More Mix/Cast
  Demo 2
Exercises

           Physics
Differential Eq.
Euler Method
  
Demo 1
Predictor-Corrector
  
Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.