This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of DevSecOps Adventures: A Game-Changing Approach with Chocolate, LEGO, and Coaching Games and have Dana Pylayeva on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

return value for method.

 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why this one

compile and there is no error (because there is no return value)?
 
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is an interesting question. for(;;) { } is infinite loop and the statement is unreachable which is written after this. Maybe java know it and doesn't require return statement. I think this loop throws StackOverflowError when we run code. But it doesn't happen
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sergej Smoljanov wrote:Why this onecompile and there is no error (because there is no return value)?


Because the compiler knows the loop will run forever and thus adding a return statement would result in an unreachable code compiler error

Time for another pop quiz: which of the following methods will compile and which won't?


Hope it helps!
Kind regards,
Roel
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, if you throw an exception from the method, it compiles without a return statement.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushfiq Mammadov wrote:I think this loop throws StackOverflowError when we run code. But it doesn't happen


That's indeed not true! Not every endless loop will result in a StackOverflowError being thrown. A stack overflow occurs because an application recurses too deeply (meaning, too many methods on the call stack). But in this case the loop only prints something to the console. So on the call stack you only have 2 methods: main() and test(). Then you enter the loop and a 3rd method is added println(). When this method is finished, it's removed from the stack. On the next iteration, the println() method is added again and when finished, also removed again. On the next iteration, again the same story. So at most you'll have 3 methods on the call stack which is not enough to have an overflow.

You can very easily adjust the code a little bit to have a stack overflow (by adding just 1 line of code). So let's see if you can figure out which change will result in a StackOverflowError being thrown.
 
Mushfiq Mammadov
Ranch Hand
Posts: 221
27
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote: That's indeed not true! Not every endless loop will result in a StackOverflowError being thrown. A stack overflow occurs because an application recurses too deeply (meaning, too many methods on the call stack). But in this case the loop only prints something to the console. So on the call stack you only have 2 methods: main() and test(). Then you enter the loop and a 3rd method is added println(). When this method is finished, it's removed from the stack. On the next iteration, the println() method is added again and when finished, also removed again. On the next iteration, again the same story. So at most you'll have 3 methods on the call stack which is not enough to have an overflow.

You can very easily adjust the code a little bit to have a stack overflow (by adding just 1 line of code). So let's see if you can figure out which change will result in a StackOverflowError being thrown.



Thanks a lot, Roel, I confuse it with recursion. The following code throw StackOverflowError


Roel De Nijs wrote: Time for another pop quiz: which of the following methods will compile and which won't?


Thanks for your questions too, they were useful. It is interesting that there are two compile error in test4() method.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushfiq Mammadov wrote:The following code throw StackOverflowError


Absolutely! You could move the invocation of test() inside the loop as well. But not after the loop, because then you'll get an unreachable code compiler error
 
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic