Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Best way to return int through an arg (3 choices)

Reply
Thread Tools

Best way to return int through an arg (3 choices)

 
 
Marc Rochkind
Guest
Posts: n/a
 
      06-30-2003
To return an int (for example) through an argument, one can use a class
with an integer field. Three ways to do it come to mind:

1. Define a class named, say, IntHolder, with a public field:

public class IntHolder {
public int value;
}

2. Similar to #2, but make the field private and provide get and set
methods.

3. Instead of defining your own class, use the existing class
org.omg.CORBA.IntHolder.

Normally, one wouldn't code a class when a suitable one already exists, but
bringing in anything from org.omg.CORBA only for its holder classes seems
silly to me. It might confuse the reader more than including an extra class
would.

Similarly, normally one makes fields private and provides access functions,
but perhaps holder classes for primitive types is an exception. When the
only purpose of the class is to hold a primitive-type field, get and set
seem like overkill.

So, I am inclined to use #1 above.

Opinions?

--Marc
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      06-30-2003
Marc Rochkind <(E-Mail Removed)> scribbled the following:
> To return an int (for example) through an argument, one can use a class
> with an integer field. Three ways to do it come to mind:


> 1. Define a class named, say, IntHolder, with a public field:


> public class IntHolder {
> public int value;
> }


> 2. Similar to #2, but make the field private and provide get and set
> methods.


> 3. Instead of defining your own class, use the existing class
> org.omg.CORBA.IntHolder.


> Normally, one wouldn't code a class when a suitable one already exists, but
> bringing in anything from org.omg.CORBA only for its holder classes seems
> silly to me. It might confuse the reader more than including an extra class
> would.


> Similarly, normally one makes fields private and provides access functions,
> but perhaps holder classes for primitive types is an exception. When the
> only purpose of the class is to hold a primitive-type field, get and set
> seem like overkill.


> So, I am inclined to use #1 above.


> Opinions?


I would choose #2 instead if it were my code. I am opposed to public
fields in principle. And I agree with you that including CORBA just to
use one single class, which doesn't even do any object broking stuff,
is overkill.
Why do you have to use an int holder class anyway? What's wrong with
java.lang.Integer? Sure it's immutable, but can't you just construct a
new instance every time you need to change the value?

--
/-- Joona Palaste ((E-Mail Removed)) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Hasta la Vista, Abie!"
- Bart Simpson
 
Reply With Quote
 
 
 
 
Marc Rochkind
Guest
Posts: n/a
 
      06-30-2003
On 30 Jun 2003 20:26:06 GMT, Joona I Palaste <(E-Mail Removed)>
wrote:


[snip]

> Why do you have to use an int holder class anyway? What's wrong with
> java.lang.Integer? Sure it's immutable, but can't you just construct a
> new instance every time you need to change the value?
>


The issue is the type of the argument that will be used to return the
value, as in this example:

void m(X A) { ... code to change value of A ... }

where I want to return a result through the argument A.

I can't see how X can be Integer.

--Marc
 
Reply With Quote
 
Miguel De Anda
Guest
Posts: n/a
 
      06-30-2003

"Marc Rochkind" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> To return an int (for example) through an argument, one can use a class
> with an integer field. Three ways to do it come to mind:
>
> 1. Define a class named, say, IntHolder, with a public field:
>
> public class IntHolder {
> public int value;
> }
>
> 2. Similar to #2, but make the field private and provide get and set
> methods.
>
> 3. Instead of defining your own class, use the existing class
> org.omg.CORBA.IntHolder.
>
> Normally, one wouldn't code a class when a suitable one already exists,

but
> bringing in anything from org.omg.CORBA only for its holder classes seems
> silly to me. It might confuse the reader more than including an extra

class
> would.
>
> Similarly, normally one makes fields private and provides access

functions,
> but perhaps holder classes for primitive types is an exception. When the
> only purpose of the class is to hold a primitive-type field, get and set
> seem like overkill.
>
> So, I am inclined to use #1 above.
>
> Opinions?
>
> --Marc


Wow, all this trouble just to make a swap function...


 
Reply With Quote
 
Michiel Konstapel
Guest
Posts: n/a
 
      06-30-2003
> To return an int (for example) through an argument, one can use a
class
> with an integer field. Three ways to do it come to mind:

<snip>

4. Functions have a return value, so use it.

Seriously, why do you need to do it like this?
Michiel


 
Reply With Quote
 
Miguel De Anda
Guest
Posts: n/a
 
      06-30-2003

"Michiel Konstapel" <(E-Mail Removed)> wrote in message
news:iF2Ma.1511$w3.387301@zonnet-reader-1...
> > To return an int (for example) through an argument, one can use a

> class
> > with an integer field. Three ways to do it come to mind:

> <snip>
>
> 4. Functions have a return value, so use it.
>
> Seriously, why do you need to do it like this?
> Michiel
>
>


Oh, I think you can use an array of length 1.

int[] a = new int[1];
a[0] = 10;

void foo(int[] b) {
b[0] *= 2;
}

Something like that might work.


 
Reply With Quote
 
Adam Maass
Guest
Posts: n/a
 
      07-01-2003

"Marc Rochkind" <(E-Mail Removed)> wrote:
> To return an int (for example) through an argument, one can use a class
> with an integer field. Three ways to do it come to mind:
>
> 1. Define a class named, say, IntHolder, with a public field:
>
> public class IntHolder {
> public int value;
> }
>
> 2. Similar to #2, but make the field private and provide get and set
> methods.
>
> 3. Instead of defining your own class, use the existing class
> org.omg.CORBA.IntHolder.
>
> Normally, one wouldn't code a class when a suitable one already exists,

but
> bringing in anything from org.omg.CORBA only for its holder classes seems
> silly to me. It might confuse the reader more than including an extra

class
> would.
>
> Similarly, normally one makes fields private and provides access

functions,
> but perhaps holder classes for primitive types is an exception. When the
> only purpose of the class is to hold a primitive-type field, get and set
> seem like overkill.
>
> So, I am inclined to use #1 above.
>
> Opinions?



Use the function's return value.


Oh, you say it's a function that is *supposed* to alter the state of the
object it is passed. So write a class that encapsulates the state to be
passed, and treat it just like any other class. That is, it will probably
have private data and public accessors. Unless it's a conceptually a struct,
and doesn't have any methods of its own. (These are ugly but sometimes
useful.)


-- Adam Maass


 
Reply With Quote
 
xarax
Guest
Posts: n/a
 
      07-01-2003
Marc Rochkind <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> To return an int (for example) through an argument, one can use a class
> with an integer field. Three ways to do it come to mind:
>
> 1. Define a class named, say, IntHolder, with a public field:
>
> public class IntHolder {
> public int value;
> }


Use a nested private static class when all you need is
to pass around a simple-minded container object within
your main class. Use a package-scoped container class
when you need to pass around the container within a
single package.

> 2. Similar to #2, but make the field private and provide get and set
> methods.


This is good for passing simple-minded container classes
between different packages.

> 3. Instead of defining your own class, use the existing class
> org.omg.CORBA.IntHolder.
>
> Normally, one wouldn't code a class when a suitable one already exists, but
> bringing in anything from org.omg.CORBA only for its holder classes seems
> silly to me. It might confuse the reader more than including an extra class
> would.


Don't even think about doing this.

> Similarly, normally one makes fields private and provides access functions,
> but perhaps holder classes for primitive types is an exception. When the
> only purpose of the class is to hold a primitive-type field, get and set
> seem like overkill.


Only use accessor/modifier methods when the container is
used across packages.

However, having said that, there are standard Java container
classes that have simple public fields, like Dimension and
Rectangle.

You could define your own "util" package and put some simple
container classes there for use by all of your packages,
similar in purpose to Dimension and Rectangle.

> So, I am inclined to use #1 above.


Just look at the scope of how your container will be used
within a single class, a single package, or across packages.

Such container classes are very useful when you must
return several values from a single method call and also
help with type-safety.
 
Reply With Quote
 
Dale King
Guest
Posts: n/a
 
      07-01-2003
"Marc Rochkind" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> To return an int (for example) through an argument, one can use a class
> with an integer field. Three ways to do it come to mind:



The best way is to not do it at all. The very fact that you are
contemplating it tells me that there is something seriously wrong with your
design. The solution is to fix the design not finding the least hackish way
to return values through an argument.

There are at least a dozen different ways to fix the design, but without
more information about what your problem really looks like it is not
possible to advise you on the best way to go.

--
Dale King


 
Reply With Quote
 
Marc Rochkind
Guest
Posts: n/a
 
      07-01-2003
On Tue, 1 Jul 2003 10:25:38 -0500, Dale King <Dale[dot](E-Mail Removed)>
wrote:

> "Marc Rochkind" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
>> To return an int (for example) through an argument, one can use a class
>> with an integer field. Three ways to do it come to mind:

>
>
> The best way is to not do it at all. The very fact that you are
> contemplating it tells me that there is something seriously wrong with
> your
> design. The solution is to fix the design not finding the least hackish
> way
> to return values through an argument.
>
> There are at least a dozen different ways to fix the design, but without
> more information about what your problem really looks like it is not
> possible to advise you on the best way to go.
>
> --
> Dale King
>
>
>


Hmmm... I think your first and last sentences are contradictory, but I
think I know what you meant...

But, since you asked...

I'm writing a Java interface to POSIX/SUS, and my goal is to be as faithful
to the C binding as possible, as this is intended primarily as an
educational tool.

There are POSIX/SUS functions like this:

pid_t waitpid(pid_t pid, int *stat_loc, int options);

One could, of course, define a class called, say WaitResult, that included
fields for both the process ID and the status, and perhaps that would be a
good idea if the goal were to design the best interface from the Java
perspective. But, if the goal is to teach waitpid and how it works, then
it's better to return the pid_t as the value of the method and to return
the status through the 2nd argument. Also, this allows the 2nd argument to
be null, which indicates that the status isn't wanted.

I expect that in the end the Java binding will be widely used from Jython,
as that will provide an interactive way to exercise the POSIX/SUS system
calls. An excellent educational tool, in my opinion.

I will hae reached my goal if one can make the POSIX/SUS calls from
Java/Jython by reading only the SUS, without referring to any separate
documentation (e.g., JavaDocs) for the Java/Jython binding at all.

--Marc



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass a multiline arg to exec('some.exe arg')? n00m Python 5 05-05-2008 02:58 PM
Trouble with setTimeout(arg, arg) nat.hourt@gmail.com Javascript 7 11-12-2005 05:13 PM
Best way to return an int as a std::string brightoceanlight@hotmail.com C++ 8 08-09-2005 04:37 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments