views:

1715

answers:

21
+23  Q: 

Java Urban Myths

Along the line of C++ Urban Myths and Perl Myths:

What are the Java Urban Myths? That is, the ideas and conceptions about Java that are common but have no actual roots in reality.

As a Java programmer, what ideas held by your fellow Java programmers have you had to disprove so often that you've come to believe they all learned at the feet of the same drunk old story-teller?

Ideally, you would express these myths in a single sentence, and include an explanation of why they are false.

+47  A: 

Java is slooooow.

We all know there's some tasks at which Java can't beat C for speed (like low-level bit-twiddling where Java has to do a lot of copying and type conversion while C can use unions and pointer juggling). By now we also should all have seen the benchmarks where Java code runs as fast as (sometimes a little faster than) C.

But still, often you only have to mention Java to get knee-jerk jokes about how horribly slow it is.

Michael Borgwardt
I ported a small internal message decoding library from C# to Java just to try to beat down that myth for some in-house locals. It decoded 4 times as many messages/second in Java. (and 7 times as many as the c# library running on Mono). :-)
nos
Unfortunately the first Javas that people saw, the ones embedded in Netscape 4 and the equivalent IE, *were* dog slow. And unfortunately there is still a grain of truth in it - some JVMs on some platforms don't make use of graphic accelerations, so you can have a graphics demo that runs great on XP but dog slow on OS X, for instance.
Paul Tomblin
A java app is necessarily slower than the equivalent C program due to the time it takes to compile the bytecodes. This is the price paid for portable executables. This is probably not an issue for long running programs with JITers, but should still be noted.
Joel
Yes! I most recently saw this claim in an otherwise excellent cycling book, "Training and Racing with a Power Meter", which said that a particular analysis program was unfortunately written in Java so it was slow.
David Johnstone
To be controversial, I've rarely seen a Java program that was fast. They always seem to be slow compared to similar programs that weren't Java apps.
Paul Nathan
@Joel there actually are Java to native code compilers available (eg: GCJ, a Java frontend for gcc), so assuming Java == jit is a bit of a conflation. That said, Hotspot can do some pretty interesting things that ahead-of-time compilers can't do. For example, it can do dynamic deoptimization. This allows it to perform very aggressive optimizations that it can later undo if it determines that they aren't general enough. It can also make use of real-world profiling information to figure out what actually needs inlining, while an ahead of time compiler can only guess.
Laurence Gonsalves
"A java app is necessarily slower than the equivalent C program due to the time it takes to compile the bytecodes." Fair enough, but if the code is only going to be executed once or twice, but if that's the case who cares! After it has been executed a few time this overhead will be insignificant and not just in long running servers.
vickirk
"I've rarely seen a Java program that was fast." I've seen some very slow c++ apps too :-) There are quite a few applications out there that people do not released are java based, e.g. those based on eclipse (Vuse bit torrent client/Oracles SQL Developer). Maybe it is the ones that less experienced developers have wrote that people instantly recognise as java applications?
vickirk
IBM have a JDK that will save the compiled machine code to disc.
Tom Hawtin - tackline
@joel: You can just as well argue in the opposite direction: A C program is necessarily slower than the equivalent Java program, since the C compiler usually has no clues about the actual runtime system. Since Java programs are finally compiled on the system they will be running, the compiler can perform system dependent optimizations, which are not possible for a C compiler (unless you want to compile different C binaries for every possible target hardware).
jarnbjo
@vickirk: I can't account for it, but I've noticed that slow programs have Java UI elements often. Perhaps the really skillz developers spent the time to make a faster program and the time to make a native UI. But, from my user's perspective, java is slow.
Paul Nathan
@Paul: If you've ever seen a stack trace from a Swing application, you'll have an idea why it's slow. Those stacks are usually at *least* thirty or forty levels deep. I'm not sure what they hoped to gain by having that many layers.
Michael Myers
@Paul: If an application uses the ugly java ui/swing look and feel then that is a good indication that the developer is a newbie, any application used by real users is almost always setup to use teh platform or a custom look and feel, if the developer doesn't know how to do that there is a good chance they don't know how to do other things. Apologies to anyone who likes the default x-platform look and feel. BTW, 80% of teh software I use daily is implemented in java and i'm happy with the performance with them all (maybe eclipse could be better but I do ask it to do an awful lot).
vickirk
@vickirk: I'm not trying to provide a technical explanation for the slow feel. Java is not my expertise area. I'm just saying, "hey, this is what I've noticed".
Paul Nathan
@mmyers: I think they were trying to implement a useful, flexible and easy to use UI library, for that you need a bit of complexity ;-)
vickirk
@Paul: Sorry if it came across like that, I was trying to suggest why you might have noticed java apps are slow, some java apps instantly look like they were implemented in java and many do not. I was suggesting that those that look like java apps were more likely knocked together by inexperienced developers, the ones that don't look like that you may not have known were java, hence to the casual eye java apps are inherently slow.
vickirk
A huge part of this myth is the loose meaning of "Java app", which can mean any of the following: server-side web application, client-side console application, desktop GUI application, applet, mobile phone app, etc.
matt b
@mmyers, deep stacktraces does not automatically imply slowness. HotSpot inlines a lot of calls.
Thorbjørn Ravn Andersen
@Thorbjørn: Yes, I'm aware of that, but from my memory most of those are not simple wrappers which are easily inlined.
Michael Myers
I don't think Java is necessary slower on bit-twiddling than a well written C program (that excludes type conversion via unions or pointer casts, as they are not portable).
starblue
Although Java is comparative slower than its native C counterpart, but consider its Time to Market. Is still worth considering using Java most of the times, where I can kick off my competitors easily by pushing my product to the market FAST.
Yan Cheng CHEOK
+13  A: 

That Java and the Java Virtual Machine are the same thing

oxbow_lakes
This is partly Sun's fault. They put a lot of work early on into confusing the Java language and the Java "platform".
Laurence Gonsalves
+2  A: 

Myth: Being statically typed Java's less powerful than dynamically typed languages

[Edit] Ok, so it seems some explanation might be helpful here. On a theoretical level almost all computer languages, apart from SQL, are equally powerful (see http://en.wikipedia.org/wiki/Turing_completeness). Practically this means that for in any language X you can write a compiler or interpreter for any language Y thus making them more or less identical and thus equally powerful.

On a more pragmatic level these days it's not the language that matters most of the time, but the IDE, libraries, APIs etc that make for good programming.

Carsten
+1 Considering nearly all new script languages are statically typed
Helper Method
What new scripting languages do you speak of? All the ones I know are dynamic..
Earlz
@Helper Method what are these statically typed scripting languages you speak of?
Laurence Gonsalves
Groovy++, Scala, JavaFX
Helper Method
Oh. You mean new scripting languages *for the JVM*... (@Carsten: see, this isn't a myth, it's an arguing point - you can't prove it or disprove it, and didn't try)
Shog9
What do you claim is the truth here? I've heard performance, documentation, testing, and compiler simplicity as arguments for static typing, but I've never heard an argument that static typing is equal in expressive power: if it was, static typing would be better at everything! HLLs are higher level *because* you say less but the same words can mean more; having dynamic typing is more powerful for the same reason that having inheritance is: you can say less and mean more. I'd like to hear why you think this is a myth.
Ken
@Helper Method: Scala isn't a "scripting language".
Laurence Gonsalves
@Ken: I think you're confusing the statically typed languages you've used with statically typed languages in general. Yes, languages like Java where you have to provide explicit type annotations on everything mean lots of typing, but there are other statically typed languages where type inference is used. When using those languages the difference between static and dynamic typing becomes: dynamic typing lets you write code that a compiler cannot prove is "sound".
Laurence Gonsalves
+17  A: 

Write once, run anywhere: from embedded controllers to enterprise servers it doesn't need optimisation per implementation

Preet Sangha
"ask anyone who has written as a J2ME app for a phone or portable device will tell no not true" - not really javas fault, J2ME is a pile of pants, if only OSGi matured years ago.
vickirk
Test everywhere.
trashgod
I don't see this as a myth, because for the common case of Java SE and Java EE apps running on Windows, Linux and MacOS, you really have WORA to an amazingly large degree.
Michael Borgwardt
+33  A: 

Javascript is a subset of Java. This might be an old one but some people still believe it.

Byron Whitlock
I saw a FAQ once that had the question, "What is the relationship between Javascript and Java?", to which they gave the answer, "Their names begin with the same four letters.".
Jay
It is for this reason that a lot of people prefer to call it EMCAScript to avoid the people that say "Oh well I dont want to install java"... Sadly, this usually just leads to more confusion
Earlz
Sigh, now I have to say it...Java is to Javascript as Car is to Carpet...
Maltrap
@Earlz - probably just a typo - but it's ECMAScript not EMCAscript ! :-). It's a shame that another name wasn't chosen in the first place for Javascript - but I never liked the term 'ECMAScript' - sounds like a medical condition; so I'm forced to keep the 'Javascript' meme alive and well: which then inadvertently keeps the 'Javascript is like Java' meme alive...
monojohnny
Scala is the real Javascript!
+3  A: 

That Java is a simple language. Yes, the core language is simple, but most of the complexity is just pushed into the libraries.

dsimcha
1.0 and 1.1 *were* simple. But that was a looong time ago.
Jörg W Mittag
@Jorg: It's still simple compared to C++, D, C#, etc. The point is that this is not necessarily good because the complexity ends up in library APIs when there aren't any good features geared toward library writers to make these APIs more useable.
dsimcha
The hidden complexity is often a reason for bad performance. Because people don't know what REALLY happens when they do this or that (the + operator for String concatenation coming to my mind), they end up writing reeeally bad code.In order to write efficient code in Java, you really need a deep understanding of what's going on under the hood (which can take some time).
Helper Method
+12  A: 

Myth: Everything is an object

(Even though those instructors try to force OOP down your throat)

Earlz
This is more true in Java than C++, but still not completely true. That's why we have boxing and unboxing issues.
Joel
Goddangit, yes! I wasted about half a year at university wrstling wtih compiler errors till I'd understood that there simply isn't a way to put an int into a Vector...
Michael Borgwardt
Nope, that's Smalltalk and Eiffel.
Matthew Flynn
I don't understand why such an OOP centric language as Java decided to not make the primitive types objects as Ruby or other strongly OO languages are..
Earlz
@Eartz: Performance reasons woudl be teh main one, but how would you write 2.4 * someNumber / 4.2 - 4 + 2 without adding complexity to the language?
vickirk
@Micheal - you were either a victim of singularly bad teaching ... or you skipped too many lectures :-)
Stephen C
vickirk: Except they tried to retrofit this (with autoboxing) later, anyway -- if they'd simply made everything an object from day one, the language would be *simpler* today.
Ken
@vickirk: 1) Doesn't really work, you can't create your own primitive types. 2) Operator overloading.
Bastien Léonard
+3  A: 

.net and Java - oh they're the same. We can change over really easily later in development.

Preet Sangha
What? Has anyone really said this?
Michael Myers
Management: "Well they both run on a virtual doo hickey, so they are basically the same then huh? "
Earlz
Thanks @Earlz - usually to the PHB
Preet Sangha
I think this one depends on your position in the world. They both roughly solve the same problems at the same development pace at about the same business cost. If you're a CEO of a small non-tech franchise and need a internal webapp built, the deciding factor will be the contractor's bid price, not the technology they use.
Matt Garrison
+13  A: 

Java is dead.

(See this 1999 reference.)

Michael Brewer-Davis
i never heared such a comment , what it really means
sreejith
@sreejith - it is opinionated rubbish that has clearly been proven wrong by history.
Stephen C
It's not dead - it's resting!
Piskvor
+10  A: 

You lose control over memory mismanagement if you use Java. Nope you can still write memory hogs regardless of the garbage collection.

Preet Sangha
That's more of a GC myth than a Java myth though
sfussenegger
@sfussenegger: Java's GC is touted pretty loudly, so I'd say it's a fully valid Java comment. Also, less professional Java coders tend to assume they don't need to think about memory at all since there's GC around.
Marcus Lindblom
@marcus your absolutely right. I didn't day it's not a valid comment; I just pointed out that this myth isn't specific to Java.
sfussenegger
I'd say it's half-true: compared to some other languages with GCs, it does provide fairly little in the way of tunability or escapes (though with recent versions it seems to be getting better). The Java way has always been "don't worry about that, the runtime will try to do it for you as best it can", rather than the "runtime does what it can, and we provide 47,000 hooks to let you fill in the rest" approach some other languages take.
Ken
+30  A: 

"Java function / method arguments are passed by reference"

I have argued with some pretty intelligent people about this one. I would guess that some people reading this thread might still be inclined to disagree (though I suppose it wouldn't be a very good answer if that weren't the case).

danben
Ignoring primitives, I assume you are referring to a method parameter being an object reference and not a reference to an object reference.
vickirk
_Everything_ in Java is passed by value. Often those values are references to objects but the reference itself is still passed by value. I think that the fact that you're passing references tends to confuse people into thinking that you're passing _by_ reference.
Jonathan M Davis
A: 

Java is the new C++. Again circa 1995

Preet Sangha
+5  A: 

Asserts are of no use / are poor man's exceptions

Helper Method
+1. Asserts have two advantages: They can be disabled in release mode and they're easier to write (less typing), encouraging their liberal use for simple sanity checks.
dsimcha
Asserts are useful if you think of them as compiled comments, nothing more.
Kevin Bourrillion
They're also useful in complex private methods.
Helper Method
Always use the long form of assert which contains a description of what has actually happened!
oxbow_lakes
+6  A: 

There are no pointers in Java

Chinmay Kanchi
There aren't! the only pointer in java is the word Pointer in NullPointerException! Sure they are used by the jvm but that works directly on memory, what else is it going to do.
vickirk
Yes there are. All objects are pointers in Java. What Java doesn't support is pointer arithmetic...
Chinmay Kanchi
@Chinmay, this is going to depend on your definition of teh word pointer. To most developers I know, the value of pointer is generally considered to be a memory address and that is not the case in java. True, if you want to use the wider sense and say a pointer simply points to an object you would be correct, however when people say "There are no pointers in Java" I'm pretty sure they have my definition of a pointer in mind.
vickirk
@vickirk: Most developers learn about pointers in C, and thus that becomes to them the definition of "pointer", which I consider a bit narrow-minded. And *of course* a Java reference contains a memory address - it's just not directly accessible to you.
Michael Borgwardt
C programmers don't usually care that a pointer is a memory address either. A pointer is an abstraction. That's why C pointer arithmetic is automatically scaled, and you get a warning when you convert a pointer to/from an int.
finnw
Using the wider meaning of the word pointer gives it the same meaning as reference, that make the orginal answer to mean the same as "There are no references in java" which is clearly wrong. True references have more in common semantically with say a c++ pointer than a c++ reference, but I would say they are more like a handle than a pointer.
vickirk
@Michael - Your last sentence is not necessarily true ... depending on what you mean by "contains"!! For example, I've seen a Java implementation in which all Java references were offsets into an array of machine addresses.
Stephen C
"I've seen a Java implementation in which all Java references were offsets into an array of machine addresses." Re-enforces the claim that java references are more akin to handles in the C world than pointer, they provide a handle to get hold of the object.
vickirk
+1 for this; I believe the accurate statement (I read this in some O'Reilly book - can't remember which) is that Java _does_ have pointers, but does not allow pointer artithmetic. So you everytime you instantiate an object or get one from a factory (etc) you get a pointer(reference) given to you, but you can't do anything to that pointer other than use it to reference methods and fields - you can't add one to it or 're-point' it somewhere else in memory [unless you ask for a new reference by instantiating etc again].
monojohnny
Isn't a reference the abstraction of a pointer, which in turn is the abstraction of memory addresses? A pointer can be incremented and manipulated directly, whereas a reference only allows us to manipulate what it references, or be dereferenced.I think this is an important distinction to either respect or formalize; if we as developers don't respect or maintain our own nomenclature do we not become no better than the marketing execs who turn them into buzz words?
LeguRi
Richard: Well, this is Java we're talking about here. :-)
Ken
A: 

MYTH: Java doesn't support multiple inheritance.

This one is a huge Java myth: you can have multiple interface inheritance and THERE ISN'T A SINGLE THING THAT YOU COULD DO WITH CONCRETE IMPLEMENTATION INHERITANCE THAT YOU CANNOT DO TOO WITH INTERFACE INHERITANCE.

As a matter of fact some OO languages are "purely abstract" and support multiple inheritance.

As another matter of fact James Gosling himself said regretting not having gone "pure interface" with Java:

http://www.artima.com/intv/gosling13.html

And as the final matter of fact Bjarne Stroustrup said something similar regarding C++'s "pure abstract classes":

http://www.artima.com/intv/modern.html

Since then I have consistently pointed out that one of the major ways of writing classes in C++ is without any state, that is, just an interface.

So I think that "Java doesn't support multiple inheritance" is one of the biggest myth about Java. Usually spread by people that don't understand neither OO nor delegation.

Note that the article by Gosling I linked to above talks exactly about this and it mentions delegation. Because this is what it's about.

Sadly a lot of not-so-knowledgeable programmers mistake "multiple implementation inheritance" (which Java doesn't support) with "multiple inheritance", which Java fully supports.

Same programmers are very confused about mutliple inheritance and mistake "inheritance" for "code reuse", which is of course rubbish.

And of course there isn't a single OOD using multiple inheritance that could not be translated to a Java OOP.

So you may disagree with me but then you'd be disagreeing with Bjarne Stroustrup and with James Gosling too and, anyway, it would be impossible for you to come up with a single OOA/OOD example using multiple inheritance that I could not translate to Java using multiple interface inheritance.

Webinator
There's not a single thing you can do with ALGOL you can't do with COBOL but that doesn't mean that COBOL supports ALGOL.
Ken
Willi
Woaw woaw woaw just woaw. The fact that I'm getting voted down just shows how big a myth this is. People simply don't get it. Vote me down, disagree with the father of C++ and with the father of Java. All the people who modded me down are completely clueless. I knew it and my votes down proves it: this is the biggest Java myth ever. And people simply don't get it. This is amazing, just amazing. It shows how much clueless people are about OO.
Webinator
BTW, to all the clueless who anonymously modded me down, you have no clue but please do have at least have balls and post a comment so that the world can see how clueless you are.
Webinator
@Ken: no kidding? and all Turing language complete languages can reproduce anything that can be done in any other language. I know that. The point is that it is *trivial* to do in Java: any OOA/OOD using the most complex multiple inheritance hierarchies can be trivially translated to a Java OOP using multiple interface inheritance. And "code reuse" / "implementation details reuse" can be achieved using delegation. But you don't understand this, nor the people who voted your comment up, nor the people who vote my post down. It doesn't exactly make my statements less true so no biggie.
Webinator
@WizardOfOdds: the fact that you're trying to back up your claim with some combination of unsubstantiated assertion, irrelevant quotes and *insults* is proof of nothing beyond your inability to provide a reasonable answer. If you can't prove your myth is a myth, then it doesn't really belong here...
Shog9
Wizard: I understand inheritance just fine. "Can be trivially translated to" is quite distinct from "supports". Pascal can be trivially translated to C but I don't think anybody would say that this means C "supports" Pascal.
Ken
@Shog9: sadly if you think that Gosling talking about Objective-C's "pure interface" (which btw allows translationist (the correct term) to translate (yes, it's the term once again) *any* OOD to OOP in Objective-C (just like any OOD using MI can be translated to OOP in Java using interface) and Bjarne Stroustrup mentionning that a major way to model programs is using C++'s "pure abstract classes" is irrelevant then you're obviously one of those falling for the Java myth. The fact that I'm +2/-5 just shows how much a myth it is: people like you simply don't understand OO despite your high rep.
Webinator
Java _doesn't_ support multiple inheritance. You may be able to get along just fine without it (after all, multiple inheritance can be quite dangerous), but regardless of whether multiple inheritance is a good idea, Java still doesn't have it.
Jonathan M Davis
@webinator: Code example pls
Øyvind Skaar
+3  A: 

String concatenation is expensive, and you should use a StringBuffer/StringBuilder

While true in some cases (e.g., concatenation within loops), modern Java compilers will optimize String concatenation (using the + operator) to StringBuffer/StringBuilder based concatenation anyway.

Jack Leow
In fact there is no "String concatenation" in java, except StringBuilder/Buffer.append(..). All + operations on Strings are compiled into append()-calls.
Willi
String concatenation _can_ be expensive. True, in some cases it can be optimized away, but not always. I've seen programs which tested it and showed StringBuilder to be significantly faster (though I don't have the code for such a test at the moment). If you care about performance, don't risk it: use StringBuilder. However, if it's not code that really needs to be fast or you're not doing much concatenation, then there isn't necessarily a big gain with StringBuilder. It _can_ matter though.
Jonathan M Davis
+6  A: 

Myth: Object pooling is required for performance.

A few years ago Java, it seemed like everyone was espousing pooling objects to avoid the "cost" of creating and disposing frequently used objects over and over again. Aging information on The Internet being what it is, I see people who still run into this old chestnut every once in a while.

The truth: from java 1.4 on (at least), generational garbage collectors have made the cost of creating and reclaiming an object cheaper than looking it up from a stored cache, especially when the cache has to be thread safe. Future optimizations (stack allocation via escape analysis, for example) threaten to make it even cheaper.

The moral of the story: it's likely best to just create objects when needed and throw them away, unless your profiler tells you differently.

Sean Reilly
+7  A: 

Object reference creation is expensive

I've seen people declare references outside of of loops thinking it is "more efficient," at the expense of the reference being scoped wider that it has to be.

Jack Leow
Talk about premature optimization.
fastcodejava
+4  A: 

Java is dead for the desktop

There were some questions about that on SO. No, it isn't dead - it has many frameworks, components and tools and much enterprise software is made using a desktop client.

Swing additions in JDK 7 are here to support the thesis that Java isn't dead for the desktop.

Bozho
You should split this into three answers.
Shog9
@Shog9 - where is this written as a rule? ;)
Bozho
Ok, so if you want to allow folks to vote for one answer when they don't necessarily agree with *all* of your answers, you should post them as separate answers. See also: http://meta.stackoverflow.com/questions/9782/what-about-a-multi-answer-question
Shog9
ok, sounds reasonable.
Bozho
SO founders are known to propagate this myth/opinion :) http://blog.stackoverflow.com/2009/03/podcast-44/ "Joel thinks that Java on the desktop is essentially dead. I don’t necessarily disagree [..]"
Jonik
Not only are there frameworks etc (we certainly don't lack those), but some great Java desktop apps exist: http://stackoverflow.com/questions/521110/examples-of-good-java-desktop-applications (Though admittedly many of the best ones are sw dev tools.)
Jonik
+9  A: 

Imports can affect performance

Or, having package.* and unused imports has performance impact. No, it doesn't - this information is used only at compile-time.

Bozho
Excessive imports can increase compilation time, but it's irrelevant once the code has been compiled.
Jonathan M Davis
I think its the biggest since 90% of Java programmers was taught that. ctrl shift O in eclipse.
01
+2  A: 

Nobody uses applets anymore

Au contraire. In many enterprise environments, where it's guaranteed that the users have the appropriate JRE installed, applets are used for many tasks, such as digital signing, customized printing, etc.

Bozho
+1 JavaFX apps also run in Applets.
Helper Method
Applets do (did) generally suck though; Java Web Start is (was?) a lot better deployment option for Java apps. (Though I must admit I don't know how JavaFX uses applets.)
Jonik
@Jonik - I've been using applets for digital signing of documents - almost no alternatives.
Bozho