Convert Object to Int in Java

Joel Swapnil Singh Jan 30, 2023 Jan 07, 2022
  1. Convert Object to Int in Java by Using Integer Wrapper Class
  2. Convert Object to Int in Java by Using Number and intValue() Function
  3. Convert Object to Int in Java by Using Integer and intValue() Function
  4. Convert Object to Int in Java by Using Integer.parseInt() Function
  5. Conclusion
Convert Object to Int in Java

We can use the Object class to refer to any object whose type we don’t know in Java.

We can say the Object class is the parent class of all classes in Java by default. In Java, we can use the int keyword, a primitive data type, to declare variables and return integer type values with methods.

This article will discuss different ways to cast an object to int in Java.

We need to cast an object to an int to do various operations. One of them is while doing computations that can only be performed on integers.

Another factor could be when we need to return an integer value and much more.

Suppose we are given an object of the Object class. We need to cast the object into an int. We can do this by using the Integer wrapper class, using the Number and intValue() function, using the Integer and intValue() function, and using Integer.parseInt() function.

Let us discuss each of the approaches one by one.

Convert Object to Int in Java by Using Integer Wrapper Class

In Java, we can use the Integer wrapper class functionality to convert an object to int.

If we are given an object of the Object class, we will convert it into int by simply casting it into (Integer). We can then return the converted int.

Let’s look at the code below to understand how it works.

public class Main 
{
    public static int objectToInt(Object obj)
    {
        int x = (Integer)obj;
        return x;
    }
    public static void main(String args[]) 
    {
        Object obj=new Object();
        int value=123;
        obj=value;
        int res=objectToInt(obj);
        System.out.println("The converted int is " + res);
    }
}

Output:

The converted int is 123

Convert Object to Int in Java by Using Number and intValue() Function

We can also use the intValue() method to convert an object to int in Java.

If we are given an object of the Object class, we can convert it into int by first casting it (Number). Then we will invoke the intValue() method on the extracted result to fetch the integer.

We can then return the converted int. Let’s look at the code below to understand how it works.

public class Main 
{
    public static int objectToInt(Object obj)
    {
        int x = ((Number)obj).intValue();
        return x;
    }
    public static void main(String args[]) 
    {
        Object obj=new Object();
        int value=123;
        obj=value;
        int res=objectToInt(obj);
        System.out.println("The converted int is " + res);
    }
}

Output:

The converted int is 123

Convert Object to Int in Java by Using Integer and intValue() Function

We can modify the previous method and convert an object to int in Java.

If we are given an object of the Object class, we can convert it into int by first casting it (Integer). Then we will invoke the intValue() method on the extracted result to fetch the integer.

We can then return the converted int. Let’s look at the code below to understand how it works.

public class Main 
{
    public static int objectToInt(Object obj)
    {
        int x = ((Integer) obj).intValue();
        return x;
    }
    public static void main(String args[]) 
    {
        Object obj=new Object();
        int value=123;
        obj=value;
        int res=objectToInt(obj);
        System.out.println("The converted int is " + res);
    }
}

Output:

The converted int is 123

Convert Object to Int in Java by Using Integer.parseInt() Function

We can also use the Integer.parseInt() method to convert an object to int in Java.

If we are given an object of the Object class, we can convert it into int by first invoking the toString() method, which will convert the object into the string format. After that, we will invoke the parseInt() method, which will cast the extracted string into an integer.

We can then return the converted int. Let’s look at the code below to understand how it works.

public class Main 
{
    public static int objectToInt(Object obj)
    {
        int x = Integer.parseInt(obj.toString());
        return x;
    }
    public static void main(String args[]) 
    {
        Object obj=new Object();
        int value=123;
        obj=value;
        int res=objectToInt(obj);
        System.out.println("The converted int is " + res);
    }
}

Output:

The converted int is 123

Conclusion

This article looked at four different approaches to cast an Object to int in Java.

All of the techniques have their advantages, and you can apply them at your convenience. We suggest you go with the first approach as it is easy to remember and use.

Related Article - Java Object

Related Article - Java Int

Related Article - Java Casting