• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about primative return types  RSS feed

 
Barbara Flink
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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?
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barbara : Welcome to JavaRanch.
The K&B book also explains this very well - just read further . . .

Jim ... ...
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks, guys!!!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!