How to convert integer to int in java

How to convert Java Integer to Int Array instance

Let’s see an example of converting an int value into an int[] .

First, create a String representation of the int type using Integer.toString() method:

Then, create a new int[] where you will put the integers later.

Use the String.length() method to determine the size of the array:

Finally, create a for loop to iterate through the String and assign each character inside the String into the array.

Take a look at the code below:

You need to use the combination of Character.getNumericalValue() and String.charAt() methods to get the right integer value.

This is because the String.charAt() method returns the number as a String , and you will get the Unicode value of the numbers without using the Character.getNumericalValue() method.

The code below shows the result of calling only the charAt() method:

That’s why you need to wrap the value returned by charAt() method with getNumericalValue() method.

Below is the full code for converting an int to an int[] using Java:

    Feel free to modify the code above as you require 😉

Take your skills to the next level ⚡️

I’m sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I’ll send new stuff straight into your inbox!

About

Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.

Type the keyword below and hit enter

Tags

Click to see all tutorials tagged with:

Источник

How to Convert Integer to Int in Java

The terms “int” and “Integer” are used in Java to store the integer type data. The Integer is a wrapper class for creating integer objects defined by java.lang package, while int is a primitive data type that holds a primitive integer value. It saves 32-bit signed two’s complement integers, whereas the Integer object stores its int value in 128 bits. However, there exist chances that you may need the conversion of an Integer object to a primitive data type int.

This write-up will illustrate the methods for converting the Integer to int in Java.

How to Convert Integer to int in Java?

In Java, you can utilize the following methods for converting Integer to int:

We will now check out the working of each of the mentioned methods one by one!

Method 1: Convert Integer to int in Java Using Assignment Operator

Converting Integer to int using assignment operator “=” is implicit type conversion. It is the simple and easiest way to convert Integer to int.

The syntax of converting Integer to int is given below:

Here, “x” is the object of the “Integer” class which will be converted to int “y” using the “=” assignment operator.

First of all, we will create an Integer object “x” that contains the integer value “11”:

Next, we check the type of variable “x” using the “instanceof” operator:

We will print the value of “x” by using print statement:

Now, we simply convert the object of Integer “x” to a primitive type int “y” by using Assignment Operator:

Lastly, print the value of the “y” variable:

The output shows the successful conversion of Integer to int:

Note: For Java version 1.5 or above, you can perform Integer to int conversion using implicit conversion. However, for Java version 1.4 or lower same operation have to be performed using explicit conversion.

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

For converting Integer to int in Java explicitly, you can utilize the “intValue()” method of the Java “Integer” class. It takes no arguments and gives a primitive value as an output.

The intValue() method has the following syntax:

Here, the “intValue()” method is called with an Integer type object “x”. The specified method will convert Integer x to int.

Create an Integer object “x” with value “14”:

Print the value of “x” by using print statement:

Now, convert Integer to primitive type int by calling the method “intValue()”:

Lastly, print the value of “y”:

As you can see, the intValue() method returned the required int value:

There can be a situation where the Integer object you want to convert has a “null” value. What will happen in such a situation? The below-given example will let you know about that.

In this example, the Integer object “x” is assigned a “null” value:

Print the value of “x” Integer by using print statement:

Here, we use the ternary operator to check whether the object is null or not; if null, then assign any default value that will be returned as an int type by calling the “intValue()” method:

Print the value of “y” int type variable:

Here, the output shows that the Integer object has a null value which is converted to “0”:

Let’s see another method for explicit conversion of Integer to int.

Method 3: Convert Integer to int in Java Using parseInt() Method

There is another method of the Integer class called “parseInt()” that is also used to convert Integer to int. In this method, a string is accepted as an argument and gives an int value as an output.

The following describes the syntax for the “parseInt()” method:

Here, the “x” Integer object is first turned to a string, which is then parsed as a “int” with the “parseInt()” method..

In this example, we have an integer value “5” that is stored in Integer object “x”:

We will print the value of “x” by using the “System.out.println()” method:

Now, we will use “parseInt()” method and passing the Integer object “x” with “toString()” method as an argument:

Finally print the value of “y”:

We have compiled all the methods for converting Integer to int in Java.

Conclusion

There are two ways for converting an integer to an integer: implicit conversion and explicit conversion. Java version 1.5 and above follow the implicit conversion, while Java version 1.4 and lower support explicit conversion. You can use the Assignment operator to convert Integer to int implicitly. While parseInt() and intValue() methods are utilized for explicit Integer to int conversion. This write-up illustrated the methods for converting Integer to int in Java.

About the author

Farah Batool

I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.

Источник

Convert Integer to Int in Java

Convert Integer to Int in Java

  1. Convert Integer to Int in Java
  2. Integer to Int Conversion in Java 1.4 or Lower
  3. Safe Conversion of Integer to Int in Java
  4. Integer to Int Conversion Using parseInt() Method in Java

This tutorial introduces how to convert Integer to int with some examples in Java.

In Java, Integer is a wrapper class that is used to create integer objects, whereas int is a primitive type that holds a primitive integer value. There can be a scenario when we need to convert the Integer object to primitive int type and vice versa. To convert the Integer to int, we can use the intValue() or the parseInt() method. However, after Java 1.5 version, the Java compiler does this implicitly, and we don’t need any explicit conversion. Before Java 1.5, no implicit conversion was available.

Let’s understand with some examples.

Convert Integer to Int in Java

In this example, we have an Integer object and convert it to a primitive int type. See, we did not use any method or explicit casting, but a simple assignment and the conversion takes place. This is the simplest solution to get a primitive int value from an Integer object. See the example below.

public class SimpleTesting  public static void main(String[] args)  Integer a = new Integer(10);  System.out.println("Integer value = "+a);  int b = a; // implicit conversion  System.out.println("int value = "+b);  > > 
Integer value = 10 int value = 10 

Integer to Int Conversion in Java 1.4 or Lower

If you use the Java 1.4 version or a lower one, then use the intValue() method of the Integer class to convert the Integer object to int type because no implicit conversion is supported. This method does not get any argument, but it will return a primitive value. See the example below.

public class SimpleTesting  public static void main(String[] args)  Integer a = new Integer(10);  System.out.println("Integer value = "+a);  int b = a.intValue();  System.out.println("int value = "+b);  > > 
Integer value = 10 int value = 10 

Safe Conversion of Integer to Int in Java

Since an Integer is an object, then it can be null also. So, to avoid any runtime error or exception, use this code example. Here, we used ternary operators to check if the object is null or not and assign any default int value. See the example below.

public class SimpleTesting  public static void main(String[] args)  Integer a = null;  System.out.println("Integer value = "+a);  int b = (a!=null) ? a.intValue() : 0;  System.out.println("int value = "+b);  > > 
Integer value = null int value = 0 

Integer to Int Conversion Using parseInt() Method in Java

The parseInt() is a method of Integer that can convert an integer value to int. It gets a string argument and returns an int value. It is useful if we have string integer object only. See the example below.

public class SimpleTesting  public static void main(String[] args)  Integer a = new Integer("10");  System.out.println("Integer value = "+a);  int b = Integer.parseInt(a.toString());  System.out.println("int value = "+b);  > > 
Integer value = 10 int value = 10 

Related Article — Java Integer

Related Article — Java Int

Copyright © 2023. All right reserved

Источник

Convert Int to Integer in Java

Convert Int to Integer in Java

  1. Convert Int to Integer Using Autoboxing in Java
  2. Convert Int to Integer Using Integer Constructor in Java
  3. Convert Int to Integer Using the Integer.valueOf() Method in Java

This tutorial introduces how to convert primitive int to an integer object in Java.

Java uses either primitive int type or Integer wrapper class to hold integer values. If we want to convert primitive int to an Integer object, Java provides several methods such as valueOf() and Integer() constructors.

In this article, we will learn to use these methods. So, let’s get started.

Convert Int to Integer Using Autoboxing in Java

Autoboxing is a technique in which a primitive type gets converted into an object implicitly. Its inversion is called unboxing. Java supports autoboxing implicitly, so we don’t need to write any extra code.

In the example below, we used autoboxing to convert int to an Integer object and see both variables hold the same value. See the code example below.

public class SimpleTesting  public static void main(String[] args)  int a = 10;  System.out.println("a = "+a);  Integer i = a; // autoboxing  System.out.println("i = "+i);  > > 

Convert Int to Integer Using Integer Constructor in Java

The Java Integer class is a wrapper class used to create objects of primitive int type. We can use its constructor to convert an int to an Integer object. In the below example, we used the Integer class constructor that takes an int value as an argument and returns it as an Integer object.

public class SimpleTesting  public static void main(String[] args)  int a = 10;  System.out.println("a = "+a);  Integer i = new Integer(a);  System.out.println("i = "+i);  > > 

Convert Int to Integer Using the Integer.valueOf() Method in Java

This is another that we can use to convert an int to an Integer in Java. Here, we used valueOf() method of the Integer class. It is a static method that takes an int primitive argument and returns an Integer object. So, we can use this method Here. See the code example below.

public class SimpleTesting  public static void main(String[] args)  int a = 10;  System.out.println("a = "+a);  Integer i = Integer.valueOf(a);  System.out.println("i = "+i);  > > 

We have seen how to convert an int to an Integer in Java, but here we are giving one more trick to you to verify whether the conversion is successful. It means you can verify the result by using the getClass() method of the Object class. This method returns a fully qualified name of the class(including package name).

We used the getClass() method to check whether the resulted value is an object of the Integer class or not and see it works fine.

We used the getSimpleName() method to get only the name of the class from the fully qualified name. See the example below.

public class SimpleTesting  public static void main(String[] args)  int a = 10;  System.out.println("a = "+a);  Integer i = Integer.valueOf(a);  System.out.println("i = "+i);  System.out.println(i.getClass().getSimpleName());  > > 

If we don’t use the getSimpleName() method, the output will be below.

a = 10 i = 10 class java.lang.Integer 

Related Article — Java Integer

Источник

Читайте также:  Python serial port extension
Оцените статью