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 Beginning Java and the fly likes weird return 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 » Java » Beginning Java
Bookmark "weird return" Watch "weird return" New topic
Author

weird return

amr talaat
Greenhorn

Joined: Sep 02, 2012
Posts: 19


found it in one of java books

i want to understand this return ;

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60740
    
  65

Nothing too odd about it. It merely causes the method to return if the conditions in the enclosing if statement are met. Because the method is declared void, the return has no value.

One does not need to put a return statement at the end of a void method, because one is implicit.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

And just to add a bit more detail to what Bear is saying, it's important to understand that return has two roles:

1) To cease execution of the current method or constructor and trasfer control back to the caller. It always does this.

2) To specify the result value of the method. It only does this for methods that have non-void return types.
amr talaat
Greenhorn

Joined: Sep 02, 2012
Posts: 19
Jeff Verdegan wrote:And just to add a bit more detail to what Bear is saying, it's important to understand that return has two roles:

1) To cease execution of the current method or constructor and trasfer control back to the caller. It always does this.

2) To specify the result value of the method. It only does this for methods that have non-void return types.


from what you said i understand that if the condition is met it wont excute line 24 which is items.add(item) ;

why didnt he use if - else instead of return ; ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

amr talaat wrote:why didnt he use if - else instead of return ; ?


Because after if-else, the next iteration of the for-loop will run. That's different than returning, wouldn't you agree?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
My, that is crappy code! Which book did you find it in? You should always acknowledge your sources.

That shows a linear search through a List for a code, and adding it if the code has not been found before. That suggests they are using the wrong data structure. That code‑item pairing suggests to me that they should be using a Map<Code, LineItem>.
amr talaat
Greenhorn

Joined: Sep 02, 2012
Posts: 19
Paul Clapham wrote:
amr talaat wrote:why didnt he use if - else instead of return ; ?


Because after if-else, the next iteration of the for-loop will run. That's different than returning, wouldn't you agree?


ok got it
thanks
amr talaat
Greenhorn

Joined: Sep 02, 2012
Posts: 19
Campbell Ritchie wrote:My, that is crappy code! Which book did you find it in? You should always acknowledge your sources.

That shows a linear search through a List for a code, and adding it if the code has not been found before. That suggests they are using the wrong data structure. That code‑item pairing suggests to me that they should be using a Map<Code, LineItem>.


the source is Murach's Java Servlets and JSP, 2nd Edition

http://www.amazon.com/Murachs-Java-Servlets-JSP-Edition/dp/1890774448/ref=sr_1_2?ie=UTF8&qid=1367358224&sr=8-2&keywords=jsp+servlet

its the best selling jsp , servlet book
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Murach usually manage a lot better than that. Did they explain why they were using that strange search techniqu?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: weird return
 
Similar Threads
Hashtable
Why doesn't exercise 8-1 from Murach's Java Servlets work for me on IE7?
Hashtable method arguments
Add Update problem in Hashtable
Reiterating tags