This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes when return is required Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "when return is required " Watch "when return is required " New topic
Author

when return is required

narendra bhattacharya
Ranch Hand

Joined: Feb 17, 2010
Posts: 66
i am confused where returned is required and where it is not...........

lets see a code..

[/code]



can anyone help me in this....


SCJP1.6,SCWCD1.5
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18497
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
narendra bhattacharya
Ranch Hand

Joined: Feb 17, 2010
Posts: 66
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
Sheriff

Joined: Sep 28, 2004
Posts: 18497
    
  40

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

Joined: Feb 17, 2010
Posts: 66
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
Sheriff

Joined: Sep 28, 2004
Posts: 18497
    
  40

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

Joined: Dec 16, 2009
Posts: 39
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 ...


SCJP 6.0
Tejas Morabia
Greenhorn

Joined: Mar 06, 2010
Posts: 7
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

Joined: Jan 14, 2010
Posts: 317

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

Joined: Feb 17, 2010
Posts: 66
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: when return is required
 
Similar Threads
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
Converting an decimal int to a hex without using Integer.toHexString
about statement unreachable
counter is not shown in JTextField
Project Euler problem 10