Java int
Java int – In Java, the int keyword is used to define a variable that holds a whole number (an integer) without any fractional component.
The int data type is one of Java’s eight primitive data types. It is used to store integer values efficiently.
This tutorial covers various aspects of the Java int data type, including:
- The type of values an
intcan store. - How to declare, initialize, and update an
intvariable? - Maximum and Minimum values an Int data type can allow.
- Printing int value to the console output.
- Performing arithmetic and bitwise operations on integer values.
- Converting between int and other primitive data types.
Each section includes detailed descriptions and examples to help you master integer operations in Java.
1 Number of Bytes for an Integer
An int in Java occupies 4 bytes (32 bits) of memory.
2 Range of an Int in Java
Since an int is 32 bits, one bit is reserved for the sign (positive or negative) and 31 bits for the value. This gives a range from -231 to 231 – 1, which is from -2,147,483,648 to 2,147,483,647.
3 Declare Variable of Type Int
To declare an integer variable, use the following syntax:
int variable_name;
For example, to declare an integer variable named a:
int a;
4 Initialize Variable with Int Value
You can initialize an integer variable by assigning it a value. For example, to declare and initialize a with the value 8:
int a = 8;
5 Update an Int Variable
To update an existing integer variable, assign it a new value. For example, if a is initially 9 and you want to update it to 65:
int a = 9;
a = 65;
5 Default Value of a Static Int Variable
Static integer variables are automatically initialized to 0 if no explicit value is provided. For example:
public class Example {
static int a;
public static void main(String[] args) {
System.out.println(a);
}
}
Output:
0
6 Print Int to Console
To print an integer to the console, use the System.out.println() method. For example:
public class Example {
public static void main(String[] args) {
int a = 54296;
System.out.println(a);
}
}
Output:
54296
7 Always Initialize Local Int Variables
Local variables must be explicitly initialized before use, otherwise the compiler will throw an error. Consider this example:
public class Example {
public static void main(String[] args) {
int a;
System.out.println(a);
}
}
Output (Compilation Error):
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)
8 Int Maximum Value
You can retrieve the maximum value an int can store by using the Integer.MAX_VALUE constant. For example:
public class Example {
public static void main(String[] args) {
int a = Integer.MAX_VALUE;
System.out.println(a);
}
}
Output:
2147483647
9 Int Minimum Value
Similarly, use the Integer.MIN_VALUE constant to retrieve the minimum value an int can hold:
public class Example {
public static void main(String[] args) {
int a = Integer.MIN_VALUE;
System.out.println(a);
}
}
Output:
-2147483648
10 Java int – Arithmetic Operations
You can perform various arithmetic operations on integers. For example, consider the following operations on two integers:
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
11 Java int – Bitwise Operations
Bitwise operations allow you to manipulate the individual bits of an integer. The example below demonstrates common bitwise operations:
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
12 Function Returning Int
A method in Java can return an integer value. For example, the following static method adds two integers and returns the result:
public class Example {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int a = 7;
int b = 2;
int result = add(a, b);
System.out.println("Result is: " + result);
}
}
Output:
Result is: 9
13 Converting Int to Other Primitive Data Types
You can convert an int value to other primitive data types using typecasting. For example:
Note: Converting an int to a boolean is not allowed.
public class Example {
public static void main(String[] args) {
int a = 7;
byte b = (byte) a;
short s = (short) a;
long l = (long) a;
float f = (float) a;
double d = (double) a;
char c = (char) a;
}
}
14 Convert Other Primitive Data Types to Java int
You can also convert other primitive types to an int using typecasting. Keep in mind that converting from a larger data type (such as long or double) might lead to loss of precision or data.
Note: There can be information loss when converting from float or double to int, or when converting a larger type like long to int.
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) l;
float f = 3.142F;
a = (int) f;
double d = 635.214;
a = (int) d;
char c = 65;
a = (int) c;
}
}
Conclusion
In this Java Tutorial, we explored the int data type. We learned how to declare, initialize, and update an int variable; accessed its maximum and minimum values; printed it to the console; performed arithmetic and bitwise operations; converted between different primitive data types; and reviewed the use of the Integer class. These fundamental concepts are essential for effectively working with numbers in Java.
