Linked by Bjorn Raupach on Thu 17th Jul 2008 06:01 UTC
Thread beginning with comment 323470
To view parent comment, click here.
To read all comments associated with this story, please click here.
To view parent comment, click here.
To read all comments associated with this story, please click here.
RE[2]: Never programmed in Java before...
by Vanders on Thu 17th Jul 2008 17:30
in reply to "RE: Never programmed in Java before..."
RE[3]: Never programmed in Java before...
by FunkyELF on Thu 17th Jul 2008 18:26
in reply to "RE[2]: Never programmed in Java before..."
With C++ you'd just pass multiple values by reference, rather than muck about trying to "return" multiple values.
Then there is no "contract" in the function prototype. It is nice to know by looking at the arguments what can be changed and what can't be changed. You wind up looking at the code to see what is really going on, or actually reading the documentation which nobody wants to do.
Pointers are an ugly hack. The output variable is an input to the function?
RE[2]: Never programmed in Java before...
by tristan on Thu 17th Jul 2008 17:31
in reply to "RE: Never programmed in Java before..."
"but couldn't you just return an array of pointers to the multiple data values you want to return?
1.) Java doesn't have pointers.
2.) There's no type safety in that approach (meaning it's not a good idea for C++ too) "
My first thought in C++ would be to have a templated ReturnTriple struct along the lines of:
template<class A, class B, class C>
struct ReturnTriple
{
A first;
B second;
C third;
};
which is cheap, type-safe and reusable.
EDIT: Or, as another poster pointed out, just pass parameters by reference. Is this not possible in Java?
Edited 2008-07-17 17:37 UTC
RE[3]: Never programmed in Java before...
by saucerful on Thu 17th Jul 2008 18:29
in reply to "RE[2]: Never programmed in Java before..."
Java doesn't have data structs so it would have to be a class but the same concept works.
In fact it makes sense... if you're returning multiple values, they have a relationship. Chances are that that relationship will be studied elsewhere in the code. For instance other methods will take as input both of those pieces of data. Or there will be methods that specifically massage that data. What better place to put them than in this return value class? So in the end such a class almost always pays off by making code easier to read and more organized.
Edited 2008-07-17 18:31 UTC
RE[3]: Never programmed in Java before...
by FunkyELF on Thu 17th Jul 2008 18:43
in reply to "RE[2]: Never programmed in Java before..."
My first thought in C++ would be to have a templated ReturnTriple struct along the lines of:
template
struct ReturnTriple
{
A first;
B second;
C third;
};
which is cheap, type-safe and reusable.
template
struct ReturnTriple
{
A first;
B second;
C third;
};
which is cheap, type-safe and reusable.
The first poster on here gave a nice link for javatuple which does something very similar. I just looked and it seems real nice. it has classes for Single, Pair, Triple etc. up to Decuple.
I'm not sure if your C++ example retains type safety but this javatuple package does.
EDIT: Or, as another poster pointed out, just pass parameters by reference. Is this not possible in Java?
Java has Objects and primitives (because back in the day people were bitching about performance). Primitives are pass by value, Objects are passed as a reference. This means that if you pass a Vector to a method, because it is an Object, if you add or remove from that Vector in the method the changes will be visible after the method is called. However, even though Integer is a class, there are no methods to change the value of one. So, a method that needs to return two integers could return a List<Integer> or an Integer[], but things get ugly when you need to return an Integer and a String.
RE[2]: Never programmed in Java before...
by msundman on Thu 17th Jul 2008 18:29
in reply to "RE: Never programmed in Java before..."
RE[3]: Never programmed in Java before...
by tristan on Thu 17th Jul 2008 19:58
in reply to "RE[2]: Never programmed in Java before..."
RE[2]: Never programmed in Java before...
by IvoLimmen on Thu 17th Jul 2008 20:03
in reply to "RE: Never programmed in Java before..."
1.) Java doesn't have pointers.
Java works with references and a reference is a typed pointer. So if you use Object you kind of are using pointers...
2.) There's no type safety in that approach (meaning it's not a good idea for C++ too)
Exactly: that's why Java made them type-safe.
RE[2]: Never programmed in Java before...
by ebasconp on Thu 17th Jul 2008 20:57
in reply to "RE: Never programmed in Java before..."
1.) Java doesn't have pointers.
Where do you get that assert from???
Everything in Java but the primitive data types are referred via pointers (ok, let's call them "references", but in Java they are the same).
When you declare
MyObject obj = new MyObject();
you are creating an instance of class MyObject and you are assigning a pointer to that instance.
The only difference here is that you cannot do pointer arithmetic (as in C).
RE[2]: Never programmed in Java before...
by Ralf. on Thu 17th Jul 2008 22:14
in reply to "RE: Never programmed in Java before..."







Member since:
2005-08-29
1.) Java doesn't have pointers.
2.) There's no type safety in that approach (meaning it's not a good idea for C++ too)