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 Java Objects (Retired) Meet Objects Welcome Back

Integer question!

Why do I have to put Integer.parsInt for age but not for ageOfBob?

3 Answers

Enrique Munguía
Enrique Munguía
14,311 Points

The purpose of Integer.parseInt() is convert a number represented as a String to number. This is a subtle but important difference, in Java "23" is radically different than 23, one is a String a the other is an integer. In the example age is a String and ageOfBob is an integer.

In what circumstances is "23" used over 23?

kabir k
PLUS
kabir k
Courses Plus Student 18,036 Points

An example of where you'll encounter "23" (a string) in your program is if a user enter 23 as their age which will be a String and you will have to convert it to an integer using

int age = Integer.parseInt("23"); 

where "23" is the user input or whatever variable that's storing it.

An example of integer, 23 in your program is if say, you set the integer variable, ageOfBob to 23 yourself like so

int ageOfBob = 23;

u can use it where ever u want (just for getting int from strings) it depends on Program.