- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between an Integer and int in Java
A Java both int and Integer are used to store integer type data the major difference between both is type of int is primitive while Integer is of class type.This difference become significant when concept of OOPs comes in picture during development as int follows the principle of primitive data type while Integer behave as a wrapper class.
Following are the important differences between int and Integer.
Sr. No. | Key | int | Integer |
---|---|---|---|
1 | Type | A int is a data type that stores 32 bit signed two's compliment integer. | On other hand Integer is a wrapper class which wraps a primitive type int into an object. |
2 | Purpose | int helps in storing integer value into memory. | Integer helps in converting int into object and to convert an object into int as per requirement. |
3 | Flexibility | int provides less flexibility as compare to Integer as it only allows binary value of an integer in it. | Integer on other hand is more flexible in storing and manipulating an int data.Since Wrapper classes inherit Object class, they can be used in collections with Object reference or generics. |
4 | Memory allocation | As already mentioned int is a primitive data type and takes 32 bits(4 bytes) to store. | On other hand Integer is an object which takes 128 bits (16 bytes) to store its int value. |
5 | Casting | In java one canâTMt assign a string value (containing an integer only) to an int variable directly or even by casting. | In case of Integer we can assign string to an object of Integer type using the Integer(String) constructor or by even use parseInt(String) to convert a String literal to an int value. |
6 | Direct Conversion to Other base. | In case of int we can't convert its integer value to other base. | However in Integer we can directly convert its integer value to other bases such as Binary, Octal or Hexadecimal format using toBinaryString(), toOctalString() or toHexString() respectively. |
7 | Allowed operations | int do not allowed any of inbuilt functions to change its value or syntax. | However in Integer we can reverse number or rotate it left or right using reverse(), rotateLeft() and rotateRight() respectively. |
Example of int vs Integer
JavaTester.java
Example
public class JavaTester { public static void main(String args[]){ Integer a = new Integer("456"); // Casting not possible // int a = (int)"456"; // Casting not possible // int c="456"; // Casting possible using methods // from Integer Wrapper class int b = Integer.parseInt("456"); System.out.print(b); } }
Output
456
- Related Articles
- What are the differences between an Integer and an int in Java?
- What is the difference between int and integer in MySQL?
- Difference between const int*, const int * const, and int const * in C
- Difference Between int and long
- Difference between const int*, const int * const, and int const * in C/C++?
- Difference between “int main()” and “int main(void)” in C/C++?
- What is difference between int and const int& in C/C++?
- What is the difference between an int and a long in C++?
- What is the difference between const int*, const int * const, and int const *?
- What is the difference between integer and floating point literals in Java?
- C/C++ difference's between "int main()" and "int main(void)"
- Difference between MySQL BigInt(20) and Int(20)?
- Difference between an Iterator and ListIterator in Java
- Difference between signed and unsigned integer in Arduino
- What is the difference between size_t and int in C++?

Advertisements