2

Possible Duplicate:
Why there is no way to pass by reference in java

Can anybody tell me why exactly Java does not provide C# "out" type feature when dealing with method parameters to pass by reference ? I mean why would not it allow us to pass primitive data types like boolean for example, pass by reference. I have tried also with wrapper class java.lang.Boolean but still to no avail. It still wont allow me to pass variable by reference.
Is there any specific reason why Java still has not provided us with this even in version 7 ?

Community
  • 1
  • 1
Faisal Mq
  • 5,036
  • 4
  • 35
  • 39
  • You just have to make a cut somewhere when you design a language. The more unnecessary features a languages has the less useful it gets … – Kijewski Apr 26 '12 at 05:52
  • .Net (C#) had took second movers advantage, they show what application developers are missing in Java and incorprated like "Pass by refrence of primitive type". Now Java was not intialy design to allow, now for Oracle to provide it now may be quiet complicated, how stack and heap are managed by JVM/JRE, lot of other tech constraints in adding this feature now. – Pritesh Apr 26 '12 at 05:55
  • @Pritesh: Java isn't exactly the first language invented, we have had *tons* of languages before Java, so the 'second movers advantage' isn't a strong argument. – Arafangion Apr 26 '12 at 06:30
  • Hey @Arafangion i know there were n no. of languages before Java :)Java and .Net came as derivative of C++, C++ (with OS tapping) had answer to most questions in application software developemnt except Productivity. Java was first one to come with high Productivity over C++ for that they removed feature from C++ which were not easy to use. .Net got some time so they noticed what c++ kind features programmers were missing in Java and incorprated them in .Net For e.g. Opeartor Overloading. – Pritesh Apr 26 '12 at 06:58
  • @Pritesh: That is *hugely* arguable. Curious you didn't mention the BCL influence. – Arafangion Apr 26 '12 at 07:38

4 Answers4

5

Java only has pass by value. This was a decision made when the language was designed.

There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.

-- James Gosling, et al., The Java Programming Language, 4th Edition

If you want you can put your boolean as a member inside a mutable class (you can't use Boolean because it is immutable), and pass a reference to that class. Apache Commons even has a class called MutableBoolean that you can use.

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Thanks Mark. Yes, Apache Commons MutableBoolean class gives us nice alternative. But still I do not understand what the creators of java mean by "keeping things simple". – Faisal Mq Apr 26 '12 at 06:00
  • @TopGun743 If a parameter can be passed either by reference or by value, that is one additional level of complexity to worry about, both for the programmer and for the compiler. – trutheality Apr 26 '12 at 06:30
1

Only the language design team could tell you why, but I believe the reason for not allowing "out" parameters might be something like: If you want a method that calculates two things, what you really want is either two methods, or one method that returns an object. This supposedly leads to better design and more maintainable code.

Note that you if you really want "out parameters" you can easily use arrays of one element. For example:

void div(int a, int b, int[] q, int[] r) {
    if (q != null) q[0] = a/b;
    if (r != null) r[0] = a%b;
}

// elsewhere:
int[] quotient = new int[1];
int[] remainder = new int[1];
div(4, 3, quotient, remainder);
trutheality
  • 23,114
  • 6
  • 54
  • 68
Joni
  • 108,737
  • 14
  • 143
  • 193
0

This is just my opinion but I feel that the designers of Java believed they could simplify programming by eliminating features rather than making them more intuitive and easier to handle.

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
0

Short answer is that it's a design decision and there's nothing you can do with passing by reference that you couldn't do with passing object references by value.

As for your particular problem, there are two solutions:

A mutable wrapper class:

final class BooleanRef {
    public boolean value;
}

And use it as:

// Function
void changeTheBoolean( BooleanRef b ){
    b.value = true;
}

// Call:
BooleanRef b = new BooleanRef();
changeTheBoolean( b );

OR, (more hackish but more lightweight) wrap in an array:

// Function
void changeTheBoolean( boolean[] b ){
    b[0] = true;
}

// Call:
boolean[] b = new boolean[1];
changeTheBoolean( b );
trutheality
  • 23,114
  • 6
  • 54
  • 68