Java Notes


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 (and char, 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 (except char) 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 use char 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 typeWrapper class
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.

byte 18 -128+127
int 432-2,147,483,648+2,147,483,647
long 864-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).

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