• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is the retun Type?

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look at the following code and tell me what is the possible return code for this method?
public xxxx myMethod(Byte b, int i)
{ some code;
return (long)(expression);
}
Thanks.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
long, float, or double, unless I haven't had enough coffee yet and am missing something...
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like your coffee intake is at the optimum level.
------------------
Tom
Sun Certified Programmer for the Java� 2 Platform
Co-Moderator of the Programmer Certification Forums
 
Wasim Ahmed
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would you return float? I mean we are casting it to long and as I understand float is 32 bits and long is 64 bits.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Wasim
after having read this from the JLS:
"A return statement with an Expression must be contained in a method declaration that is declared to return a value (�8.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 (�5.2) to the declared result type of the method, or a compile-time error occurs.
"
I think the explanation could be:
Forget about the expression. We are assuming it compiles so now it has a long type. Consider xxxx would be float, for instance, when returnin it would be assigned to float, that is fine. What the compiler would complain about would be if xxx is int because an asignment conversion (JLS 5.2) wouldn't allow such conversion (long --> int)
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps this might help in explaining how a long value can be assigned to a float variable:
The ascii figure below depicts the widening conversions of primitives in Java. Note that intermediate datatypes can be skipped in the widening effort. For example, a short can be converted to a long without first converting to an int. Any widening conversion can occur without an explicit cast.
<pre>
byte --> short --> int --> long --> float --> double
^
| *this figure is inspired by Fig 3.1,
char------/ pg 44, Mughal & Rasmussen, "A
Programmer's Guide to Java(tm)
Certification", 2000.
</pre>
Java considers a conversion from a long to a float (or double) as a widening conversion. One may consider this unintuitive, since a long has a bit-depth of 64, and a float has a bit-depth of 32, as pointed out in a previous post. However, Java allows such a conversion, and the result is a possible loss of accuracy (inherent in floating-point numbers) in the resulting float.
On the other side of the coin, if a conversion is not a widening conversion, it is always a narrowing conversion (requiring explicit casts), which seems intuitive until the issue of converting chars to bytes and shorts (and vice-versa) comes to mind. Conversions between these three types are considered narrowing conversions, and, again, explicit casts are needed.
I hope this enlightens (and I hope this is 100% correct!).
[edited to correct ascii art!]
[This message has been edited by Ted Schrader (edited October 10, 2001).]
[This message has been edited by Ted Schrader (edited October 10, 2001).]
 
Wasim Ahmed
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks folks, I got it .
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic