Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Regarding Function Call  RSS feed

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


As far my knowledge the above class will not compile.
You can't return a long here. you should return an int.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is he returning a long.

regards,
vijay.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!