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?
views:
359answers:
8Having 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.
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.
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.
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.
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.
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.
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.
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)
{
...