tags:

views:

359

answers:

8

While I am not a huge fan of using the out parameter in c# I would like to know why Java chose not to include it in its language syntax. Is there any special reason or maybe its because a person can simply pass an object as a parameter type?

+2  A: 

Having an out parameter basically allows for two return values from a method.

Java left this feature out to simplify it's syntax and remove the potential for misuse and programming errors.

jjnguy
Hard to make programming errors when you have to explicitly type out even when you call the function.
Blindy
I didn't say that I agreed with it. But I can see where the designers may be coming from.
jjnguy
+1  A: 

In order to return more than one value you can have your method return an object or a collection of objects. So you don't necessarily require out parameters to return more than item from the method.

neesh
But it is convenient for methods like TryParse which returns a bool and uses an out argument for the parsed value.
Brian Rasmussen
I agree. In certain cases it would be more convenient to have out arguments; the language designer(s) probably had to make a trade off between convenience and adding complexity to the language.
neesh
returning complex objects can be great. But when you need to drag that last bit of performance from the system "out" and "ref" are very handy.
Matthew Whited
@Matthew Why would you want to use Java if you really that last bit of performance.
GuiSim
+8  A: 

Probably because the designers didn't feel the need to allow for multiple ways of returning objects.

The same question can be asked about delegates, generic, etc.

But the fact of the matter is that the C# designers learned from Java's mistakes/inconveniences, which is why many I've spoken to feel C# is the nicer language to work with.

hythlodayr
+1  A: 

You make it sounds as though Java and C# were designed around the same time. Remember, Java is nearly 15 years old. The lack of out parameters (if you can call it a lack) is the least of its omissions.

skaffman
Sometimes I resist the urge to jump over to Java due to its "lack" of features compared to c#...heard word of Java 7, hope this will finally make me swap over.
Draco
I'm currently looking with great interest at Scala... all the advantages of the java platform, but with a modern, statically-typed language. Lovely. Java 7 looks increasingly remote and irrelevent.
skaffman
@Draco - Java 7 has apparently blown the chance to catch up. Strangled by "community process".
Daniel Earwicker
@Earwicker - sigh...
Draco
A: 

C# has a much lower barrier to entry for random features. "Out" parameters are not that useful (and get abused), so they are not supported in Java. There were lots of features submitted under Project Coin for JDK7, but IIRC out parameters were not among them.

Tom Hawtin - tackline
@Tom: I think saying 'C# has a much lower barrier to entry for random features' is being a bit unfair. Read: http://blogs.msdn.com/ericlippert/archive/2009/06/22/why-doesn-t-c-implement-top-level-methods.aspx: I am asked "why doesn't C# implement feature X?" all the time. The answer is always the same: because no one ever designed, specified, implemented, tested, documented and shipped that feature. All six of those things are necessary to make a feature happen.
Grant Wagner
@Tom: All of them cost huge amounts of time, effort and money. Features are not cheap, and we try very hard to make sure that we are only shipping those features which give the best possible benefits to our users given our constrained time, effort and money budgets.
Grant Wagner
@Tom: And then there is: http://blogs.msdn.com/ericgu/archive/2004/01/12/57985.aspx: So, we decided on the additive approach instead, and worked hard to keep the complexity down. One way to do that is through the concept of “minus 100 points”. Every feature starts out in the hole by 100 points, which means that it has to have a significant net positive effect on the overall package for it to make it into the language.
Grant Wagner
Nothing remotely random about out parameters - they're for interop.
Daniel Earwicker
+7  A: 

Java was designed to be a very simple language, with a very simple syntax - a kind of "Spartan OO programming language" in contrast to C++ with its abundance of features that hardly anyone knows and understands completely (including compiler implementors).

Basically, features were omitted unless they were perceived to be absolutely necessary.

On one hand, the goal was achieved - there are very few areas in which Java's behavior is hard to understand or predict.

On the other hand, the missing features have to be worked around, which leads to Java's oft-maligned verbosity. The designers of C# tried to build on this experience.

However, I personally wouldn't count out parameters as a great loss.

Michael Borgwardt
I agree, out parameters have a very limited value. In most cases I've only seen out parameters miss used.
Chuck Conway
A: 

I feel like I must be missing something here, so I'm fully prepared to be downvoted into oblivion...

Doesn't Java support "out" parameters equally as much as C++? My C#-fu is not very strong, but I thought the out keyword was just a way of telling the compiler to allow objects to be changed inside the method. If I'm right, then both Java and C++ support this too, just without the explict (and IMHO nice distinction) of using a keyword. In C++, you can pass a pointer or a reference to the function and the function can modify the object in whatever way it wishes. Perhaps it would be more like C#'s implementation to pass in a pointer to a pointer and have the function allocate an object there, but whatever. In Java, you can achieve the former exactly the same as using references in C++, since every object is a reference in Java.

So, where's the error of my ways? There must be a reason no one has mentioned this yet. I look forward to learning what it is.

rmeador
I think that approach was defined in the question with "maybe its because a person can simply pass an object as a parameter type?"
Yishai
Both the ref and out keywords allow the method to overwrite the object reference, whereas a regular parameter can't be overwritten (although it can be modified through the reference). The difference bet'n ref and out being that in the latter case the object doesn't need to be initialized (since its an out parameter)
Amit G
To clarify - Foo(out x) where x is a reference to some object, after that method call the variable x itself may have been modified to point to a different object. This is very different from just being able to modify the contents of the object.
Daniel Earwicker
+2  A: 

The number 1 use case for out parameters is smooth interop with native code.

As Java does not prioritise smooth interop with native code, it doesn't especially need this feature.

There's no logical need for output or ref parameters. As they are available, they have become part of a common idiom in the CLR languages, the "TryXXX" idiom for a non-throwing version of a method. But that could have been done by returning a compound value type (a struct in C#) instead:

struct TryResult<T>
{
    public T Result;
    public bool Succeeded;
}

TryResult<int> Parse(string intString)
{
    ...
Daniel Earwicker