• Post Reply Bookmark Topic Watch Topic
  • New Topic

JQ+ Return Type Question  RSS feed

 
Zac Roberts
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here it is:

JQ+ says that:
"Class A will not compile because the return type is a byte but you are returning int." But... the code compiles fine on my JVM. Anyone?
 
Brian Lugo
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiles fine on mine too.
Valentin - I need your help - what section in JLS mentions conversion of int to byte?
Does it mention anything about return type?
Brian
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually 0 and 1 fit in a byte and that's why some compilers may have been optimized to allow this. If you change 0 (or 1) to 200 for instance, then you may get into trouble... try it...
 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the link to THIS SAME EXACT QUESTION ASKED AND ANSWERED YESTERDAY.
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014715
PLEASE do a search before you post questions. It saves you time too. You don't have to wait for some one to answer it all over again.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asignment conversion (JLS 5.2) is applied when:
assigning an expression to a variable.
comparing the constant after "case" with the expression within "switch(..here..)" (No reference conversions allowed in this case).
returning an expression from a method to the type returned by the method.
invoking a method: type of the parameters to the type of the arguments. (No implicit primitive narrowing conversion)
runtime checks for casting.
anymore?
maybe the type of the exception thrown to the type of the exception caugth.
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shivaji Marathe:
Here is the link to THIS SAME EXACT QUESTION ASKED AND ANSWERED YESTERDAY.
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=014715
PLEASE do a search before you post questions. It saves you time too. You don't have to wait for some one to answer it all over again.

Actually I looked at the replies to this question yesterday and still did not see a good answer. If this comes up on the test what is the correct answer? It looks like according to the JLS you can do this (which I always though you could if the return value is a constant that will fit), so JQ+ is wrong here, correct?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitive Answer:
(always comes from JLS):
s14.16 The return Statement.

A return statement with an Expression must be contained in a method declaration that is declared to return a value. (s8.4) or a compile-time error occurs. The Expression must denote a variable or value of some type T, or a compile-time error occurs. The type T must be assignable (s5.2) to the declared result type the method, or a compile-time error occurs.
s5.2 Assignment Conversion
Assignment conversion occurs when the value of an expression is assigned to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the user of an identity conversion, a widening primitive conversion, or a widening reference conversion. In addition, a narrowing primitive conversion may be used if all of the following conditions are satisfied:
* The expression is a constant expression of type byte, short, char, or int
* The type of the variable is byte, short, or char
* The value of the expression (which is known at compile time, because it is a constant expression) is representable in the type of the variable

So the JLS blesses this code and says it should compile just fine.
Hope that's definitive enough for you!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!