• 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

Return Type of an Expression

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
During the arithemetic expression if i have both int and float what will be the resulting type

i know for the expression that involved byte char int short all will return the integer type.


what about the long float and double.
calrify my doubt
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the features of automatic promotion, the expression will return the type of the widest type among the operands, and the type will be at least int.

For example, int + short returns int, int + long returns long, int + double returns double.

Nick
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Parameswaran

I recommend you learn more about Unary and Binary numeric promotions. Nicholas is right but there is a big big catch here:

If both the operands are smaller than an int, the resulting type will be an int!!

For example:

short s = 5;
short s2 = 5;

short s3 = s + s2; ///will not compile, needs casting to short!

In short, operands smaller than an int will return atleast an int, and bigger than an int will return biggest of both the operands.

regards
Saurabh
reply
    Bookmark Topic Watch Topic
  • New Topic