Marco Dinatsoli
Marco Dinatsoli

Reputation: 10580

java does for loop COPY the object?

This is my code

public static String change(String word, char gone, char here) {
    char[] chars = word.toCharArray();
    for (char c : chars) {
        if (c == gone) {
            c = here;
        }
    }
    return new String(chars);
}

and this how i call it:

System.out.println(change("supper", 'p', 'o'));

the result is supper I was trying to find explanation to what is going on ...

the chars variable is a variable that is refers to an array object, and which contains the characters of the String object word. then the only explanation that I thought about is that in the for statement, java actually copies the chars array.Is that correct?

some users said that there is a warning in my code,

but here you go, no warnings

enter image description here

Upvotes: 1

Views: 2474

Answers (6)

No, java does not COPYing object in For loop, but you are not using object in this example. "char" is primitive variable so you always receive a copy of a primitive veriable.

Upvotes: 0

weston
weston

Reputation: 54791

c = here;

Updates the value of the character, not the array. If you used an editor it would tell you that the assigned value is not used.

IntelliJ

Editors like IntelliJ above are free, so you have no excuse.

Upvotes: 3

helpermethod
helpermethod

Reputation: 62185

It's not working because

for (char c : chars)

here you are creating a new variable c which contains a COPY of the character you are currently iterating over.

What you could do instead is using a c-style for loop, iterate over each array element and replace the element within the array, something like:

for (int i = 0; i < chars.length; i++) {
    if (char[i] == gone) {
        char[i] == here;
    }        
}

Or even better: Skip the loop and use Strings replace method.

Upvotes: 0

Pankaj Saboo
Pankaj Saboo

Reputation: 1185

In for loop u had checked whether gone to chars each character and changed value but that changed variable not used to change the again "chars". you had changed value of variable c every time but never used so u got "Supper" as it is. so if u want to change "Supper" then use following code

public static String change(String word, char gone, char here) {
char[] chars = word.toCharArray();
for (int i = 0; i < chars.length; i++) {
    if (chars[i] == gone) {
        chars[i] = here;
    }
}
return new String(chars);
}

Upvotes: 1

OsYYYY
OsYYYY

Reputation: 253

What you did there is just change the local variable c not the element in the char array

Upvotes: 0

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

The c variable is just a copied reference to the array element.

The reason for this is that the enhanced for loop uses an Iterator and in order to get the next element, it invokes the Iterator.next() method, which gives a copy to the original collection element.

In order to make it work, you have to directly set the new value into the array:

public static String change(String word, char gone, char here) {
    char[] chars = word.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        if (chars[i] == gone) {
            chars[i] = here;
        }
    }
    return new String(chars);
}

Upvotes: 1

Related Questions