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

Android Build an Interactive Story App (Retired) Finishing the User Interface Formatting Strings

Jiannan Li
Jiannan Li
5,476 Points

Is char null (ie ' ') and String null (ie " ") equal?

Please help me with this I have problem setting up default player's name in this game.

2 Answers

They are not equivalent as the data types are different. Remember that Java's default comparator is what is in RAM. Data of different types, even if they contain the same practical value are still stored differently in memory. If you need to compare Strings and chars use the method String.charAt(n) where n is the index of the character you want to compare. This method will return a char type from the specified index which you can use normal comparative operators on (I.E. String a = "hello"; a.charAt(0) == 'h'; -- returns "true"). However, I don't think it will work with empty variables. I'm not even sure that Java will allow a char to be assigned an empty value. If this is absolutely necessary, I suggest building both as String data types. Also, in Java a null value is different from an empty string. The empty string variable points to an empty section of memory whereas the null variable points to nothing.

Jiannan Li
Jiannan Li
5,476 Points

I see... That makes more sense to me now thank you very much