• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return value for method.

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why this one

compile and there is no error (because there is no return value)?
 
Mushfiq Mammadov
Ranch Hand
Posts: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
 
Paweł Baczyński
Bartender
Posts: 1877
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 4
  • Mark post as helpful
  • send pies
  • 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: 187
25
Java jQuery Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic