Skip to main content
  1. About
  2. For Teams
Asked
Viewed 60k times
65

How come the following prints boss and not bass?

String boss = "boss";
char[] array = boss.toCharArray();

for(char c : array)
{
 if (c== 'o')
     c = 'a'; 
}
System.out.println(new String(array)); //How come this does NOT print out bass?It prints boss.
1
  • @KacyRaye by the way, this is a similar behavior when using the enhanced for loop on object references classes (and the solution proposed here is the same).
    Luiggi Mendoza
    –  Luiggi Mendoza
    2013-04-05 22:23:38 +00:00
    Commented Apr 5, 2013 at 22:23

6 Answers 6

83

You're changing the iteration variable c. That doesn't change the contents of the array. The iteration variable is just a copy of the array element. If you want to modify the array, you need to do so explicitly:

for (int i = 0; i < array.length; i++) {
    if (array[i] == 'o') {
        array[i] = 'a';
    }
}

Your original code is equivalent (as per section 14.14.2 of the JLS) to:

for (int i = 0; i < array.length; i++) {
    char c = array[i];
    if (c == 'o') {
        c = 'a'; 
    }
}

Changing the value of a local variable will never change anything else - it just changes the local variable. The assignment:

char c = array[i];

copies the value in the array into a local variable. It doesn't associate the local variable with the array element perpetually.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh okay thanks. I didn't understand that c was merely a copy. Thanks that makes sense now.
@BaroudiSafwen: A copy of the array element is made at each iteration. What do you mean by "deform" here? I'm very confused as to what you're saying.
9

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++;
}

1 Comment

At that point wouldn't you be better off using a normal for loop?
4

c's value is a copy of the value in array. Access array directly to change the value in question.

Comments

3

You variable c gets changed, but not the array contents. To change the array, don't use c, manipulate the array directly.

for(int i = 0; i < array.length; i++)
{
 char c = array[i];
 if (c== 'o')
     array[i] = 'a';
}

Comments

3

You're assigning 'a' to the local variable c, but not to the array element. To make it print bass, you'd need

for (int i = 0; i < array.length; i++) {
    if (array[i] == 'o') {
        array[i] = 'a';
    }
}

Comments

1

Changes applied in 'for each' loop are made just inside her body (that's because values are copied, not referentions). To work on referentions use 'for' loop.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.