Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

weird return

 
amr talaat
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


found it in one of java books

i want to understand this return ;

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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 ; ?
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Murach usually manage a lot better than that. Did they explain why they were using that strange search techniqu?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic