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 Meet Objects Welcome Back

Jonathan Sanders
Jonathan Sanders
752 Points

Chain methoding - does it change variables permanently?

If I apply the ".toLowerCase" method to a string variable, then use chain methoding to apply something else to that variable (like ".contains"), will that initial ".toLowerCase" have changed the string variable permanently (or until another method is applied to it) or just within the context of that line of code for the sake of applying ".contains" to it?

1 Answer

Ruud Claassen
Ruud Claassen
9,359 Points

The String variable itself will not be changed by applying the method to it. In this case you can use the modified value of the variable within the context of that line.

String name = "Jonathan";
name.toLowerCase().contains("j"); // true
name.contains("j"); // false
Jonathan Sanders
Jonathan Sanders
752 Points

Brilliant - thank you! My first question on here answered with lightening speed!