Java int
Java int – In Java, int is a keyword used to define a variable of type integer.
int (integer) is one of the eight primitive datatypes in Java.
In this tutorial, we will learn about the type of values a Java int can store, how to declare a variable of type int, initialize an int variable, modify an int variable, get maximum and minimum integer values, print int to console, different operations like arithmetic, bitwise, etc., that could be performed on int, conversion of int to other primitive datatype and vice versa, etc., with well detailed description and examples.
Number of Bytes for an Integer
In Java, an integer value occupies 4 bytes in memory. 4 bytes equals 32 bits.
Range of an Int in Java
Range of an Integer is the range of values an integer variable can hold.
Considering facts that an integer has 32 bits allocated to it, and an integer can have positive and negative values, there must be one bit allocated for sign and 31 bits for magnitude.
Therefore, an integer can range from -2^31 to (2^31)-1. Which is from -2,147,483,648 to 2,147,483,647.
So, an integer variable can store a value that lies in the range specified above.
Declare an Int
To declare an integer variable, the syntax is
int variable_name;
Int the following code snippet, we are declaring an integer with variable name a
.
int a;
Initialize an Int
To initialize an integer variable a
with a specific value, say 8
, use assignment operator.
int a;
a = 8;
We can combine the declaration and initialization into single statement.
int a = 8;
Update an Int Variable
To update an integer variable a
with a valid integer value, say 65
, use assignment operator.
int a = 9;
a = 65;
Default Value of Static Int Variable
The default value of an integer variable with static access modifier is zero.
Java Program
public class Example{
static int a;
public static void main(String[] args){
System.out.println(a);
}
}
Output
0
Print Int to Console
To print an integer to Standard Console output, we can use System.out.println() method.
Java Program
public class Example{
public static void main(String[] args){
int a = 54296;
System.out.println(a);
}
}
Output
54296
Always initialize local Int variable
We should always initialize an int variable which is declared locally. Otherwise compiler would throw an error saying “The local variable a may not have been initialized”.
Java Program
public class Example{
public static void main(String[] args){
int a;
System.out.println(a);
}
}
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The local variable a may not have been initialized
at Example.main(Example.java:4)
Int Maximum Value
To get the maximum value of an integer, we can access MAX_VALUE constant in Integer class.
Java Program
public class Example{
public static void main(String[] args){
int a = Integer.MAX_VALUE;
System.out.println(a);
}
}
Output
2147483647
Int Minimum Value
We know that the minimum value of an integer is -2^31. We can get this value programmatically using Integer class.
To get the minimum value of an integer, we can access MIN_VALUE constant in Integer class.
Java Program
public class Example{
public static void main(String[] args){
int a = Integer.MIN_VALUE;
System.out.println(a);
}
}
Output
-2147483648
Java int – Arithmetic Operations
We can perform all arithmetic operations on integers.
In the following example program, we will take two integers: say a
and b
, and perform addition, subtraction, multiplication, division, modulus, increment, and decrement operations.
Java Program
public class Example{
public static void main(String[] args){
int a = 7;
int b = 2;
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));
System.out.println("++a = " + (++a));
System.out.println("--b = " + (--b));
}
}
Output
a + b = 9
a - b = 5
a * b = 14
a / b = 3
a % b = 1
++a = 8
--b = 1
Java int – Bitwise Operations
We can perform all bitwise operations on integers.
In the following example program, we will take two integers: say a
and b
, and perform bitwise and, bitwise or, bitwise xor, bitwise compliment, bitwise left shift, bitwise right shift, and bitwise zero fill right shift operations.
Java Program
public class Example{
public static void main(String[] args){
int a = 7;
int b = 2;
int c = 15;
System.out.println("a & b = " + (a & b));
System.out.println("a | b = " + (a | b));
System.out.println("a ^ b = " + (a ^ b));
System.out.println("~a = " + (~a));
System.out.println("b << 2 = " + (b << 2));
System.out.println("a >> 1 = " + (a >> 1));
System.out.println("c >>> 2 = " + (c >>> 2));
}
}
Output
a & b = 2
a | b = 7
a ^ b = 5
~a = -8
b << 2 = 8
a >> 1 = 3
c >>> 2 = 3
Function returns Int
A function can return an integer value.
In the following example, we will define a static method that takes two integers as arguments and return an integer.
Java Program
public class Example{
public static int add(int a, int b) {
int result = 0;
result = a + b;
return result;
}
public static void main(String[] args){
int a = 7;
int b = 2;
int result = add(a, b);
System.out.println("Result is: " + result);
}
}
Output
public class Example{
public static int add(int a, int b) {
int result = 0;
result = a + b;
return result;
}
public static void main(String[] args){
int a = 7;
int b = 2;
int result = add(a, b);
System.out.println("Result is: " + result);
}
}
Convert Java int to Other Primitive Datatypes
We can convert an int value to other datatypes using typecasting.
Note: There is an exception. We cannot convert int to boolean.
Java Program
public class Example{
public static void main(String[] args){
int a = 7;
// to other datatypes
byte b = (byte) a;
short s = (short) a;
long l = (long) a;
float f = (float) a;
double d = (double) a;
char c = (char) a;
}
}
Convert Other Primitive Datatypes to Java int
We can convert values of other datatypes to int using typecasting.
Note: There could be loss in information like precision when converting from float or double to int, or some bytes of data when the size of int is less than the other datatype like long.
Java Program
public class Example{
public static void main(String[] args){
int a;
byte b = 12;
a = (int) b;
short s = 14;
a = (int) s;
long l = 45215L;
a = (int) b;
float f = 3.142F;
a = (int) b;
double d = 635.214;
a = (int) b;
char c = 65;
a = (int) b;
}
}
Java Integer Class
Java has Integer
class in java.lang
package to wrap value of a primitive data type int
in an object.
java.lang.Integer provides constants and methods to facilitate commonly used operations on integers.
Conclusion
In this Java Tutorial, we learned about Java int, how to declare an int, initialize an int, modify an int, get maximum and minimum integer values, print int to console, different operations that could be performed on int, etc., with well detailed description and examples.