• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

when return is required

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am confused where returned is required and where it is not...........

lets see a code..

[/code]



can anyone help me in this....
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For void methods, explicit returns are not necessary -- as the compiler knows what to do, when you get to the last line of the method.

For other types, you must follow all possible paths through the method, and it must reach a return, or an exception with all paths. And the compiler isn't always very smart, as it is possible to fool it to think certain paths are possible.

Henry
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
For void methods, explicit returns are not necessary -- as the compiler knows what to do, when you get to the last line of the method.

For other types, you must follow all possible paths through the method, and it must reach a return, or an exception with all paths. And the compiler isn't always very smart, as it is possible to fool it to think certain paths are possible.

Henry





sir i get confused in returns ,......i expect that sir you can tell me an easy way in this.....
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

narendra bhattacharya wrote:
sir i get confused in returns ,......i expect that sir you can tell me an easy way in this.....



Can't elaborate on the answer, if you don't tell us what of the previous answer you are confused with.

Henry
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

narendra bhattacharya wrote:
sir i get confused in returns ,......i expect that sir you can tell me an easy way in this.....



Can't elaborate on the answer, if you don't tell us what of the previous answer you are confused with.

Henry




Sorry sir sorry for the trouble .. sir when i am writing like this ..
what is wrong with it...
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

narendra bhattacharya wrote:
Sorry sir sorry for the trouble .. sir when i am writing like this ..
what is wrong with it...




It's not valid Java. In fact, I am not even sure what you are trying to accomplish (trying to return). Maybe if you tell us, what did you expect to happen, we can give you a hint in the right direction.

Henry
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Narendra,

It's a simple way to understand is that
return has some rules..
return where to use...
first of all you don't need to use return with those method which have types void
you can type return it's legal with void because it's not have any return type.
void method1(){
return;
}

so void don't need to declare return
like..
void method2(){
}
both are fine because void don't expect to anything return.

so in your first example you have to see that method is required boolean ....
so you have to return boolean.

for further understanding must check out return types.

in your second example assert is giving compiler error because it's illegal to use non return value with assert due to void method
which has return but it is not returning anything ...
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi just to simplify the answer to first question and the problem with below line of code,

1) Return statement needs to be specified for each condition in the function, in this case for condition if(t==10) you are assigning b=true and not returning value which is incorrect.
2) return type cannot have assignment(like b = 10) or conditional statement(like if else statement) in it.
And its advisable to do most of the variable value manipulation before the return statement rather then inside return. this will make your life easier in debugging.
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is perfectly legal to use assignement with return statement how ever usage of if condition is not legal. As in case of "if" we are not providing any value to return statement which could be returned. But in case of assignement we are assigning a value to some variable which will be returened after assignment.
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

narendra bhattacharya wrote:
Sorry sir sorry for the trouble .. sir when i am writing like this ..
what is wrong with it...




It's not valid Java. In fact, I am not even sure what you are trying to accomplish (trying to return). Maybe if you tell us, what did you expect to happen, we can give you a hint in the right direction.

Henry



Is the concept is:

there is always a right to left flow whenever return is required .. like return a=1; (right to left)

also when we are using it in the assert it is going to be printed if condition test returns false along with the exception..and there toString() method is called ..and with void it can't be...


Henry Wong Sir is this right
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic