Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return type

 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am wondering here, invocation of method m returns an int value but there is no int variable to hold that returned value, so why doesn't any compile error in such a case ?

int i=obj.m(); //looks fair

but,

obj.m(); //then what about returned value? compiler shouldn't complain about this ?
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you assign the return value to a variable and don't use it ..this feature actually gives you an option to use it or not worry about it.
 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
User does usage a variable or not that user will decide on need but when a method returning something explicitly then compiler should complain.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don’t explicitly say return ...; then you are implicitly not returning anything.
 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you don’t explicitly say return ...; then you are implicitly not returning anything.

If method has void return type then its invocation doesn't give anything so no worries but i am talking about when a method is explicitly returning something which i have declared to return then why doesn't compiler look that i have declared that its invocation will give some value so what about that value if no one there to hold ? where that value is going ? and then what was the meaning on such invocation ? a compile error can at least convey to programmer that you are not storing something useful data.
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shalini Srivastav wrote:
i am talking about when a method is explicitly returning something which i have declared to return then why doesn't compiler look that i have declared that its invocation will give some value so what about that value if no one there to hold ? where that value is going ? and then what was the meaning on such invocation ? a compile error can at least convey to programmer that you are not storing something useful data.


This is just a facility given to programmers that it is optional to return a value from a method which has a return type. I came up with the same problem when i was preparing J2ME for my University exam. In J2ME there is a method with the signature so here you can see this method is appending some thing and then return an int. But some times a programmer is just interested in appending and not what this method would return, so in that case the return value is optional, but if the programmer is interested he/she can also use that value.

So this method does 2 kinds of work-----you can use it as void return type by simply ignoring that return value + you can use it as a method which returns something .
 
Jo Joseph
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can use these types in chained implementations. I my opinion when we assign to a variable a new memory location is allocated and assigns the value to it, otherwise it will be pointed to original reference.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rameshwar Soni wrote: . . . This is just a facility given to programmers that it is optional to return a value from a method which has a return type. . . .
No, it isn’t. If a method says it returns a Foo, then it must always return exactly one Foo.

When you invoke the method, you are free to ignore its return type, which is probably what you meant.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jo Joseph wrote: . . . when we assign to a variable a new memory location is allocated and assigns the value to it, otherwise it will be pointed to original reference.
That doesn’t sound right.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well as Campbell said, we can choose to ignore or use the returned value from the invoked method.

A Code snippet



The boolean returned from the mkdirs method is often ignored by most programmers.
 
Jo Joseph
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Jo Joseph wrote: . . . when we assign to a variable a new memory location is allocated and assigns the value to it, otherwise it will be pointed to original reference.
That doesn’t sound right.
can you please give an idea how memory allocation happens in this case so that I can have a good idea. Thanks...
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yu don’t actually need to know how memory is allocated.
You have objects, which live usually on the heap, and each has a memory location.
You have memory reserved for variables, fields, etc, and re-assigning means moving the location of memory (as a number) into that memory reserved.
 
Jo Joseph
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Yu don’t actually need to know how memory is allocated.
You have objects, which live usually on the heap, and each has a memory location.
You have memory reserved for variables, fields, etc, and re-assigning means moving the location of memory (as a number) into that memory reserved.



Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic