- 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
So, it would be counterproductive to use valueOf(String) it pre-Java 5, because it would create a new Integer AND a new String every time. So your comment that Integer.valueOf() does it since the early days of Java is plain wrong.
Admin
Oops, just saw that I didn't get your argument that String.valueOf(1) would be interned: no it wouldn't and you could easily check this first, before posting any more wrong claims here!
Admin
Nowadays Java is usually faster than C++ as the bytecode can be optimised on the fly (by the HotSpot features for instance), whereas C++ code is optimised once at compile time. Google for "java faster than c++" to find a number of articles with benchmarks.
In the bad old days, a number of design decisions were made that with hindsight we now know were wrong. Hence why we have things like the ArrayList class that's preferred over Vector, and StringBuilder that's preferred over StringBuffer - in both cases the older classes were thread safe, but experience showed that most often they were only used in a single thread and that the synchronisation overhead was costly. Synchronisation can be added to the newer classes only where necessary - by using the java.util.concurrent classes in many cases.
Another thing that hindered Java performance was the lack of appreciation among many Java programmers of how String concatenation was costly in terms of memory use and heap fragmentation. The "Effective Java" book went a long way towards making programmers aware of that problem, and encouraged the use of a buffer instead.
Finally, when you compare the time it takes to write and test C++ code versus the time it takes to do the same in Java for the same application, Java wins every time.
Admin
Umm, on my machine, using the Sun JRE 1.6.0_07, the following code:
Gives the following output:
As any sane person would have expected.
Admin
Admin
Judging by the sources:
http://www.docjar.com/html/api/java/lang/Integer.java.html http://www.docjar.com/html/api/java/lang/String.java.html
You're almost totally right. The one exception is if the value passed to String.valueOf(int) is equal to Integer.MIN_VALUE, in which case you get a reference to a static instance. But, ignoring that pedantic point, I stand corrected.
Admin
Well we'll agree to differ then, as those "synthetic" benchmarks concur with my experience of replacing two large systems written in C and C++ with Java based ones. Performance, testability and maintainability were far better for the Java implementations.
(I do love the way that people use words like "synthetic" when describing benchmarks that don't agree with their preconceived world view).
Admin
Mr. Top Cod3r, sir I'm sure you are already aware that java.lang.Integer does this already.
Admin
This is not necessarily a WTF. If you've ever had memory pressure, and you end up with a lot of 1s and 0s in your data, this is a way of avoiding polluting your space with millions of objects when only two are required.
Admin
If I understand correctly using Integer.valueOf(int) accomplishes the same thing. Doing that will cache values, which can be used again. Unlike new Integer(int), which creates a brand new object every time.
From the javadoc for Integer.valueOf(int): "Returns a Integer instance representing the specified int value. If a new Integer instance is not required, this method should generally be used in preference to the constructor Integer(int), as this method is likely to yield significantly better space and time performance by caching frequently requested values. "
Addendum (2009-01-14 11:00): OOps. looks like other people brought up the same thing.
Admin
Ah, but is it compatible with ObjectWrapper?
Admin
The real WTF is that you're using a language(*) that allocates 32 bit PoD objects using a garbage-collecting memory allocator, and then you're whining about the craptastic performance of programs written in that language.
(*)yes, I know the differences between Integer and int, and you probably do too. Does the guy in the next cube over from you know? What about the person next to him? What about the person who supplied the libraries you're using, or the framework you're coding to?
Admin
Since Java 1.5 has been released 4 years ago, it is time to replace to CommentUtils.getIntegerValueOf(int) by java.lang.Integer.valueOf(int i).
The real WTF is to link to the Java SE 1.3.1 Javadoc which is eight years old... If you want to use a language that didn't change during the past decade use C, C++ or MUMPS.
Admin
Admin
... and C++03
Admin
after reading all the comments on the first page, I came to the conclusion that TRWTF here is Java itself.
Admin
If only Integer weren't a final class, I think one could simplify the IntegerType class to just one line of code:
public class IntegerType extends Integer;
Admin
Not a WTF.
This code is a nice way to avoid instantiating 0 and 1 as brand new objects. If you are using say a database that represents booleans with 0 and 1 and you have a large dataset, this could be a significant speed increase.
Also, this code would allow you to use == with autoboxing 9at least with values that are supposed to be boolean) without borking b/c you won't have more than one Object representing 1 and 0.
Admin
This is the correct way to do it in JDK5, but Integer.valueOf(int) did not exist in previous versions. Sure, this code could (and should) be updated with JDK5 additions, but that doesn't make it a WTF.
Admin
DomainConstants.FIRST
Honestly, wake up people. Tsh.
Admin
In C++ you can make really fancy wrappers for stuff that have absolutely no footprint overhead whatsoever. That's the tip of the iceberg.
Admin
1)"Some of the other differences include the compilation policy used, heap defaults, and inlining policy." Am I the only one who noticed the "inlining policy" thing? Considering "method call" was one of the most compelling arguments for his case (by orders of magnitude!), the fact that the methods being "called" are being called INLINE should mean something.
If you're allowed to turn on the java inliner, surely you can spare the time to turn on the C++ one as well (he used -O2, not -O3, for compiling the C++ apps).
Picked a bad compiler for code optimization (g++ is better for porting)
Many of the C++ tests are not optimized. That is, they use C++ features like the iostream stuff (cout, and friends) which is extremely slow. The C versions are available and very fast. C++ is pretty much just an extension of C. You don't need to use C++ features if they slow you down. Another one is the hash stuff. In the C++ hash benchmark there are some goofy mistakes made by using the brackets [] operator where it forces several unnecessary lookups. You can also substitute a better STL hashing function that is faster (like MLton's insanely fast hasher).
I just went through and tested the hash2 benchmark and found that I was correct. The C++ version slaughters the Java version (even in "server" mode). This is completely different than what this dude's page shows.
Here is the "correct" code for hash2.cpp:
Yeah, this guy is a great source for java vs gcc tests. "Some of the C++ tests would not compile. I've never been very good at decoding GCC's error messages, so if I couldn't fix a test with a trivial modification, I didn't include it in my benchmarks."
Admin
Can't java.lang.Integer just use its static methods to avoid allocating Integer objects? Almost every transformation method has a static equivalent.
The Integer class probably doesn't pre-calculate all the cases. So, it is enough to run the static code on a simple "int" every time. No class object ever has to exist.
Admin
public class IntegerType { Integer i; ... }
I'm guessing this IntegerType class was created to effectively have a mutable-value (changeable) version of Integer. The Integer class methods were replicated simply as a convenience.
They didn't code a getter or setter for the wrapped i value, but it is clearly not private as is the underlying value of the Integer class, so it can be directly accessed from outside methods.
Use of a mutable integer object (or String, or ...) in an application could very well be warranted. I've used a similar construct myself in the past.
I see no WTF here if this was the intention.
Admin
The Java compiler already handles storage optimizations. The time and effort to instantiate this IntegerType class is more work for the JRE than any memory savings. An "int" is 32-bits!
Since Java is an OOP language, most "int" variables will belong to an object. So, there are NOT many shared, constant 0 or 1 values.
As the fool who proposed this posted, a shared java.lang.String constant is useful. It takes some time and memory to make a String, and String is immutable once created. None of that applies to scalars like "int".
Admin
TRWFT (is there a limit as to how many of these there can be for any given article?) is that nobody has noticed or commented on the new Irish girl!
Admin
Admin
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#valueOf(int)
Admin
Admin
she's not nearly as good looking. just a lame booth babe
Admin
Integer type is immutable
Admin
Admin
My biggest worry now is that, if you framework has a class named BLOB, your programs are doomed to create several BLOB objects during their lifetime.
Admin
I use a LOT of 0s and 1s....In fact everything I do seems to end up 0s and 1s
PS: I wouldn't be surprised if something along thesxe lines has been saidf (I'm too lazy to check)
Admin
saidf? Is that like a speech synthesis version of printf?
Admin
ROFL! Put down the Kool-Aid for a moment and take a look at the Gentoo Language Benchmarks Game.
By the way, the "benchmarks" you are referring to is an old rant written by an agnorant (spelling intentional) Java code monkey, and was thoroughly dissected on Slashdot over four years ago.
Your argument about JITting is also completely bogus for several reasons. First, HotSpot mostly helps Java solve performance problems that a well-written C++ program doesn't have in the first place because templates can be and are used for high performance compile-time polymorphism. C++ is currently replacing FORTRAN in number crunching and other supercomputing because compilers have come a long way especially when it comes to templates.
Second, C++ can be compiled to bytecode for JITting as well, C++/CLI is one example. As a sidenote, Java can be compiled directly to native code, which just goes to show that you are the poor victim of the classical false dichotomy logical fallacy.
Not if maximum performance or modest memory usage are requirements (and they often are). Also, Java's type system is too limited for writing highly reusable generic code. The euphemistically named "generics", which are just statically typechecked syntactic sugar for old boring object inheritance, just don't cut it. (Don't get me wrong, they are way better than nothing!) Templates may be ugly but they are far more powerful than what Java has to offer.
Admin
Actually "System.out.println(a == b); " will almost certainly return false in this case.
Remember you are examining the Object Reference value NOT the value of the object in this statement.
Or in other words, you are checking to see if pointer to Object a is the same as the pointer to Object b.
The only time this will equate to true is if both a and b are the EXACT same Object. Not the same value, but the SAME Object. This can happen if you assign them. (aka) a=b;
It can also happen as part of a compiler optimization to save memory space. This is what happens when you get true with the following code:
String a = "bob"; String b = "bob"; if(a==b){ //Will always happen because the compiler changed the //above lines to //String a,b = "bob"; //Which means that they both point to the same spot in memory }else{ //Never happens }
Ironically, the WTF here is that the IntegerType function will actually cause any IntegerType object to encapsulate the SAME Integer and not different Integers with the same value.
Admin
INTEGER_0 and INTEGER_1 are actually database login information. The code is obscured to protect from hackers.
Admin
Two large systems written in c & c++ had presumably evolved over some time - with a vareity of coders adding a vareity of hacks over the years. I'm sure for most apps that have been around for years, even re-writing them in the languiage they were originally written in would allow you to increase performance, stability and mainatinability.
When you start from scratch you don't have to concenr yourself about how big a hack this might be, and the effort required. You can fix things properly. Assuming you have any sense (and possibly even if you don't), you probably managed performance increases because you realised where things were done inefficiently/unnecessarily.
If you didn't, the whole task of rewriting such applications seems useless, other than to make a point here.
(I do love it when people try to subtly attack pooster's they disagree with, jusrt to make the world believe their point absolutely must be better)
Admin
Maybe it's time for you to read through some of the posts beforehand (esp[ecailly the ones that explain WHY the equality will return true for (certain) Integer objects. (I think someone even produced results that they claimed to have for actually RUNNING the code, not hypothesisng or guessing what might happen)
Admin
I am surprised! Usually Java gets bagged everywhere (especially sites like this).
I am absolutely flabbergasted how many people seem to have intricate knowledge (or at least opinion) of how Java works. Clearly the language has broader usage/support than I thought possible.
The end is nigh, people, flee to the hills!
Admin
So you replaced badly designed systems written in C and C++ with better designed systems written in Java, and as a consequence, performance was also improved. You said it yourself: "Performance, testability and maintainability" - if the designs were equally good, there would be little if any difference in at least one of those characteristics.
I could just as well show that quicksort implemented in JavaScript is faster than bubblesort implemented in C++ for sufficiently large input sizes. I have no idea what that would prove, but it could be done. In fact, I have written an advanced prime sieve program in Haskell that would certainly outperform a naive C++ sieve by far. But I definitely wouldn't be so foolish as to believe that this was any indication of the relative performance potential of the two languages.
I don't know whether it's still the case, but in Java 1.5, generic containers used Objects even if the element type was primitive, e.g. ArrayList<int> was actually ArrayList<Integer>. I'm not too fond of .NET generics either, but you have to give Microsoft credit for getting this right - .NET generic containers don't use boxing for (at least) the built-in value types.
Admin
My favourite part of the IntegerType code is that:
will throw a ClassCastException. Ditto for compareTo.Admin
The problem is that a full production benchmark is equivalent to doing the development, test, deployment to full service and then support of an application in each of the languages being checked. With that, you get to measure all the trade-offs throughout (e.g. faster code versus faster development) and understand what is actually right for you, and is pretty close (conceptually) to running a full scientific experiment. It's also brutally expensive, so hardly anyone ever does it. Instead, we use small benchmarks to test specific features, and we (if we're honest) attach big health warnings to the results of the benchmarks.
(In my experience, though well-written C can out-perform Java, it can be a real challenge to determine what that well-written C looks like, especially when memory-usage patterns are non-trivial. Java's more costly in system resources, but if you can produce a correct program more easily then you've frequently got a win overall. Of course, even bigger wins can be had by using multiple languages with differing abstraction levels but that's getting out of the realm of benchmark "discussions" and into real world programming...)
Admin
Admin
I believe it's time for Java 1.0xFF to introduce the concept of "temporary," as in overriding "final" for classes. Thus:
I think we can all agree that this is easily translateable into byte code, can only be used explicitly to break immutability where necessary, and obviates the hideous perceived requirement to aggregate like a wildebeest on heat that the OP suggests. In the field of languages in general, this is not known as an "optimization." It's known as a "regrettable kloodge."Java's not an especially attractive option for "taking stuff from the database and sending it to (the) browser," either.
Think "Green" and "Oak." Java was foisted on the world around fifteen years ago; dug deep to justify itself; and has never really been convincing outside either (a) evangelism or (b) job security. The goddamn thing's a mess and should be put out of its misery.
FTR, I'm not particularly a language bigot. I see value in C/C++/Assembler where appropriate. I use Perl for simplicity and for reliable cross-platform libraries (eg SNMP-Net). I use Python for elegant scripting. I'm reasonably happy with Javascript, in small doses, where appropriate. I'd use Haskell or OCamL or Scheme given the chance.
What I won't choose to use is Java. It's an insane language, in the same sense that insane human beings tend to paint themselves into a corner, produce delusional arguments in an attempt to persuade themselves that this is a good thing to do, and dribble all over themselves.
"Taking stuff from the database and sending it to browser?" Pah. From AWT 1.0 through Swing to whatever's new, jazzy, and fusion-orientated, it used to be that Java was great because you got cross-platform graphics for free. (I could go on about RMI, but why shoot a dead cow in the head?)
It's crap. Get over it.
The JVM is still pretty sweet, though. 'Bout time we saw comparisons between that and .Net ...
Admin
I think you forgot Ruby in your list of 'I'm so cool' languages. All the cool kids are now claiming perl, python and Ruby as their languages of choice! (although it was cool how you chose a particular dialect of Lisp {Scheme})
Admin
Yes... it's so non-WTF that Sun even decided to implement the same thing in Integer.java:
http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#valueOf(int)
It is implemented so that all ints between -127 and 127 are cached in the internal static IntegerCache class.
Admin
That's funny, I suddenly had a peculiar sense of Deja Vu'
Admin
The java.lang.Integer class is final, so MyInteger could be a way of working around that. So, for example, you could extend MyInteger with BrokenIntegerType() which throws NumberFormatExceptions in some weird cases, or extend MyInteger with MutableIntegerType() which has a setValue(int) method on it.
Ashamedly, I've followed a similar pattern for other classes such as java.io.File, or java.lang.Runtime. I know it sucks but I can't see a better way for unit testing against these kinds of classes.