This time we'll look at the integer data type, which holds whole numbers.
About the Integer Data Type
Integer, as the name suggests, is a data type that can only hold whole numbers (negative, zero, and positive)
Integer Sub Types
In Java and programming languages in general, the integer data type is split into 4 sub types based on memory length. Among them:
| Data Type | Size | Value | Object Form |
|---|---|---|---|
| byte | 8 bit (1 byte) | -128 to 127 | Byte |
| short | 16 bit (2 byte) | -32,768 to 32,767 | Short |
| int | 32 bit (4 byte) | -2,147,483,648 to 2,147,483,647 | Integer |
| long | 64 bit (8 byte) | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | Long |
Because everything in Java is an Object, each primitive also has an object counterpart you can see in the table above. Telling them apart is easy, just look at the first letter. A capital first letter means object.
For usage, adjust it to your needs. In most cases the int type is used most often, but if you need a very large number you can use long. Below that (short and byte) are fairly rare.
Integer Literals
Literals accepted by the integer data type are whole numbers. These numbers can be written in several formats, among them:
- Decimal (base 10): ordinary decimal numbers, no special format. Example: 1, 95, -29, 1000
- Octal (base 8): the format starts with the digit
0. Example: 01, 0137, -035, 01750 - Hexadecimal (base 16): the format starts with the
0xcharacter. Example: 0x1, 0x5f, -0x1d, 0x3e8 - Binary (base 2): the format starts with the
0bcharacter. Example: 0b1, 0b1011111, -0b11101, 0b1111101000
To get a better feel for it, let's build an example program.
public class TipeDataInteger{
public static void main(String[] args) {
int decimal = 250;
int octal = 0372;
int hexadecimal = 0xfa;
int binary = 0b11111010;
System.out.println(decimal);
System.out.println(octal);
System.out.println(hexadecimal);
System.out.println(binary);
}
}
Now, if we run it, the output looks like this:
250
250
250
250
We can see that when we print the contents of the variables they're all the same, 250. That's because whatever the number format, in memory it's stored in 1 identical form, so the output comes out identical too.
If you want to output a number in a specific format, you can use printf with a matching template. For example:
public class TipeDataInteger{
public static void main(String[] args) {
int number = 250;
System.out.printf("desimal : %d\n", number);
System.out.printf("octal : %o\n", number);
System.out.printf("hexadesimal: %x\n", number);
}
}
Try running it and see the result.
Using Separator Characters
Integer literals in Java also let you add a separator character, an underscore (_). That separator helps when you declare large values. For example like this:
int satuJuta = 1_000_000; // same as 1000000
int satuMiliar = 1_000_000_000; // same as 1000000000
int seribu = 1_00_0; // valid but not recommended for readability
Integer Data Type Operations
Arithmetic Operations
As a numeric data type, integer has operations roughly like arithmetic in general. That includes multiplication, division, addition, and subtraction.
The special one is probably division. In Java, when a variable or literal of integer type is divided by another integer, the result is rounded automatically.
That's because division can produce a fractional number, which in Java is of type float. To avoid that type change, the result has to be rounded so it stays an integer.
For example:
int a = 10;
int b = 3;
int c = a / b; // the value of c is 3
int d = 6 / 5; // the value of d is 1
One more thing, Java also knows the modulus operation, marked with the % operator. It gives you the remainder of dividing 2 numbers.
For example:
int a = 10;
int b = 3;
int c = a % b; // the value of c is 1
int d = 6 % 5; // the value of d is 1
Just like in math, working through numbers follows an order. That order follows the rules of math:
- Innermost parentheses
- Multiply / divide
- Add / subtract
- Operations from the left
Arithmetic Assignment
Next comes arithmetic assignment, assignment operations fused with arithmetic.
Say you have code like this:
int a = 10;
a = a + 20; // value of a: 10 + 20 = 30
a = a * 5; // value of a: 30 * 5 = 150
a = a / 2; // value of a: 150 / 2 = 75
Those operations can be shortened like this:
int a = 10;
a += 20; // value of a: 10 + 20 = 30
a *= 5; // value of a: 30 * 5 = 150
a /= 2; // value of a: 150 / 2 = 75
See? Shorter, right. If we print the value of a the result is the same, whether we use the long version (top) or the arithmetic assignment (bottom).
Unary Operator
Last comes the unary operator. Earlier each operation involved at least 2 literals or variables; with a unary operator you only need 1 variable.
There are 4 kinds of unary operators for the Integer type, among them:
- Pre-increment (
++a) - Post-increment (
a++) - Pre-decrement (
--a) - Post-decrement (
a--)
Their job is simple, adding 1 to a variable or taking 1 away. If we spell it out, the 4 lines of code below do the same thing.
a = a + 1; // full version
a += 1; // simpler
++a; // even simpler
a++; // same as above
Not convinced? Try running this code:
public class TipeDataInteger{
public static void main(String[] args) {
int a = 10;
int b = 10;
a++; // or ++a
b--; // or --b
System.out.println(a);
System.out.println(b);
}
}
If you run it, the numbers 11 and 9 come out.
Now a question might come up. If they do the same thing, why are there 2 versions, post and pre?
Standing alone they do behave the same, adding to or taking from a number. But beyond that, the operation also returns a value.
Pre and post here show when that value is returned. In a pre operation the current variable value is operated on first, then returned. In a post operation the current value is returned first, then operated on.
To avoid confusion, let's look at a code example.
public class TipeDataInteger{
public static void main(String[] args) {
int a = 1;
System.out.println(++a); // output: 2
System.out.println(a); // output: 2
int b = 1;
System.out.println(b++); // output: 1
System.out.println(b); // output: 2
}
}
If we spell it out, the code above is the same as this:
public class TipeDataInteger{
public static void main(String[] args) {
int a = 1;
a += 1;
System.out.println(a); // output: 2
System.out.println(a); // output: 2
int b = 1;
System.out.println(b); // output: 1
b += 1;
System.out.println(b); // output: 2
}
}
Hope you've got the concept.
Next Up
This discussion of integers runs long. But numeric data types are actually pretty simple in general, and all the operations above work for the float data type too.
Some of the operations above even work on the String data type. Because of that, later pieces may point back to this article a lot.
Next time we're still on numeric data types, namely float in the Java language.
Hope it helps, happy coding 👋