aspose file tools*
The moose likes Beginning Java and the fly likes Passing the method's return as argument Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing the method Watch "Passing the method New topic
Author

Passing the method's return as argument

Gjorgi Var
Ranch Hand

Joined: Feb 24, 2004
Posts: 85
Hola,
What would be the correct way to pass the return of one method as an argument to another method? Let's say we have a method dugh() that returns some value and a method foo() that takes the return of dugh() as an argument...
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
If I understand you correctly?

Gjorgi Var
Ranch Hand

Joined: Feb 24, 2004
Posts: 85
Any legal way to get the return of dugh() to be the argument of foo()...
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
I don't understand your question. That is legal.

e.g.

Gjorgi Var
Ranch Hand

Joined: Feb 24, 2004
Posts: 85
Originally posted by Horatio Westock:
I don't understand your question. That is legal.

e.g.

Sorry for the confusion caused...
Yes, that solved the problem, thank you for the point. On the other hand, my approach with method as an argument to another method may not be a good OOP...
[ March 27, 2005: Message edited by: Gjorgi Var ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Gjorgi Var:
...my approach with method as an argument to another method may not be a good OOP...

I think it's prefectly fine from an OOP standpoint. You just want to consider how easy the code is to read, and whether you want a "handle" on the return value(s).

int d = dugh(); //here, variable d can be used elsewhere
foo(d);

foo(dugh()); //here, return value remains anonymous
[ March 27, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
...my approach with method as an argument to another method may not be a good OOP...

To be clear, the method isn't an argument; the return value of the method is an argument. This may seem a trivial distinction, but there are languages (Smalltalk, Ruby, Groovy, and more) in which a method can be an argument. And in general these are considered to be more object-oriented than Java. The idea is that everything's an object, including a a method. And you can pass any object as a parameter. (Note that scripting languages like Ruby and Groovy tend not to have type safety, so you really can pass anything as an argument - even if it's something that makes no sense to pass as an argument.)


"I'm not back." - Bill Harding, Twister
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
well, if you want to split hairs and debate technicalities, you could say Jim's simplifying a little. type safety isn't quite the same as static typing; just because a language will let you pass any object, of any type, as an argument to a method, doesn't mean the language isn't keeping track of the types involved. typically, in modern-day languages that let you do this, you're in for some sort of type mismatch exception being thrown if you're not careful.

static typing, like Java has, is where you have to tell the compiler what types of objects a variable can take and what types the parameters and return values of functions can be; attempting to violate your own declarations about types usually generates a compile-time error in statically typed languages. the opposite is dynamic typing, which occurs in languages that figure out the types of variables, objects, parameters, and so on as they go along, and usually try to convert between types as needed whenever they can.

strong typing or type safety is when the language doesn't try to treat an object of one type as if it were of any other. if such a language is also statically typed, usually you have to explicitly cast or convert objects before you can treat them as something other than what they are; dynamically and strongly typed languages can sometimes attempt to do some of the simpler conversions themselves (like .toString() and conversions between int, float and double, say) but occasionally they'll force you to do some converting yourself, or catch a TypeError exception or something.

weakly typed languages are not often designed any longer. such languages, like C, will let you treat any variable or object as if it were any other thing without converting it first. in C, multiplying your birthdate with your mother's maiden name is straightforward and easy. some weakly typed languages got really bad - in very old versions of FORTRAN, you could redefine the values of literal constants; you could make the symbol "1" have the value "2"... yes, it really got as messy as you might imagine; that's why modern languages tend to be strongly typed.
Gjorgi Var
Ranch Hand

Joined: Feb 24, 2004
Posts: 85
Originally posted by Jim Yingst:

To be clear, the method isn't an argument; the return value of the method is an argument. [/QB]




That was exactly what I wanted; to take the return value of a method as an argument to another method...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing the method's return as argument