This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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.
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!