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).