File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help in method return type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help in method return type" Watch "Help in method return type" New topic
Author

Help in method return type

Erum Momin
Ranch Hand

Joined: Jun 10, 2009
Posts: 32
Hi All,
I'm having difficulty in understanding..... if a method is defined with some kind of return type other than void then is it necessary to assign the called method which has returned something to it's type specified in the method....
for eg.... take this method

class A{
public int aMethod(){ int i; i++; return i;}
}

should this be called like this....

A a = new A();
int j = a.aMethod();--------------------------->1

or like this
a.aMethod();------------------------------------>2

or you can call it any way you want???
According to me no.1 is correct but i've found in most of the mock tests saying that 2 works either..... you can call it any way you want... I have seen the questions where in they have called no.1 and no.2 together and answer is something different from compilation error.... Can anyone please explain it to me...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40


If a method has a return type, it is perfectly valid to ignore it. For example, say a method that does something and returns a status, shouldn't there be cases where you just want the something to happen (and at a best effort), and you don't care about the success status?

On the other hand, if the method doesn't return anything (void), then assigning it to something, or using it in an expression, should yield a compilation error.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Erum Momin
Ranch Hand

Joined: Jun 10, 2009
Posts: 32
Thanks Henry,
Actually i was confused since this does not happen in c or c++ and i was comparing it with that.... but now it's clear! ! !
Thank you.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

Actually i was confused since this does not happen in c or c++ and i was comparing it with that


What "does not happen in c or c++"?

Henry
Erum Momin
Ranch Hand

Joined: Jun 10, 2009
Posts: 32
Henry Wong wrote:
Actually i was confused since this does not happen in c or c++ and i was comparing it with that


What "does not happen in c or c++"?

Henry


i think ignoring the return type is not allowed in c or c++
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

Erum Momin wrote:
i think ignoring the return type is not allowed in c or c++


That's not true.... Heck, even printf() returns an int. It would be annoying to have to check the return type of calls to printf()...

Henry
Erum Momin
Ranch Hand

Joined: Jun 10, 2009
Posts: 32
hmmm.... you are rite....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help in method return type