Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

return() call  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[See UseCodeTags]

i think output is 7.
if(b) gives true so (i/7); becomes (343/7); which returns 49.

but after that return(i/49) => (343/49) ..does it have any significance ?

answer should be 49
 
author
Marshal
Posts: 23437
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

harshal deolekar wrote:answer should be 49


Perhaps I'm misunderstanding something, but the answer is 49, so what exactly is your problem/question ?
 
harshal deolekar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
answer is 49
but i am not able to understand how?

does the second return(i/49); come into picture? if it comes answer should be 7.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

harshal deolekar wrote:does the second return(i/49); come into picture? if it comes answer should be 7.


No. A method will stop executing and return to its caller as soon as it reaches a return statement.*
So your method returns the value of i/7 if b evaluates to true and the value of i/49 if b evaluates to false.





*Unless the return is in a try/finally block, in which case any code in the finally block will be run before the method returns.
 
harshal deolekar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Marshal
Posts: 58367
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not very good style code there. Have a look at better style here. If you write
return b ? i / 7 : i / 49;
it becomes clearer. As you have seen from the link, that is identical to what you wrote (or maybe i / 49 : i / 7 )
To be pedantic: it is not a return() call, but a return call. Remember return is a keyword, so you cannot use it as a method name, so the () is part of the expression, not attached to the return word. In fact the precedences are such that you can remove all the () from those return statements and get exactly the same results.
 
Author and ninkuma
Marshal
Posts: 66536
160
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!