Convert Integer to Int in Java

We will discuss how to Convert Integer to Int in Java in this tutorial .In java, We used to wrapper class Integer to create integer objects and int is a primitive type in java that holds a primitive integer value.There we convert the Integer object to primitive int type. We have three methods to convert java integer to int .

  1. Convert Integer to int Using assignment operator
  2. Convert Integer to int Using intValue() method
  3. Java Integer to int Using parseInt() method
  4. Safe Conversion of Integer to Int in Java

Now we will discuss these method with example .

Method 1 : Convert Integer to int Using assignment operator

This is a simplest solution for convert integer object to int primitive type in java. In this method we did not use any method or explicit casting to convert integer to int , but we use simple assignment operator as below java example . Implicit conversion will happen there.

public class IntegerToIntJava {
	public static void main(String[] args) {
		Integer integerValue = new Integer(200);
        System.out.println("Integer value is: "+integerValue);
        //convert Integer to int using implicit conversion
        int intValue = integerValue;
        System.out.println("int value is: "+intValue);
	}
}

Output:

Integer value is: 200
int value is: 200

Method 2 : Convert Integer to int Using intValue() method

To convert integer object to int primitive type in java we can use intValue() method of Integer Class . This method does not take any argument but Returns the value of this Integer as an int. There we will convert Integer to int using intValue() method as below example .

public class IntegerToIntJava1 {
		public static void main(String args[]) {
	        Integer integerValue = new Integer(200);
	        System.out.println("Integer value is: "+integerValue);
	        //convert Integer to int using intValue() 
	        int intValue = integerValue.intValue();
	        System.out.println("int value is: "+intValue);
	    }
}

Output :

Integer value is: 200
int value is: 200

Method 3 : Java Integer to int Using parseInt() method

parseInt() method of Integer class also used to convert integer object to int primitive type in java. parseInt() method is a overloading in Integer Class . There we use which parseInt() method take string as argument and returns int value .

Syntax :

public static int parseInt(String s);

This method a return int representation of argument .

public class IntegerToIntJava2 {
    public static void main(String[] args) {
		    Integer integerValue = new Integer(500);
	        System.out.println("Integer value is: "+integerValue);
	        //convert Integer to int using parseInt() 
	        int intValue = Integer.parseInt(integerValue.toString());
	        System.out.println("int value is: "+intValue);
	        
	}
}

Output:

Integer value is: 500
int value is: 500

Safe Conversion of Integer to Int in Java

As we knew that Integer creates object , InĀ  java object can be null if sometimes Integer object have null value and we use these any method as discuss above , we will get null pointer exception.

public class IntegerToIntJava3 {
      public static void main(String[] args) {
          Integer integerValue = null;
	      //convert Integer to int using intValue() 
	      int intValue = integerValue.intValue();
	      System.out.println("int value is: "+intValue);
	}
}

Output:

Exception in thread "main" java.lang.NullPointerException
	at com.demo.datatype.IntegerToIntJava3.main(IntegerToIntJava3.java:7)

We have to use null check when use such methods .

public class IntegerToIntJava4 {
  public static void main(String[] args) {
	     Integer integerValue = null;
	      //convert Integer to int using intValue() 
	      int intValue = integerValue ==  null ? 0:integerValue.intValue();
	      System.out.println("int value is: "+intValue);
	      
		   Integer integerValue2 = new Integer(500);
		      //convert Integer to int using intValue() 
		   int intValue2 = integerValue2 ==  null ? 0:integerValue2.intValue();
		   System.out.println("int value is: "+intValue2);
}
}

Output:

int value is: 0
int value is: 500

In this tutorial we learned convert integer to int in java . We have used some different methods to java integer to int change . You can learned more java program for practice.

Leave a Reply

Your email address will not be published. Required fields are marked *

80 − 75 =