No3x
No3x

Reputation: 479

Java for loop by value or by reference

I figured out a a problem in my Code. First the code:

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String[] blablubb = { "a", "b", "c" };

        for(String s : blablubb) {
            s = "over";
        }
        printArray(blablubb);


        for (int i = 0; i < blablubb.length; i++) {
            blablubb[i] = "over";
        }
        printArray(blablubb);

    }

    public static void printArray(String[] arr) {
        for( String s : arr ) {
            System.out.println(s);
        }
    }

}

The output is:

a
b
c
over
over
over

I assumed the first loop would also overwrite the String in the array. So the output would be over in any case. It seems it creates a copy of the value instead creating a reference. I never perceived this. Am I doing it wrong? Is there an option to create a reference instead?

//Edit: Seems like everybody knows about that except me. I'm from C background and doesn't pay enough attention to the term reference which is very different to C. Fortunately it took me just 10 minutes to figure this out (this time).

Upvotes: 14

Views: 16912

Answers (6)

Adam Stelmaszczyk
Adam Stelmaszczyk

Reputation: 19857

This:

for (String s : blablubb) {
     s = "over";
}

Is equal to this:

for (int i = 0; i < blablubb.length; i++) {
     String s = blablubb[i];
     s = "over";
}

This creates a temporary String with a copy of the value from array and you change only the copy. That's why blablubb[] content stays untouched.

If you want to change values in the array, just use your second option:

for (int i = 0; i < blablubb.length; i++) {         
    blablubb[i] = "over";
}

And, by the way, you can print an array with just one line:

System.out.println(Arrays.toString(blablubb));

Upvotes: 30

user529543
user529543

Reputation:

When you want to do a low level optimization, know how, you have to look inside Java code and inside byte-code either(compiled code)

for(String s : blablubb) {
            s = "over";
}

is equals with:

for (int i = 0; i < blablubb.length; i++) {
     String s = blablubb[i];            
     s = "over";
}

and that's why the output as how it is.

Upvotes: -1

Loki
Loki

Reputation: 4130

for(String s : StringArray)
{
}

is like

for(int i = 0; i < StringArray.length; i++)
{
    String s = StringArray[i];
}

Upvotes: 0

cyon
cyon

Reputation: 9548

Your for(String s : blablubb) loop is equivalent to the following code:

for(int i = 0; i < blablubb.length; i++ ) {
    String s = blablubb[i];
    s = "over";
}

Hopefully, from this you can see that all you are doing is reassigning a different value to s without changing blablubb[i]. This explains the output you see.

Upvotes: 5

rocketboy
rocketboy

Reputation: 9741

 s = "over"; 

just changes the reference of s and not the String in the array.

blablubb[i] = "over";

changes the value stored at ith location in the array

Upvotes: 0

Maxime Piraux
Maxime Piraux

Reputation: 340

The for-each loop don't modify the objects contained in the Collection of objects it's iterating over. It's passing the value not the reference.

Upvotes: 2

Related Questions