×

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

Java md5 Hash Example

A 128-bit hash value is generated by the Message Digest Algorithm 5, which is a cryptographic algorithm. A stationary hash value is generated by the hash function from data of...

3 minutes read.

Java Read File Line by line

Java provides two options to read files line by line. Each option offers different benefits and has its own set of drawbacks. Following are the ways through which on accomplish...

5 minutes read.

Java Try Resource

In Java, a try argument that specifies one or more resources is known as a try-with-resources statement. When your program is finished utilizing an object, it must be closed, which...

4 minutes read.

Bin Packing Problem Java

Assigning some items with known weights to bins with consistent capacities is necessary for the bin packing problem. The goal is to use the smallest possible boxes while ensuring everything is put...

6 minutes read.

Calculator Program in Java

Calculator Program in Java A calculator performs the basic mathematical operations such as addition, subtraction, multiplication, etc. In this section, we will create a calculator program in Java using switch case...

5 minutes read.

Difference between String Tokenizer and split Method in Java

Introduction Today, let us understand the difference between String Tokenizer and Split Method. First, let us learn about the String Tokenizer and Split method individually and then know about their differences String...

7 minutes read.

How to take Multiple String Input in Java using Scanner class

Scanner class is a class which takes the multiple input data or the single input data through an objects and methods. The Scanner class will be available in the java.util...

3 minutes read.

Manachers Algorithm in Java

Here, we'll go over the four scenarios once more in an effort to approach them differently and use the same strategy. The values of (centerRightPosition - currentRightPosition) and LPS length at...

3 minutes read.

String Declaration in Java

A string is a group of characters. In Java, the string can be treated as both class and datatype. In Java programming, the String class have many advantages. Everything in...

3 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Java Thread Creation

Java provides the two ways to create a Thread: Implementing the Runnable interface.Extending the Thread class. Implementing Runnable interface The easiest way of creating a thread is to make a class that implements...

5 minutes read.

Trim Method in String Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

3 minutes read.

How to get the current date and time in Java

Introduction: In this article, we are going to discover many processes for Getting the existing-day Date and Time in Java. Most programs require timestamping events or showing date/times, among many...

3 minutes read.

Java 8 filters list

A stream with the components of this stream that match the given predicate is provided by the streaming filter (Predicate predicate). This process is step-by-step. Because these operations are always...

4 minutes read.

Star Pattern Programs in Java

Star Pattern Programs in Java The star pattern programs in Java is the part of pattern programs in Java, which we discussed earlier. Right Triangle Star Pattern Filename: StarPatternExample.java public class StarPatternExample {              public static void...

4 minutes read.

How to Convert Date to Timestamp in Java

How to Convert Date to Timestamp in Java You can convert Date to Timestamp by using the getTime() method of Date class. It returns the long millisecond from Epoch which can...

1 minute read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

3 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

ConcurrentSkipListSet in Java

ConcurrentSkipListSet is a class that is a part of collection framework in Java.It has been available since Java version 1.6. ConcurrentSkipListSet  is a scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap.The...

16 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.