• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

weird return

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


found it in one of java books

i want to understand this return ;

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ; ?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Murach usually manage a lot better than that. Did they explain why they were using that strange search techniqu?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic