Java for loop by value or by reference

前端 未结 6 815
忘了有多久
忘了有多久 2020-12-10 04:37

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

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
           


        
6条回答
  •  醉酒成梦
    2020-12-10 05:13

    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.

提交回复
热议问题