• Post Reply Bookmark Topic Watch Topic
  • New Topic

Returned Value not accepted  RSS feed

 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have this conceptual doubt that what happens if i return a value from a function but do not receive it in a variable?
Where does the returned value go?
Thanks....
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhanshu Mishra wrote:Hi all,
I have this conceptual doubt that what happens if i return a value from a function but do not receive it in a variable?
Where does the returned value go?
Thanks....


It's ignored, and no harm is done.

Say you have a method like this:


When you call


a spot is allocated on the stack to store a reference value (the method's return value). The value of a reference pointing to the "foo" String object is placed there. This happens regardless of whether the caller assigns the result to a variable of not.

Now, if you do


then the result is not only placed in that spot on the stack, but after that it is copied from that spot on the stack into the variable "s". That might be another spot on the stack (if "s" is a local variable) or on the heap (if "s" is a member variable).

So, no matter what, a place is created to store the method result. Then, that result either is or is not copied to a variable.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:So, no matter what, a place is created to store the method result. Then, that result either is or is not copied to a variable.

Do you know what? I had no idea what the actual mechanism was until you just explained it (cheers, BTW). I suppose I could have found out if I'd really wanted to, but one of the liberating things for me about Java is that I don't have to know.

@Sudhanshu: This is not a criticism of your post in any way. I came to Java via C and C++ and several procedural languages before them, and I am continually amazed at how clear the ethos of Java is. There is so much you can do without needing to know exactly how all the nuts and bolts work (and believe me, I've taken a few guesses ).

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Jeff Verdegan wrote:So, no matter what, a place is created to store the method result. Then, that result either is or is not copied to a variable.

Do you know what? I had no idea what the actual mechanism was until you just explained it (cheers, BTW).


Well, to be honest, there's a certain element of guesswork there. The return value definitely goes on the stack, and it's definitely okay to ignore it (as I'm sure you know). As for the rest, well, it could be that A) The return spot is only created if the value is actually used and B) The return value is placed directly where it's gonna end up, rather than the final copy step (at least in the case of local usage, as opposed to being copied to a member variable), but that seems unlikely, and would probably complicate the compiler and JVM. I think, though, that A and/or B come into play for tail recursion optimization, and the fact that Java doesn't do this also contributes to why I'm fairly confident this is how it's done.

@Sudhanshu: Don't worry about the details. I may have not gotten them quite right, but they were just there to give you an idea how things can work. The main point is: You can safely ignore the return value, and nothing bad will happen.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!