Copyright

Converting Integer to Int in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Subtraction in Java: Method, Code & Examples

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Objects vs. Primitives
  • 1:53 Java Run-Time Magic
  • 3:07 Null Values
  • 3:52 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

There are times when we need to convert from an object to a primitive data type in programming. This lesson will define how to convert the Integer object to the int data type using Java.

Objects vs. Primitives

Since Java is an object-oriented language, you'll discover that even so-called primitive data types (such as integer, double, float, or byte) have corresponding Classes (with a capital C). These classes are called wrapper classes, and they are all children of the parent class Number. We'll be covering Integer to int, but these are the primitives with their corresponding wrappers:

  • int -> Integer
  • double -> Double
  • short -> Short
  • byte -> Byte
  • float -> Float
  • char -> Character
  • long -> Long
  • boolean -> Boolean

The idea is to contain an otherwise primitive data type in an Object. Think of the Integer as the parent, wrapping its arms around int. So why use it?

As you gain experience and skill in Java, you'll find certain structures don't let you use int, double, or float. Things such as an ArrayList or HashMap will only accept objects. We'll need to wrap the primitive in the parent (in this case Integer), but we have to be able to convert it back down to int. Thankfully, Java wrapper classes can be converted to their primitive types, which is called unboxing. In this lesson, we'll convert an Integer object to its primitive counterpart, int.

This code shows how to declare a new Integer object (myInteger) and give it a value of 5,000. Next, we create a new primitive type of int and convert the object value to a primitive value.


Integer myInteger = new Integer(5000);
int i = myInteger.intValue();


The function intValue( ) is used to convert the object to the primitive type.

Java Run-Time Magic

Java actually does this type of conversion when programs run. Let's say we have a function that takes an integer as a parameter. But when we call the function, we pass our Integer variable to the function. Java does the heavy lifting and converts it down to int, like this:


public static void main(String[] args) {
  Integer myInteger = new Integer(5000);
//call a method and pass the Integer
  coolMethod(myInteger);
}
public static void coolMethod(int n) {
 
//Java converts to int at runtime
  System.out.println(n);
}


We created the same Integer variable as before and passed it to our function, which accepted an int as a parameter. When the code runs, this output is displayed:


Java automatic unboxing output

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support