References in Java and C#
1 2 | Whatever blah= new Whatever(); blah= utility.GetANewBlah(); |
Today is the second time I get a piece of code like this and I have to stand this huge misunderstanding about references. This time was done in C# but the first was in Java. Anyway, they behave in the same way for this matter.
Java and C# behave with variables that point to objects as placeholders for references. Using the example code as a guide, we execute the following steps in order:
- Define a variable called blah
- Create a new instance of Whatever
- Assign to the variable a reference to the new instance we’ve just created.
- Call a method in the object utility. The utility variable in turn holds a reference to another object.
- The method returns a reference to an object of type Whatever. We assign it to blah loosing the previous reference.
There is the mistake. We create a new object for the fun of typing code and then we forget about it. This behavior should be avoided in 99% of the cases because when the programmer does that he believes he is assigning the contents to the first generated object, when in fact he is forgetting about it and assigning a new one. This, if done in C++, will make your application eat your memory for breakfast.
This misunderstanding can lead to the following snippet I saw in a previous project.
1 2 3 4 5 6 7 | List<Url> blah= new List<Url>(); ... blah.Add(icanhascheezburger); .... blah= utility.GetANewBlah(); ... this.useBlah(blah); |
Of course icanhascheezburger was lost and the coder was wondering why. In this case the previous coder assigned to that project did the same, and the new coder, not being used to the Java language, followed his example copying the behavior. From this snippet we have to understand that in line 5 we loose the reference to the object and if we don’t have a copy of that reference whatever we did before is lost.
C# notes.
- No, the equal operator cannot be overloaded in C#.
- Doesn’t apply on properties. A property can replace the behaviour of the equal operator and, for example, on assignment copy the content. But we have to code that behavior.
Use with caution.
We can use this behavior in some restricted cases. For example when the new assignment is “exception prone” and we want to continue no matter the result.
1 2 3 4 5 6 | Whatever blah= new Whatever(); try { blah = utility.GetPreviousBlahs(); } catch () {} this.DoSomething(blah); |
In this case perhaps GetPreviousBlahs should return an empty list. Anyway, in case it fails, we can continue and operate with blah because it’s initialized.
Summary
Do not waste your time,others time, memory and CPU time. Object variables are references and the = operator removes the previous reference and assigns a new one.
Tags:c#,Java,mistake,references »
Trackback URL: http://fitri.manzanisimo.net/2008/11/05/references-in-java-and-c/trackback/
NazguL2 said,
2008-11-06 @ 8.43 am
A very logic and clever post … What kind of coder do that ? … Alas, I remember … the poor coder hired for a ridiculous amount of money.
graffic said,
2008-11-07 @ 9.09 am
@Nazgul2: some people do.
Usually people who is not used to the language methodology and/or the language itself. They type code, it compiles and it’s OK. Latter they will “test” their monster running it once.
Perhaps the coder is a “web 2.0 bubble” coder. Hey do you know what is a for loop? yeah! I do. Ok, welcome to our Web 2.0 butcher’s shop.