How come the following prints boss and not bass?
String boss = \"boss\";
char[] array = boss.toCharArray();
for(char c : array)
{
if (c== \'o\')
c =
This is because c = 'a'
is assigning a
to the local variable c
which is not referencing the actual value present at that index of the array
itself. It is just containing a copy of the value present at the specified index of array
. So the change is actually made in the local variable not in the actual location where array[i]
is referencing..
If you want to change value you should use the following indeed:
int i = 0;
for(char c : array)
{
if (c== 'o')
array[i] = 'a';
i++;
}