Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Cool take! I didn't know you could use if statements inside matches like that!
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
It works because a new object is returned. The variable 'x' does not change.
public String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
Thanks for your guys' explanation!
Correct me if I'm wrong. So, if my understanding is correct, the reason "final" is used is to ensure that String x does not change in the hands of outsiders, and the reason "x.replace" works is because String x is an object...
Warning: a mutable object declared as final will still be mutable. The only thing that
final
locks is the binding between the variable name and the reference of the object in memory, so final "protects the code" only when used on immutable data structures or basic data types:If you try to assign new object or value to the variable with final keyword, the code will not compile.
You should use it, if you specifically do not want any other programmist (or yourself) to change the value, because some of your code depends on it. In example:
final double PI = 3.1415;
// PI = 10.0; this line would not compile
In this kata:
x = "my custom solution"; // would not compile
Can someone explain the purpose of "final"?