• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why no pass-by-reference?  RSS feed

 
Debashish Chakrabarty
Ranch Hand
Posts: 234
Java Python Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recently a blog raised this question as to why Java designers chose not to have pass-by-reference (taking the classic swap() problem) and cited that .NET has "overcome the problem" by having the same. Now there may be hacks to perform a swap in Java but I am curious to know as to why Java does not have it? I remember having read (perhaps on Javalobby) that the pragmatism behind this issue is based on facts that problem with the garbage collection mechanism that would occur using pass-by-reference.
I would appreciate your comments on (a) the rationale behind not having pass-by-reference in Java and (b) how do we explain its merits when comparing with other languages/platforms that do have pass-by-value as well as pass-by-reference mechanisms.
Thanks for your time.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the most useful aspects of Java is its readability. I can look at code and know what it does. All the operators do what I expect, it's easy to locate any variable or class if you see a reference to it, fields and variables won't change unless you specifically tell them to by name, etc.
Assuming you mean C++ style references declared only by the method:
Pass-by-reference is a problem both for readability and security. You would need a way to disallow it in untrusted code, which would lead to annoying configuration and harmless library code breaking in odd settings. More importantly, I can't just read my code and know what it does. I have to start browing between files to know why local variables changed when I stepped the debugger over a method I'm not intimately familiar with. My IDE can't locate all assignments to my field with it's Find References functionality. It would take sophisticated debuggers examining running code to do what was once a simple lexical search.
If you mean references that are actually a part of a variables type (as in "int ref n = 3") then you're just introducing syntactic sugar for a feature that Java already has, mutable classes. Although we could always do it, in Java 1.5 supporting references will be easier than ever thanks to generics. A single reference class with only the methods get() and set() can support all types and still be typesafe. You can write "Reference<Integer> n = new Reference<Integer>(3)", assuming I have the 1.5 syntax correct. Not the most beautiful and compact code but in true Java fashion it's understandable, easy to write, and effective.
I think that's the idea behind Java. It wasn't created to be the most compact language, the most expressive language, the most language most optimized for speed, or the most innovative language. Java is understandable (to both humans and computer tools), easy to write (keyboard typing speed is not the limiting factor in software development), and effective (an amazing amount and variety of simple and complex code has been written in Java).
[ May 01, 2004: Message edited by: David Weitzman ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!