- 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
This looks to me like the work of someone used to Ruby or some other language without useful type checking. People seem to get into the habit of repeatedly forcing variables to the type they want before using them, because they can't just assume the type is correct as a Java or C* programmer would.
I bet they then go home and feel really pleased about all the time they saved by not having to declare their variable, too :)
Admin
the real wtf is the use of "zero" which isnt a real quantity.
Admin
I agree with most of your explanation. Except here, I'd like to comment that Java pretends to pass the object to the function (because pointers "don't exist"). And because it in reality does pass a pointer, the object can be modified after the function call returns. This violates one of the properties of "pass by value" (the one that says that the thing you pass in can't be modified because it's a copy). So, whatever they say, the effect is like "pass by reference". Purely because they started lying about pointers in the first place.
Admin
Admin
That's right, but during conversation, the mind should be active in parsing the words said and actually construct a logical word patterns. I am a psychologist who happens to love programming.
Admin
Admin
Only objects are passed by reference. Primitives types are passed by value.
Admin
Nope. String s = "foo"; Now the value of s is your 4 byte reference to the actual ~30 byte object.
So when you call doSomething(s) the 4 byte reference is copied to the stack. That is pass by value.
If you are using a primitive:
long l = 0L; Now the value of l is your 8 byte long. When you call doSomething(l) the 8 bytes are copied to the stack. Also pass by value.
Also it is possible to mess with immutable objects using reflection: http://javaspecialists.co.za/archive/newsletter.do?issue=014&locale=en_US
Admin
Admin
GOD NO! NOTHING nothing nothing is "passed by reference".
Everything is "passed by value". Primitives are "passed by value". Reference Types are "passed by value". Objects are NEVER EVER EVER passsed.
Admin
You don't need JNI, Reflection is all you need.
Admin
That's what most languages do, too, the interface is completely uniform and you don't know nor care whether you're manipulating primitive or wrapped values.
Nor should you.
The issue isn't that they used primitive types instead of making everything an object (primitive types are sensible in optimization contexts), the issue is that they didn't make everything look like objects. Python and Ruby only manipulate objects, but C# (for instance) uses primitive types, it just boxes and unboxes them so that you can't tell them apart from regular objects (unless you read the IL code) No, that's called "pass reference by value", "pass by reference" is usually understood with C++' semantics, which aren't even remotely close to Java's (and most reference-using langage) semantics. Do you, really? I don't, I just want stuff to get done, and the primitive/boxed dichotomy is just a retarded annoyance, I don't give a damn about what happens under the hood, but I care about having to actively think about whether I want a primitive or a wrapped datatype. i shouldn't have to. And how it's done in pretty much every language ever since. Except Java. Congratulation. You, sir, are an idiot. And you've never worked with a dynamically, strongly typed language (Smalltalk, Python, Ruby, Scheme, Erlang, you name it)Admin
Your logic is flawed. You assume that the statement: String s = "blah"; defines s as 'object of type String'. In fact it doesn't, it defines s as 'reference to object of type String'. In this sense Java has pointers. Here's what Java Language Specification has to say about it:
"4.1 The Kinds of Types and Values There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3)."
See? There are no 'object variables' in C++ sense. Now, let's see what happens when you call a method - JLS again:
"When the method or constructor is invoked (§15.12), the values of the actual argument expressions initialize newly created parameter variables, each of the declared Type, before execution of the body of the method or constructor."
Which sound hell a lot like 'passing by value' to me... And if you don't agree with JLS - well, it's no more Java then.
Admin
And to prove the point that reflection will work, here is the voodoo.
Admin
Well...
String a = new String(String.valueOf("a"));
Admin
Often it's not more "expert", only more "cheap". And since business is all about the bottom line, they go for it.
Admin
better make it: someMethod(Integer.valueOf(0).intValue());
;)
Admin
Not only can you do it with reflection (assuming you have security rights) you can screw up your JVM state with it. If you do this: [code] Integer i = Integer.valueOf(0); java.lang.reflect.Field f = Integer.class.getDeclaredField("value"); f.setAccessible(true); f.setInt(i, 42); [code] Now all subsequent calls to get a 0 Integer value using [code]Integer.valueOf(0)[code] will give you one with the value of 42.
Admin
This boxing/unboxing thing exists because languages like Java and C# try to straddle the space between C/C++ and script languages like Ruby/Python. The "primitive" types exist mainly for efficiency; a JIT can handle most primitives directly in CPU registers (fast), whereas a boxed type needs to be handled via a pointer (reference), which is slower. Similarly, boxed types are allocated on the heap and incur GC overhead, while primitives can move directly from the stack to registers etc.
Boxing is necessary to allow the primitives to be "object-like" when they need to act polymorphically (e.g. in collections). Additionally, boxing primitives provides a nice parking-place for all the type-related methods to live.
Languages like Ruby are much cleaner in that all types are objects, but they pay a significant performance penalty.
Admin
Totally!
Admin
That's a great way to drive someone mad !
Admin
OK, I had to put my 2 cents in, Java has no pass by reference, it's all pass by value
Admin
This programmer is definitely wearing MAGIC PANTS.
I think it needs at least 3 more layers of boxing and unboxing to be properly obfuscated.
Admin
Java will NOT pass the 16K Object. The confusion has been explained several times in here, but I will try to explain it again.
The terms "Pass-by-value" and "Pass-by-reference" are talking about the actual variable, not the data. When you pass the variable's value, it's called "Pass-by-value", when you pass the variables reference, it's called "Pass-by-reference". The confusion is that a value of a variable can be a reference, but you are still pass the value of the variable, not the reference of the variable. In java, you cannot access the reference of a variable (outside of JNI that is).
Admin
In VBScript you can just go
" someMethod 0 "
Admin
I was talking to one of the developers I was interning with and he told me about a friend of his who lost his work visa after he got laid off and had to go back to India. He heard from the guy a few months latter. He was basically in a coding sweatshop, lined up in a row with 20 other "programmers".
They were being paid by how many lines of code they wrote, so if you could take a 5 line function and turn it into a 50 line function you did.
This is probably one reaon why so much code outsourced offshore is so bad.
Admin
From my perspective strict type checking is not necessary in all cases - but is useful where it makes sense.
Python is a good example of the perfect compromise. As long as the result of the operation makes sense, python allows it.
So you could add a float and an integer, and get a perfectly acceptable result because python has built-in rules for arithmetic type conversions:
Of course, you could coerce the result to an integer if you wanted:
Where it doesn't make sense, python raises an exception:
In a string context, python assumes you want to concatentate the strings with the '+' operator...but it can't do that, unless you explicity force the integer to be a string:
Furthermore you can use the 'isinstance' function - which returns a boolean value to test the item's type, typically used to allow functions to do different things depending upon the type passed:
How is this any different than overloading type conversion operators in C++ to do the same thing? Not much, other than semantics.
I would argue you have to be a smarter programmer to use python - you have to understand how the virtual machine is going to coerce your types - and act selectively. Now, I have seen python code like this before also:
While this works, it is clearly overkill. a simple z=x+y would have sufficed. This is clearly a case of cargo-cult programming because the developer does not have a clear understanding of types. We can clearly see that x and y were assigned as strings - so no need to coerce them. On the other hand, if we expect to get our data from an unknown source (such as a human being - notorious for providing unanticipated input), we would instead use our isinstance function to verify the input:
This is where test-based programming comes into play - to assert a particular state - and raise and handle the exception when it is not - we could do this more elaborately to interact with automated testing. I guess the bottom line is - know your tools well, or look like a fool (probably should be the motto of WTF?!).
Admin
Just Another Virtualisation Architecture
Admin
This entire conversation alone has me completely convinced that Java sucks.
Admin
What non-Java folks need to know is that primitives and Objects are fundamentally different in Java. If you need a parameter or result that is an object, it must derive from the Object Class, which primitives don't. Normally what drives this is the need to fit a primitive into a method signature that requires an object, hence you wrap it. if you need to concatenate the value into a string, say for a log statement, you can unwrap it. And yes, it is freaking tedious. Our friend in this case is just like to exercise his int's and Integer's.
Actually this is unnecessary due to the "Autoboxing" feature starting in Tiger, jdk 1.5.0
Admin
As C++ has both "pass by reference" and "pass by pointer" (besides "pass by value"), it's easy to demonstrate the difference using C++:
Compile and run this simple program, and you'll get:
I leave it as an exercise to you to figure out why. :)
Admin
Only half of the story. Indeed, "*" and "&" are both overloaded, and hence can be confusing. Actually, the declaration "int *x" is parsed as {{int } x} while "int &x" is parsed as {{int &} x}. The spacing is irrelevant here. Although many people like to write "&" and "" together with the identifier "x", that doen't change how the C++ parser handles the declaration.
When used as type modifiers as in the above declarations, "&" means reference and "" means pointer. However, these 2 symbols are also unary operators on variables, in which case "&" means "the address of" and "" means "deference".
Let's not dig into .* and ->* operators... :)
Admin
It must give 'false' here, because you explicitly create two different instances using the new operator. All other means of producing the Integer objects (autoboxing, Integer.valueOf() etc.) may happily reproduce the same instance.
Admin
There is a fixed pool size of Byte, Short, Integer, Long and Character. See X.valueOf(primative)
I have seen a long list of the following x.getClass() == new Integer(0).getClass() x.getClass() == new Double(0.0).getClass() ...
even x.getClass() == new String("").getClass()
Admin
[quote user="Prove that you're not a robot"]That's a great way to drive someone mad ![/quote]
Admin
[quote user="Prove that you're not a robot"]That's a great way to drive someone mad ![/quote]
Admin
Our application also contains such entertaining code like
Admin
I know that java doesn't have pointers, i was merely explaining WTF byval and byref did. In one you're getting a pointer to the memory location, and in the other you're not.
Admin
Wouldn't it be simpler to call SomeMethod(0)
Admin
cheap NFL JerseyLewis says the Magic knew they could beat the Cavaliers in the Eastern Conference Finals when the the two teams cheap kobe jerseymet for Game 2. cheap Paul Smith Shoes It took a miracle shot from LeBron James to beat Orlando cheap nfl jerseys cheap nfl jerseys Bucks are looking very closely at every point guard available in the draft would make you cheap g star think Sessions replica jerseys walk, but it's far replica NFL jerseys, NHL jerseys too early to know. In truth, the Bucks nfl jerseysthemselves haven't f teams are starting to think about what cheap diesel Villanueva would look like in their nba jerseysuniforms. One of them might be the Cleveland Cavaliers. cheap NFL Jersey mlb jerseys cheap nfl jerseys James' no-show after Game 6, we're finally getting down to business. Dwight Howard nfl jerseys and Kobe Bryant will lace them up o decide cheap uggsthe best team in cheap north face jacketthe NBA. The Magic swept the season cheap jerseysseries from the Lakers, one of that feat (Charlotte was the other) cheap nhl jersey Raptors are they'd cheap tommy bahama also like to another selection there. With teams like L.A., Chicago, and Minnesota cheap nhl jersey selling that area, they're replica NFL jerseys, NHL jerseys likely to find someone they can work with.cheap nba jersey Portland, Houston failed to shifts lebron james jersey back to Houston for Game 6 on Thursday as the Rockets scurry to make Jordan jerseys adjustments cheap nhl jerseys of the Blazers cheap ugg boots Grizzlies, and it's been mentioned that that pick could be had as well. Most teams looking to buy in are looking to buy into cheap ugg bootsthe late first/early second round. Memphis has picks potentially available in both those areas.cheap mlb jerseys Timberwolves use all three of their first round picks this year (6-18-28), and while it's all but certain they'll use cheap true religion jeanstheir #6 pick and probably the #18, that #28 could absolutely be had. For teams looking to buy into that part of the draft (see below), the Wolves cheap nhl jerseys