Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Dan's Mock

 
Murtuza Akhtari
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Answers : d,e,f

I understand the d and the e...but why is it a compiler error at line 4
Also the explanation to the answer is as follows:

At line 2, the statement, "return i;", contains the expression, i. The enclosing method, m2, is declared void. The return statement generates a compile-time error, because it contains an expression. At line 3, the statement, "return;", does not contain an expression. The enclosing method, m3, is declared with the result type, int. The return statement generates a compile-time error, because it does not contain an expression that produces a value that is assignable to the declared result type.

Thanks
[ August 14, 2004: Message edited by: Murtuza Akhtari ]
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Murtuza Akhtari
The enclosing method, m3, is declared with the result type, int. The return statement generates a compile-time error, because it does not contain an expression that produces a value that is assignable to the declared result type.

I can explain more aove statement, m3 is declered as a method and it will return an integer after invoked, but please notice the method body, it does not produce a value { return;} that matches the return type of method static int m3(....).... So, compile-time error occured.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Murtuza,

At line 4, the print statement attempts to print the return values of the methods, but method m2 has a void return type.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic