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
Jae Kim
3,044 PointsOne of the questions in the code challenge for building the MVP confused me!
At the MVP challenge we are asked to create a new constant that stores the max number of characters in a Twitter tweet. It is supposed to be unchangeable and accessible right off the class.
E.g. public static final int MAX_CHAR = 140;
The second challenge asks us to add a method that returns the remaining characters available based on the length of what is in mText.
E.g. public int remainingChar() { return MAX_CHAR - mText.length(); } }
I completed the challenge, but had a question on the logic side of things. I may be overthinking this but is there not a Data Type mismatch between an int and a String? For instance, whatever I type is stored into mText as a String and using the .length() method returns the length of the sequence of characters. How does a branching statement, 'return', compile the code successfully?
Is the .length() method returning an integer?
1 Answer
Kartik Gupta
6,454 PointsHi Jae ! I would like to tell you that you are thinking absolutely right , remember how we use ' . ' to access object properties and behaviours i.e. member variables and methods . You are actually doing just that , by using .length() , You are actually calling the length method of the object of class String which is then returning an integer. You can see the official doc file : https://docs.oracle.com/javase/8/docs/api/java/lang/String.html here they have clearly stated that .length method returns an int. Hope that clears everything.!!!Happy Learning
Jae Kim
3,044 PointsJae Kim
3,044 PointsThat makes more sense now. Thanks Kartik!
I guess I was thinking too much!