## Primitive Data-Types in Java II - Integers

In this tutorial, we will discuss how Java represents integers using the primitive data-types

**byte, short, int***and**long,*and the different operations that can be performed on integer data types.__What is a signed integer?__

Well, a signed integer is an integer that could have a sign, i.e. it could be either a positive real number, or a negative real number.

For example, in C++, there is the option of having an unsigned integer, which means that we can use it to represent only positive numbers.

Java offers only signed integer data-types. The only difference between the different types are their bit-length.

For example, in C++, there is the option of having an unsigned integer, which means that we can use it to represent only positive numbers.

Java offers only signed integer data-types. The only difference between the different types are their bit-length.

__Data-type sizes and values__

Remember when we talked about how a variable's bit length decides how many values can a variable store?

For example, if I have a 3-bit variable, I can represent two to the power of three values ( 2^bit-length). The following table

represents the different binary values a 3-bit variable can represent.

For example, if I have a 3-bit variable, I can represent two to the power of three values ( 2^bit-length). The following table

represents the different binary values a 3-bit variable can represent.

# | Binary Value | Decimal Value |
---|---|---|

1- | 000 | 0 |

2- | 001 | 1 |

3- | 010 | 2 |

4- | 010 | 3 |

5- | 100 | 4 |

6- | 101 | 5 |

7- | 110 | 6 |

8- | 111 | 7 |

From the table above, we can see that a 3-bit variable

Example-1 variable 3-bits,

Range: 0 to 2^3 -1 = 0 to 8-1 = 0 to 7.

Example-2 variable 8-bits,

Range: 0 to 2^8 -1 = 0 to 256-1 = 0 to 255.

So, the formula to calculate the range of numbers a variable can represent seems simple, so what is the range of values a

Java

positive, if it is 1, then the number is negative. The remaining bits are used to represent the number itself, hence the range of values

is divided into two sections, 128 negative numbers (from -1 to -128), and 128 positive numbers (from 0 to 256).

The same logic applies for

are summarized in the table below.

__can represent__**8 values**, from 0 to 7. It can be calculated as follows:Example-1 variable 3-bits,

Range: 0 to 2^3 -1 = 0 to 8-1 = 0 to 7.

Example-2 variable 8-bits,

Range: 0 to 2^8 -1 = 0 to 256-1 = 0 to 255.

So, the formula to calculate the range of numbers a variable can represent seems simple, so what is the range of values a

Java

__signed__byte can represent? -128 to 127. That is because java allocates the left most bit for the sign, if it is 0, then the number ispositive, if it is 1, then the number is negative. The remaining bits are used to represent the number itself, hence the range of values

is divided into two sections, 128 negative numbers (from -1 to -128), and 128 positive numbers (from 0 to 256).

The same logic applies for

*ints, shorts and longs.*The range of values for each of the data-types and related infoare summarized in the table below.

Data-type | Bit-length | Minimum-value | Maximum-value |
---|---|---|---|

byte | 8-bit | -128 | 127 |

short | 16-bit | -32,768 | 32,767 |

int | 32-bit | -2,147,483,648 | 2,147,483,647 |

long | 64-bit | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 |

__Integer Operations__

Now that we know the different types of integers, its time to explore what operations java offers on these integers.

We start by a simple example, where we perform the following calculation: a = b+c

We start by a simple example, where we perform the following calculation: a = b+c

public class SimpleArithmetic1 { public static void main(String[]args){ int a; int b = 3; int c = 7; a = b + c; System.out.println(a); } } //output = 10

The program defines 3 variables, namely a, b and c. The first variable "a", is not assigned any value at the beginning since b and c are not defined yet, hence

definition statement is terminated with a semicolon. We later define the variables "b" and "c", and assign them the values 8 and 3 respectively. Finally

we

The very last line "//output = 10" is called a

also multi-line comments that are not discussed here), and have no effect whatsoever on the execution of the program. The result of running the above code

is as follows:

definition statement is terminated with a semicolon. We later define the variables "b" and "c", and assign them the values 8 and 3 respectively. Finally

we

**the values of "b" and "c" to calculate "a". We print the output of the addition using the***add***System.out.println(...)**method.The very last line "//output = 10" is called a

__comment__. Comments start with a**//**, and occupy only one line (single line comments. There arealso multi-line comments that are not discussed here), and have no effect whatsoever on the execution of the program. The result of running the above code

is as follows:

run: 10 BUILD SUCCESSFUL (total time: 0 seconds)

Lets make things more complicated. The following program defines 4 variables, a0, a1, a2 and a3, and performs different operations on

"b" and "c" to calculate their values.

"b" and "c" to calculate their values.

public class SimpleArithmetic2 { public static void main(String[]args){ int a0, a1, a2, a3; int b = 3; int c = 7; a0 = b * c; System.out.println(a0); //output = 21 a1 = b - c; System.out.println(a1); //output = -4 a2 = c / b; System.out.println(a2); //output = 2 a3 = c % b; System.out.println(a3); //output = 1 } }

The above program defines variables "a0" to "a3", without assigning them values. "b" and "c" are then defined and different operations are applied

on them.

The variable "a0" is defined by

The variable "a1" is defined by

The variable "a2" is defined by

The variable "a3" is defined as c

example, 21%3 is 0, since dividing 21 by 3 yields no remainder. But 21%8 yields 5, since the result of dividing 21 by 8 is 2.625, or

the result of the division could be 2, and a remainder of 5.

When running the code above, we will receive the following result:

on them.

The variable "a0" is defined by

__multiplying__the values b and c.The variable "a1" is defined by

__subtracting__c from b.The variable "a2" is defined by

__dividing__c over b.The variable "a3" is defined as c

__modulus__b. The modulus operator calculates the remainder of a division between two numbers. Forexample, 21%3 is 0, since dividing 21 by 3 yields no remainder. But 21%8 yields 5, since the result of dividing 21 by 8 is 2.625, or

the result of the division could be 2, and a remainder of 5.

When running the code above, we will receive the following result:

run: 21 -4 2 1 BUILD SUCCESSFUL (total time: 0 seconds)

__The Increment/Decrement operators __

In computer science (and English I guess), to "increment" something, means to increase its value by one step. Similarly, to "decrement" something is

to decrease the value of something by one step. Java offers several ways to increment/decrement something, namely using the ++/- - operators.

Lets examine the following code:

to decrease the value of something by one step. Java offers several ways to increment/decrement something, namely using the ++/- - operators.

Lets examine the following code:

public class SimpleArithmetic3 { public static void main(String[]args){ int b1 = 3; b1++; System.out.println(b1); //output = 4 int b2 = 3; b2 = b2 + 1; System.out.println(b2); //output = 4 int c1 = 7; c1--; System.out.println(c1); // output = 6; int c2 = 7; c2 = c2 - 1; System.out.println(c2); // output = 6; } }

Here, we perform the increment operator

what we have done on the variable "b2". The same goes for the variables "c1" and "c2". c1 has its value decremented by 1 using

the - - operator, which is similar to subtracting 1 from c2. Note that assigning already defined variables new values,

values for these variables. The results of running the program are below

**++**on the integer "b1". It simply increases the value of b1 by one, which is equivalent towhat we have done on the variable "b2". The same goes for the variables "c1" and "c2". c1 has its value decremented by 1 using

the - - operator, which is similar to subtracting 1 from c2. Note that assigning already defined variables new values,

*overrides*the oldervalues for these variables. The results of running the program are below

run: 4 4 6 6 BUILD SUCCESSFUL (total time: 0 seconds)

One last note about the increment/decrement operators. There are two versions of these operators, the first is the

operator (++var) has the "++/--" appear before the variable name. Both versions of the operator increment/decrement the variable by 1. The only difference

is that the prefix version

first changes the value of the variable, and then returns the its new value.

*prefix*version and the second is the*postfix*. The postfix version take the same form as in the previous code (var++) where the "++/--" appear after the variable name. The prefix version of theoperator (++var) has the "++/--" appear before the variable name. Both versions of the operator increment/decrement the variable by 1. The only difference

is that the prefix version

*evaluates to*or returns the original value of the variable*incrementing/decrementing the variable, while the postfix version*__before__first changes the value of the variable, and then returns the its new value.

public class SimpleArithmetic4 { public static void main(String [] args){ int a = 4; int b = a++; // a = 5 , b = 4 System.out.println(a); System.out.println(b); int a1 = 7; int b1 = --a1; // a1 = 6 , b1 = 6 System.out.println(a1); System.out.println(b1); } }

After running the above code, we get the following results:

run: 5 4 6 6 BUILD SUCCESSFUL (total time: 0 seconds)

In the example code above, we first initialized an integer "a", with a value of 4. In the next statement, we initialized "b" with the value

of "a++". Since a++ uses the postfix notation, it

value of "a", while "a" has been incremented by one.

On the other hand, integer "a1" is initialized with a value of 7, and "b1" is assigned the value "--a1". Since this is the prefix notation, the decrement

operation occurs first, and

There are other types of arithmetic operators which will be discussed in a later tutorial.

of "a++". Since a++ uses the postfix notation, it

__first returns a value__and later increments, hence when we print their values, "b" has the oldvalue of "a", while "a" has been incremented by one.

On the other hand, integer "a1" is initialized with a value of 7, and "b1" is assigned the value "--a1". Since this is the prefix notation, the decrement

operation occurs first, and

__the new value of "a1"__is assigned to b1. Hence, they both have the same value.There are other types of arithmetic operators which will be discussed in a later tutorial.