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) Creating the MVP Storing Guesses

Will Beasley
Will Beasley
6,020 Points

Strings aren't actually concatenated in Java

I know this isn't a question but I figured I'd throw in my 2 cents for some people. Strings are what is known as immutable, meaning their value isn't changed once they are created. A new object is created in memory even if it appears as if you are assigning a new value to the current object (Strings are objects). If you are consistently modifying a String, you should consider using what is known as a StringBuffer, in layman's terms it is to the String what an ArrayList is to an Array (meaning it can be re-sized dynamically). ANDDDD I'm out

Ryan Ruscett
Ryan Ruscett
23,309 Points

Using + to add two Strings together is a lot easier. Plus, a string being an object, means I can run all the inherited methods from object against my string. Like length etc. Which is one benefit of string buffer without using it.

Lastly they are concatenated. The definition of concatenated means linking two things together. That being said String Buffer is more typing, seemingly more complicated and not concatenation. Soooo yeah.

Will Beasley
Will Beasley
6,020 Points

Ryan Ruscett Well someone is uppity today. I will concede by definition they are concatenated but I meant in the sense they are two objects which create a new object and string concat doesn't append the current String. Also, StringBuffer.toString is perfectly suitable when you are done adding to the string. Not to mention the growth on string concat is exponential when compared to StringBuffer meaning bad things in repeated use. String.concat() is slow. Amazingly slow in fact. It's so bad that the guys over at FindBugs added a detector for String.concat (which is equivalent to += or str1 = str1 + str2) inside loops to their static code analysis tool. So sure it may be slightly more work but try justifying that as the reason you didn't use it during a code review with your project lead. SOOOO YEAH.

2 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hi Will!

You are right that concatenation is expensive. I talk a bit about that and a whole lot more about the oddities of strings in the workshop The Thing About Strings.

Will Beasley
Will Beasley
6,020 Points

Thanks for sending that over! Was brushing up on my Java (coming from the trenches of Python) and thought I'd throw it in there to help out. I can't imagine how hard it is to fit such a robust language into such a small amount of time. I hope TreeHouse is able to build out Java some more. It'd be really nice to see Swing, Spring, Hibernate, and maybe an intro to J2EE/JSP. Also, maybe it is already on here but have you guys considered a track just for OOP/OOD/SOLID principles taking people from the basics to advanced concepts? That's something that'd be a life saver for people learning Java!

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Check out (and vote) our upcoming Java content here: Trello board Treehouse Java Content Roadmap - https://trello.com/b/dBiGr15K/treehouse-java-content-roadmap.