aspose file tools*
The moose likes Java in General and the fly likes return type 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 » Java in General
Bookmark "return type" Watch "return type" New topic
Author

return type

Shalini Srivastav
Ranch Hand

Joined: Jul 21, 2012
Posts: 86

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

Joined: Dec 17, 2009
Posts: 133
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

Joined: Jul 21, 2012
Posts: 86
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

Joined: Oct 13, 2005
Posts: 39396
    
  28
If you don’t explicitly say return ...; then you are implicitly not returning anything.
Shalini Srivastav
Ranch Hand

Joined: Jul 21, 2012
Posts: 86
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

Joined: Feb 03, 2011
Posts: 247
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

Joined: Nov 28, 2010
Posts: 23
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

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: Jul 15, 2009
Posts: 537

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.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Jo Joseph
Greenhorn

Joined: Nov 28, 2010
Posts: 23
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

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: Nov 28, 2010
Posts: 23
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return type