Java Notes
Integers
What you need to know about integers in your first programming course
This page has the topics that you will need to use in a first programming course. There is more to learn about integers, and some courses may require more, so there are links to additional topics that are often dealt with in a second or later programming course.
What are integers? Integers are whole numbers, for example, -35, 0, 2048, .... Java distinguishes this from floating-point numbers, which are number with a fractional part (eg, 3.14, 2.5).
The int type. Although there are several kinds of integer types, the only one that is used for beginning programming is int. After learning the basics of programming, you will have to learn about the other integer types, which are very useful in certain cases.
Binary. Although integers are represented in binary inside the computer, you will write numbers in the normal, human-friendly, decimal system in your programs. Java converts decimal numbers you write in your source program and input data into binary numbers internally and converts results back to decimal when they are displayed, so you never have to worry about using binary.
Calculations with integers
The usual operations of addition (+), subtraction (-), multiplication (*), and division(/) can be used with integers. In addition the remainder operator, % (often called "mod"), that gives the remainder after an integer division is often used.
Comparisons. Integers can be compared with the <, <=, ==, !=, <=, and < operators.
Converting integers to and from String
Readable and typable input and output of integers requires conversion between the internal binary form and the the readable character string form. Computers could do arithmetic using a character representation of integers, but it so vastly faster to use binary that all high-performance programming languages use binary representation internally. This is covered more in more detail elsewhere, but in summary you can convert and int to String by concatenation. Eg,
int i = 256; String s = "" + i; // Converts the contents of i to a string.
To convert a string to an int requires a library method. Eg,
String s = "123"; int i = Integer.parseInt(s); // Converts the String s to an integer.
Primitive hardware integer types and additional classes.
- Primitive types - This is what you will use in all introductory programming
- The are four types of integers in Java:
byte
,short
,int
,long
(andchar
, but more on that later). The most common is int, which should be your first choice for all programming unless you have a good reason to use something else. All integers (exceptchar
) are stored in signed, two's-complement, format.char! Technically,
char
is an unsigned integer type altho it is almost exclusively used to store characters. Making it integer is largely because of Java's legacy from C++. Don't usechar
for integers unless you are sure of what you're doing. - Wrapper classes
- Corresponding to each primitive type, there is a class. The primary use
of the class is for the static conversion methods (eg,
Integer.parseInt(x)
). Another use is to represent integer values when an object is required, as with the Collections data structure classes. The integer types and their wrapper classes are as follows.Primitive type Wrapper class int Integer byte Byte short Short long Long - BigInteger - Unlimited precision
- BigInteger is used where unbounded arithmetic is important. It is much less efficient than using a primitive type, but is the best choice when integer values exceed the hardware limits of the primitive types.
How Java stores integers in memory
Java stores all integers in memory as binary numbers.
type | Size | Range | ||
name | bytes | bits | minimum | maximum |
byte | 1 | 8 | -128 | +127 |
short | 2 | 16 | -32,768 | +32,767 |
int | 4 | 32 | -2,147,483,648 | +2,147,483,647 |
long | 8 | 64 | -9,223,372,036,854,775,808 | +9,223,372,036,854,775,807 |
How to write integer literals
Here is how to write decimal integer literals (constants).
int
literals are written in the usual decimal notation, like 34 or -222.long
literals are written by adding an L (or lowercase l altho this is almost impossible to distinguish from the digit 1), eg, 34L or -222L.- There is no way to write a literal
byte
orshort
, altho sometimes Java will automatically cast an int literal to the appropriate type.
Hexadecimal literals
You can write an int in hexadecimal by prefixing the hexadecimal number with the digit zero followed by the letter x, "0x" or "0X". The hexadecimal digits are 0-9 and the letters a-f in upper- or lowercase.
int i; i = 0x2A; // assigns decimal 42 to i.
Problems with integer arithmetic
- Overflow. Arithmetic operations may produce results which are too
large to be stored in an
int
(or other integer type). This situation is called arithmetic overflow. Unfortunately, no error is caused in this case; the result is simply an incorrect number. See Example - Factorial for an example of how bad things can get without obvious programming errors. There are no special bit patterns reserved for error or overflow values (unlike the floating-point representation). Use BigInteger to prevent arithmetic overflow. - Division by zero causes an execution exception (ArithmeticException),
which may be caught with a
try...catch
.