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 Regarding Function Call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Function Call" Watch "Regarding Function Call" New topic
Author

Regarding Function Call

babu sharath
Ranch Hand

Joined: Jul 06, 2006
Posts: 42
Check below code :

public class Hello
{
public static void main(String args[])
{ new Hello().doit(20);
}
static int doit(long t) {
System.out.println(t);
return t;
}
}

Above we are calling "doit" which is returning int , but in function call we are not receiving anywhere ( like int a = new Hello().doit(20); )
Above is compiles and runs fine .
Is it not required to receive the return value ??

Regards,
Sharath.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by babu sharath:
...Is it not required to receive the return value ?? ...

No, it's not required. As explained by Bruce Eckel in Thinking in Java...
...you can also call a method and ignore the return value. This is often referred to as calling a method for its side effect, since you don�t care about the return value, but instead want the other effects of the method call.


"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
Murali Mohan
Ranch Hand

Joined: Jan 09, 2006
Posts: 66


As far my knowledge the above class will not compile.
You can't return a long here. You should return an int.


Thanks,<br />Murali...
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
Where is he returning a long.

regards,
vijay.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Murali Mohan:

As far my knowledge the above class will not compile.
You can't return a long here. You should return an int.


Yes, good catch! It should be "return (int)t;" to avoid the loss of precision compiler error.
[ July 19, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Chaitanya Vivek
Ranch Hand

Joined: May 17, 2006
Posts: 36
This code will not compile not because we are not collecting return value but because its actually returning a long instead of int

thanks
chaitanya
Ajit Amitav Das
Ranch Hand

Joined: Dec 14, 2005
Posts: 49
yea to my knowledge it won't compile as we are returning a long value in return statement and in method it's return type is int , we can downcast the value but if the expression which is returning is constant or evaluated in compile time or the value we are returning fits into the type of returntype in method defination. For more info in this go tojavaranch journal.


Warm Regards<br />Ajit Amitav Das<br />SCJP 1.5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Function Call