• 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

Question about primative return types

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Chapter 2 on Page 128 and 159 of the K&B book...
It says

In a method with a primiatve return type, you can return any value or variable that can be implicitly converted to the declared return type


The book gives an example that "char" and "int" are compatible.

My question is ...
What other primitives are compatible with which primitives?
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barbara : Welcome to JavaRanch.
The K&B book also explains this very well - just read further . . .

Jim ... ...
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Implicitly means without any explicit cast, right?

So if you dont' loose precision you can make the conversion:

For example:
if your method returns a double you can return:

byte,short,char,int,float,long and double

because all of those primitive types can be widened to double, i.e. you can assign all those primitive types to double without an explicit cast.

You can find further informations at JLS, widening primitive conversion paragraph
JLS 5.1.2
 
Barbara Flink
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, guys!!!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic