Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

integer question

What is the purpose of Integer.parseInt ? why don't we just type int age = 30; instead of int age = Integer.parseInt("30"); they have the same result right?

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Hi Khoa,

Could you add a little more context to your question. There could be multiple uses for Integer.parseInt(), but it wouldn't be used in the example you provided. If you are setting an integer, it would just be int age = 30;... that's it.

If you could provide so more examples or a reference to where in the courses you noticed this, we could shed some better light on the subject. :)

:dizzy:

1 Answer

You would only need to use Integer.parseInt(); if the argument being passed in was of a different type. Here is an example:

String number = console.readLine("Please type a number: ");
int total = Integer.parseInt(number);

Doing it this way would convert the String that was passed in by the user into an int.