Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In Java, int is a fundamental primitive data type used to store numeric values, while Integer is a wrapper class that enhances the functionality of int by encapsulating it as an object. This article delves into their key differences, uses, and examples, providing a comprehensive understanding of these two essential components of Java programming.
In Java, 'int' is one of the primitive data types that store numeric or integer values in a variable. At the same time, Integer is a wrapper class that wraps primitive data in an object. Head over to the below article for a better understanding.
int in Java is one of the eight primitive data types that assign integer values to a variable. Some of the essential properties of the 'int' data type are as follows:
Any variable declared with the 'int' data type will occupy 4 bytes of memory equal to 32 bits.
Given that integers have 32 bits assigned to them and can have both positive and negative values one bit must be set aside for sign and 31 bits for magnitude. Hence the range of 'int' data types is from -2^31 to (2^31)-1.
The syntax for declaring an integer variable is int variable_name;
Use the assignment operator to initialize an integer variable with a specific value. int a=8;
The default value of an integer variable with static access modifier is zero.
Any locally declared int variable should always be initialized. A compiler error stating will be thrown in the absence of this.
Use the MAX_VALUE constant to retrieve the 'int' data type's maximum value and the MIN_VALUE constant from the Integer class to acquire the data type's minimum value.
Example of Int in Java
import java.util.*;
public class test {
static int sum_digits(int n) // It returns an integer value which is the sum of all the digits of the given integer number.
{
int sum=0;
while(n>0)
{
int digit=n%10;
n/=10;
sum+=digit;
}
return sum;
}
public static void main(String[] args) {
int n=3462; //’n’ is a variable that stores the ‘int’ value
int sumOfDigits=sum_digits(n);
System.out.println("The sum of all the digits in the given number is: "+sumOfDigits);
}
}
You can also try this code with Online Java Compiler
The sum of all the digits in the given number is: 15
The 'int' data type is implemented in the above code, which computes the sum of all the digits of the integer number given to the 'sum_digits' function.
Wrapper classes in Java offer a way to wrap primitive values in an object so that primitives can be utilized for operations that are only available to objects. The Integer wrapper class is one such wrapper that encapsulates an int basic to simplify object operations.
The Java Integer class is present in java.lang package. A single field with an int value makes up an object of the integer class. The Java Integer class provides several methods, some of which are listed below:
Methods of Java Integer Class
Description
compare()
Integer.compare() compares two int values say x,y numerically and returns a single integer value. The method produces a positive integer value larger than zero if x>y. It produces an integer value smaller than zero if x>y. The method gives 0 if x == y.
equals()
It compares one object to another. It returns true if both objects have the same int value; otherwise, it returns false.
floatValue()
It changes the input integer value to its float equivalent and returns the result.
max()
It returns the maximum value amongst the two arguments passed inside it.
min()
It returns the minimum value amongst the two arguments passed inside it.
parseInt()
It converts the string parameter into a signed decimal Integer object.
reverse()
It provides the result of flipping the bits in the two's complement binary representation of the given integer value.
toBinaryString()
It converts an unsigned integer of binary base two into the string representation of the integer parameter.
toString()
It provides back the string representation of the input integer.
signum()
It returns 0 if the input value is equal to 0, -1 if the input value is less than 0, and 1 if the input value is larger than 0.
Example of Integer in Java
import java.util.*;
public class Main {
public static void main(String[] args) {
Integer num = 5; //The variable is declared using Integer wrapper class
String ans1 = num.toString();
//The number will be printed as a string
System.out.println("The result of toString() is: " + ans1);
int ans2= Integer.signum(num);
// Prints the signum value of the given number
System.out.println("The result of signum() is: " + ans2);
int n1=23;
int n2=67;
int maxi=Integer.max(n1,n2);
int mini=Integer.min(n1,n2);
//Prints the maximum of n1 and n2
System.out.println("The result of max() is: " + maxi);
//Prints the minimum of n1 and n2
System.out.println("The result of min() is: " + mini);
Integer num1 = 7;
Integer num2 = 7;
boolean result = num1.equals(num2);
// Prints the boolean value returned by equals() that checks whether the two numbers are equal.
System.out.println("The result of equals(): " + result);
}
}
You can also try this code with Online Java Compiler
The result of toString() is: 5
The result of signum() is: 1
The result of max() is: 67
The result of min() is: 23
The result of equals(): true
The above code shows the implementation of various functions of the Integer class in Java. You can use more than one Integer class in the same program.
Difference Between Int and Integer in Java
int
Integer
'int' is a data type used to store 32-bit integers.
An integer class wraps primitive data type 'int' into an object.
It is beneficial to store integer values in the memory.
It is beneficial to transform an object into an int and vice versa.
There is no direct or indirect way to assign a String value that contains only integers to an int variable.
Strings can be assigned to an object of Integer type.
You cannot directly convert the value of ‘int’ to Binary, Octal, or Hexadecimal format.
You can directly convert an integer value stored in the Integer class to Binary, Octal, or Hexadecimal format using toBinaryString(), toOctalString(), or toHexString()
Since it only accepts binary integer values, int offers less flexibility than Integer.
It is more flexible in terms of manipulating and storing integer data.
To compare to ‘int’ variables, you need a == sign.
To compare two Integer class objects, you need .equals().
Yes, Java can automatically convert an `int` to an `Integer` through a process called autoboxing, which simplifies working with primitive data types.
Is int and Integer same in Java?
No, in Java, `int` is a primitive data type, while `Integer` is a class that wraps `int`. They have different characteristics and use cases.
What are the primitive data types in Java?
Primitive data types are the most fundamental data types in Java. They are responsible for data manipulation. There are eight primitive data types present in Java. These are; boolean, char, byte, short, int, long, float, and double.
What is a wrapper class in Java?
Wrapper classes in Java are objects that encapsulate or represent the contents of primitive data types. Eight wrapper classes comprise Java.lang libraries in Java: Byte, Boolean, Integer, Character, Double, Float, Long, and Short.
Conclusion
This article highlights the distinctions between int and Integer in Java, emphasizing their individual features and use cases. By exploring examples and addressing common questions, readers can better grasp their applications.