Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when return is required

 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • 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....
 
Henry Wong
author
Marshal
Pie
Posts: 21195
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 21195
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 21195
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Afzal Rehman
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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 ...
 
Tejas Morabia
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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.
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic