Quote (Ideophobe @ Sep 21 2016 09:44pm)
the foreach loop in c# just aliases the reference for scope of the loop; i didnt realize that java's enhanced for loop was making a new reference variable
i think eclipse lets you turn it into a warning or error. change your compiler settings to avoid bugs like that.
it's been a few years, but i think c# throws an error and won't let you do that at all. i dont have visual studio anymore, but try running it:
Code
int[] numbers = { 1, 2, 3, 4 };
foreach (int number in numbers) {
Console.WriteLine(number);
number = 5;
}
foreach (int number in numbers) {
// since you assigned 5 previously, do you expect to see all 5?
Console.WriteLine(number);
}
forgive me if my syntax is off. but you get the idea
Quote
In Java all object variables are references.
i'm always iffy describing it like that since java is always pass by value. for objects, it's the "value of reference". in c you can pass a reference and another function can change what the reference points to, but java you cannot. you just get the value that the reference points to.
referring to them always as reference can be a little confusing since it depends on what you mean by reference (do you mean the pointer or the result from the pointer?). usually in non-formal environments we know what you mean, but i'd be careful about using that term to teach someone how it works.
This post was edited by carteblanche on Sep 21 2016 08:14pm