×

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java.

What is int in Java?

  • int is a primitive data type in Java. It can be used to store and manipulate integer values.
  • It stores the data values using a 32-bit signed two’s complement integer.
  • The value stored in data type int is changeable unless it is declared as final by the developer.

The following Java program demonstrates the use of int data type in Java.

Demoint.java

public class Demoint
{
    /* Driver Code */
     public static void main(String []args)
     {
        /* int variables declaration */
        int a = 5, b = 8, c;
        System.out.println("int value 1: " + a +"
int value 2: " + b);
        /* Addition operation */
        c = a + b;
        System.out.println("Addition of two int variables = " + c);
     }
}

Output:

int value 1: 5
int value 2: 8
Addition of two int variables = 13

In the above code, two int variables, a and b are declared and the addition is stored in the variable c.

What is Integer in Java?

  • Integer is a special class in Java also called wrapper class. It wraps the value stored as an int primitive data type into an object.
  • The need of the Integer class is because Java is object-oriented and many classes don’t accept primitive data type as argument. For example, HashMap in Java accepts key as an Integer object.
  • Integer class provides several methods for converting an int to String or vice versa.

The following Java program demonstrates the use of the Integer class in Java.

DemoInteger.java

public class DemoInteger
{
    /* Driver Code */
     public static void main(String []args)
     {
        /* Integer object creation */
        Integer a = new Integer("9");
        Integer b = new Integer("15"); 
        Integer c;
        System.out.println("Integer value 1: " + a +"
Integer value 2: " + b);
        /* Addition operation */
        c = a + b;
        System.out.println("Addition of two Integer objects = " + c);
     }
}

Output:

Integer value 1: 9
Integer value 2: 15
Addition of two Integer objects = 24

In the above code, two Integer class objects, a and b are declared and the addition is stored in the object c.

Other wrapper classes for primitive data types in Java

Primitive data type Wrapper Class
char Character
byte Byte
short Short
long Integer
float Float
double Double
boolean Boolean

The following Java program demonstrates a few methods of Integer class in Java.

          IntegerMethods.java

public class IntegerMethods {
    public static void main(String args[])
    {
        String bin = Integer.toBinaryString(123);
        String oct = Integer.toOctalString(123);
        String hex = Integer.toHexString(123);
        System.out.print(bin + "
" + oct + "
" + hex);
    }
}

Output:

int: 123
binary: 1111011
octal: 173
hex: 7b

The above code shows the use of Integer class methods like parseInt(), toBinaryString(), toOctalString(), toHexString().

int vs Integer

Sr. No. int Integer
1. The primitive data type int can store 32-bit signed two’s complement integer value. The Integer is a wrapper class in Java that allows to store int data type value as an Integer object.
2. It stores integer value into memory. It helps to convert int value as Integer object and vice versa.
3. Since an int allows only the binary value of an integer to be stored, it is less flexible. The Integer class is more flexible as it inherits properties from Object class
4. int data type takes 32-bits (4 bytes) to store a numeric value. The Integer class takes 128-bits (16 bytes) to store the integer object.
5. int data type doesn’t provide casting functionality for decimal or String type values. The Integer class allows the casting of decimal or String type values. Integer(String) or parseInt(String) methods can be used for casting a string to integer data.
6. Integer value stored using int data type cannot be directly converted into other base values. Integer class provides methods like toBinaryString(), toOctalString(), toHexString() for converting integer value to other base values.
7. Using int data type we cannot perform any operation because it doesn’t provide any built-in functions. Integer class provides multiple methods like reverse(), rotateLeft(), rotateRight() for performing different operations on integer value.

Why Java provides int data type and Integer class separately?

Primitive data type int provides limited functionalities for data values stored using it. The Integer class stores the data values as objects. Many operations can be performed on it using the built-in methods of the Integer class.

This article discusses the basics and difference between primitive data type int and Integer class in Java.


Related Topics

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

Java Program to Sort an Array of 0's, 1's, and 2’s | Dutch National Flag Problem in Java

The famed Dutch computer programmer Edsger Dijkstra's Dutch Nation Flag (DNF) challenge ranks among the most well-known programming challenges. The Dutch tricolor flag, comprising red, white, and blue, is the...

3 minutes read.

Java Image

For all other classes used for representing graphical images, Java's Image class serves as an abstract superclass. For images in Java, a specific form of object known as a BufferedImage...

4 minutes read.

Java StringWriter Class

The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer,...

3 minutes read.

Creation of Multi Thread in java

What are threads in Java? We can use threads to facilitate parallel processing. Threads are helpful when you wish to execute several pieces of code concurrently. A thread is a small process...

3 minutes read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.

Static class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM is mostly...

7 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.

LCM Program in Java

LCM Program in Java The LCM program in Java outputs the LCM of the given numbers. In Arithmetic, the lowest common multiple, least common multiple or smallest common multiple of the...

6 minutes read.

Count of Range Sum Problem in Java

In this article, we will discuss the basic approach or native approach used to count range sum problem in java. To solve this problem, we will check for numbers if they...

6 minutes read.

Undo and Redo Operations in Java

Undo and redo operation are the most widely used operation while dealing with file. In this section, we will discuss how to implement undo and redo operation in Java. Undo Redo...

2 minutes read.

Java Math expm1() Method

The expm1() method of Math class returns ex-1 where e represents Euler’s number. Syntax: public static double expm1(double x) Parameters: The parameter ‘x’ represents the exponent to raise e in the calculation of ex-1. Return...

2 minutes read.

Internal Working of ArrayList in Java

Java's version of a resizable array is called ArrayList. The dynamic growth of an array list ensures that there is always room for new elements. ArrayList's backing data structure is...

8 minutes read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.

Java Set to List

In this article, you will be acknowledged about how the process of conversion from Set or HashSet to LinkedList happens and what are the possible ways involved in conversion process. First...

4 minutes read.

Java Swing Time Picker

Prerequisites In this tutorial, we will learn the time picker in java swing. Before learn time picker, we should learn about java swing. Java Swing Introduction The Java Foundation Classes include Java Swing....

5 minutes read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

How to Create Different Packages for Different Classes in Java

Packages in Java In Java, Packages are an assortment of classes, sub-packages, and connection points. i.e. A package addresses a word reference that contains a connected gathering of styles and points...

7 minutes read.

Java String split() method

Java String split() method split current String against given regular expression and returns a char array. Syntax: public String split(String regex)                 public String split(String regex, int...

2 minutes read.

Java Math sin() Method

The sin() method of Java Math class returns the trigonometric sine of the specified angle. Syntax: public static double sin(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The sin() method...

1 minute read.