- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I didn't think I was a Java expert, but then I [discovered Smirnoff | read the comments from people who thought they were]...
Good article, made me giggle.
Admin
What makes you think he knows that?
Admin
Java doesn't reference count... but I get what you are saying.
Admin
Wow.. these comments explain exactly what happened in the code WTF, seemingly the world is full of people who think they know how java works but really have no clue.
If you don't know you are right, why would you open your mouth to spout bullsh!t? Especially in a community of people who should, at least, partially, know what is going on?
How can you appreciate a good WTF if you don't recognize that you are perpetrating one by commenting on something you don't fundamentally understand?
headdesk I have no clue.
Admin
Quick! Is she turning clockwise or counterclockwise?
Admin
http://blog.aegisub.org/2008/12/if-programming-languages-were-religions.html
Let's hope this code is not doing anything to the object's children...
Admin
Java method params are pass-by-value. In the case of object parameters, the reference is passed by value. It's a source of much confusion, as many people would call it "pass-by-reference", which is in a sense true, as the object is not copied, but the reference to the object is, so it's still pass-by-value. So in the case of the WTF, the method is setting a copy of the reference to null, while the original reference passed in is unaffected. That's why it does nothing.
Admin
Admin
TRWTF here is definitely the number of commenters who think this helps the garbage collector, deallocates the object, or does anything other than wasting CPU cycles and memory. Seriously, this is really basic Java here.
Admin
Admin
TRWTF is that the Destruction class isn't final.
Admin
Admin
This sums up 25% of the comments today and fulfills the dancing girl request (kind of).
[image]Admin
s/Irish/dancing Irish
Admin
No, we didn't want to automatically reboot them all at the same time. That would cause an outage!
Admin
Admin
Too bad nobody thought of automatically rebooting them all at different times.
Admin
Setting a referenced object to null (aka NOTHING) inside a method (aka SUBroutine) has a very "VB-esque" flavor to it.
DIM Object Object = ME Object = NOTHING
I Object 2 VB
Admin
In VB parameters are sent ByVal by default. :P
And just to troll it up a little:
"Java is TRWTF here"
Admin
The comment + code truly makes the WTF.
For CSODs like this, a mandatory example of how it was called needs to be included to prevent speculation on how it was called and most likely provide further WTFs. Of course, the worst example should be used!
I could see using a marker method like this if I was coding something in Java that I knew would be ported to C++. (Probably a lot safer than trying to code in C++ and then port to Java) Since this sample was dealing with Java EJBs, I don't think we can give the author the benefit of the doubt.
Now for the Java 101 lecture...
Java object references are really pointers. If you understand how pointers work from C, C++, or assembly then you understand how Java object references work.
Java severely limits the pointer/reference though. There is no pointer arithmetic. All objects are allocated in the heap, never on the stack. Unlike C/C++, you can NEVER assign an arbitrary value to the reference nor access its contents. A new Object from the heap, another pointer of the same type (copy the value), or null may be assigned to an object pointer/reference. That is about it.
Since Java passes ALL parameters, both primitives and object references, by value (another name for pass by copy), the unknowable "pointer" value is copied on the stack creating a duplicate reference to the same object. As the caller of the function, you know that no one will ever mess with your "pointers", but the callee can mess with the Object that lives at the address of the pointer. This is the cause of many bugs in java. Some other code changes a value that you thought they couldn't! Alas, there is no const Object concept like C++. Many of the Java primitive types are immutable which is as close to a C++ const Object as I have seen.
Garbage collection(gc) is an implementation specific detail, don't count on knowing how it actually works. Most JVMs support different algorithms for gc to allow you to tune it for different scenarios. (GUI vs. server backend vs. a batch program that exits and can dump the whole memory footprint quickly)
In some scenarios, setting a null could be necessary to ensure garbage collection, but this is rare. Stack frames are a better solution in many cases.
Unless you have taken the time to disassemble Java byte code, don't try to guess what you think is happening. Of course, as someone else pointed out, a lot of the TRWTFs appear in the comments.
One guy that I used to work with started "optimizing" tons of code by changing the scope of variable declarations. Aside from introducing bugs, the only thing his optimization did was to change the order of 2 variables on the stack frame. He had to revert all of his code.
Admin
Yeah, well, you know, that's just, like, your opinion, man.
Admin
This is totally a WTF!
The programmer is obviously an idiot, doing something so pointless and wasteful.
There, I fixed it.
</sarcasm>Admin
This might be borderline insulting to some, but here's a fun example for those who aren't quite getting the idea of how Java references work and why this code is a really big wtf.
It also shows what Englebert talked about in regards to bug creation where member objects are messed with by methods that modify the object.
Admin
Admin
Yes.
Admin
I think this is the winner. When writing my initial comment, I wondered why the story ended with something like: "this was only one of the sources of memory leaks," when, clearly, no memory is either allocated nor freed. I can only assume that the author of the Destruction class also wrote a whole bunch of code that looks like this:
Admin
Hmmm, pass by reference, pass by value. In Java there are two kinds of type: primitive types and reference types. There are, therefore two kinds of value: primitive values and reference values. Calls to methods are by value, the arguments being either primitive values or reference values.
If you are unclear on this take a look at the JLS Edition 3, section 4.1 (http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html). If you can't understand what it says there then please fuck off.
Don't persist myths like Java is pass by reference. It's misunderstandings like that that lead to code of the calibre shown in the original post.
If you think Java is pass by reference then could you write a simple swap function?
Admin
Yes! Absolutely! I do indeed concur! Wholeheartedly!
Admin
Admin
BaBow...Try Again....
Admin
I fail to see how this is better than
What did those clone() calls do?
Admin
Oh, and by the way - this isn't passing by reference. This is passing a copy of the reference to two array objects, and then manipulating those objects.
Admin
I'm not a Java programmer, but unless the language completely redefines how object programming languages work:
Captcha Genitus: The first book of the Freudian Bible
Admin
The text in red is a WTF. Consider the following:
A method's argument is only one of possibly many references to that object. Setting it to null does nothing.
No, it doesn't allow the object referenced by lonelyTurd to get garbage collected.
Once all references to an object become unreachable, then the object becomes eligible for garbage collection.
Then it is moved to the "younger generation" region by a minor garbage collection. It is first placed in a section memory section called the "Eden", then to one "survivor" space, and then to another.
Then, at a later time when it is deemed sufficiently old on the second "survivor" space, it is moved to the "tenured generation" memory space. Then ,when a "major" garbage collection executes, then and only then this object is finally garbage collected.
Admin
A.) It's a static method, there will only ever be one instance (per class loader) of that method.
B.) Java is pseudo pass-by-reference. A copy of the reference is made and handed to the called method. However, the heap-allocated object is never copied.
Admin
Yeah.... seems to be written by one of those people who chose a managed environment so the 'don't have to worry about memory management' =D
Ranks right up beside the code i see from one of our C# developers on a regular basis. It looks something like:
if (null == myValueType) Throw new WtfTheyveChangedTheDotNetFrameworkException();
Admin
Both, among other things, depending on where exactly you are looking at her from. Also, at any given point in time progression she is also not moving, which leads to the impression that she is actually just standing still. If I had to stand still with my leg out like that for 2 seconds I'd faint...
:D
Admin
Everything is passed by value in Java, both primitives and Object pointers.
From the JSL : http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#20448
What that also implies however, and taking into consideration that there is no explicit pointer de-referencing in Java, every Java pointer is itself an immutable Object reference.
And what does that mean in codespeak? That objects are passed by reference.
The reference to the object is passed by value (the the value of the memory address it points to gets copied). That is the definition of passing by value.
The object being referenced to, however, does not get copied or cloned. It is still referenced by the copy of the memory address value pointing to it. That is the definition of passing by reference.
That is, in Java, Object pass-by-reference is implemented by enforcing a pass-by-value on the Object references themselves. A coder knows the difference.
Admin
if (myValueType == null)
for readability!
Admin
I'm actually thinking that this would be a great interview question...
Admin
I thought rule '1' is 'You do NOT talk about fight club' ?
Admin
this code is ridiculous, the class should have a private constructor to prevent inexpierienced programmers instantiating it.
Admin
[quote user="luis.espinal"][quote user="nobody"]agreed.
The object being referenced to, however, does not get copied or cloned. It is still referenced by the copy of the memory address value pointing to it. That is the definition of passing by reference. [/quote]
Kinda true but you forget one thing. The object is newer passed to the function. Think about it. The object is newer passed to the function. A reference to the object is passed to the function, so talking about whatever the object is passed by value or reference does not make sense because the object is newer passed to the function.
And
Admin
Admin
You keep using that word. I do not think it means what you think it means.
(What does the age of the object have to do with anything?)
Admin
Admin
variables and pointers. concepts which elude many.
Admin
Zeno's arrow paradox...
http://en.wikipedia.org/wiki/Zeno%27s_paradoxes
Admin
I feel compelled to point out that as soon as an object becomes eligible for collection, it will be collected by the next collection that affects the region it's in. If it becomes eligible in the Eden space (as many objects do), it won't make it to the survivor space, and similarly for the survivor to survivor/tenured spaces. Minor collections affect the Eden and survivor spaces, major collections affect Eden, survivor, and tenured spaces.
What you described would defeat the entire purpose of generational garbage collection, which is to take advantage of the relative frequencies of different object lifespans (the vast majority of which live either very briefly or forever).
Admin
Dont know java, but in .Net land we have object.Finalize() method which should force a garbage collection on object if i remember right.
suscipit - moo