A safe variant of the unsafe integer arithmetic of JavaTM

  • Authors:
  • Jürgen F. H. Winkler

  • Affiliations:
  • Friedrich-Schiller University, Institute of Computer Science, D-07740 Jena, Germany

  • Venue:
  • Software—Practice & Experience
  • Year:
  • 2002

Quantified Score

Hi-index 0.00

Visualization

Abstract

Computers are finite machines and, therefore, the arithmetic operations in a programming language are different from their mathematical counterparts. These restrictions seem not to have been, in general, fully appreciated in programming languages and in computer science textbooks. One example is the programming language Java, which makes it difficult to warn the user in cases in which arithmetic operations produce incorrect results. In this paper we look at integer arithmetic in Java and develop a safe variant of the arithmetic operations in Java. The design of the safe variant of +, -un, -bin, *, / and ** for the types byte, short, int and long reveals a number of deficiencies of Java in integer arithmetic, floating point arithmetic and program structure. Some of these deficiencies are also present in other contemporary programming languages. The paper therefore ends with some proposals for the design of the arithmetic elements of programming languages.