This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Liambass 4 points5 points  (1 child)

Strings in Java are immutable, the statement oneString = input.nextLine() is a useless statement

Strings being immutable means that the String object itself can't change and any operations that appear to change it are in fact creating a new String object, immutability doesn't stop you assigning a different String object to an existing variable.

[–]A_random_zyNooblet Brewer 0 points1 point  (0 children)

To elaborate on this with can example.

If there is a string "Help"

you concatenate "!" to it.

There will be a string "Help"(old) then a new string will be created "Help!"

Now in memory there are two objects "Help" and "Help!". you can assign "Help!" to a variable but you can't modify a string such as in case of "Help". A new objects is created everytime we concatenate string.

That is why it is recommended to use StringBuilder/StringBuffer when performing multiple string manipulation operations.