This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JQ+ Return Type Question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "JQ+ Return Type Question" Watch "JQ+ Return Type Question" New topic
Author

JQ+ Return Type Question

Zac Roberts
Ranch Hand

Joined: Jan 29, 2002
Posts: 82
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?


Zac Roberts<br />SCJP2
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
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

Joined: Aug 26, 2001
Posts: 7610
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...


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
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

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
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

Joined: Jan 07, 2002
Posts: 2205
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!!


Rob
SCJP 1.4
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: JQ+ Return Type Question
 
Similar Threads
JQ+ 1003521385874
override equals
byte to int??
Chew on this!
JQ+ Question ID :1003242858408