• 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

Integral vs integer

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to clarify this concept guys, when we say Integral we mean int, short, byte and long, correct?
The reason why ask is that on page 55 of the OCA book, about numeric promotion, it says:

If one of the value is integral and the other is a floting-point, Java will automatically promote the integral value to the floating-point values's data type


Thing is, it's all nice and dandy if you promote a value from int to float, but what if you have a long? That surely can't be promoted to a floating-point...unless by floating-point we mean doube too...
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, I think I answered my own question...byte, short,int and long are integrals and float and double are floating-point. So all clear.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Attin wrote:
Thing is, it's all nice and dandy if you promote a value from int to float, but what if you have a long?



The range of a float variable is larger (and encompasses) that of the range of a long, so, Java should have no problem promoting a long variable to a float variable.  Obviously, there will be some loss of precision, but that is not a requirement for automatic promotion (implicit casting).

Henry
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Attin wrote:Right, I think I answered my own question...byte, short,int and long are integrals  

Yes but char also one of these integral types.

but what if you have a long? That surely can't be promoted to a floating-point...unless by floating-point we mean doube too...

long can be automatically promoted to float . Because range of float larger than long as Henry mentioned.
Example:Output:
Max value of long: 9223372036854775807
Max value of float: 3.4028235E38
Float range larger than long
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cool thanks guys.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganesh Patekar wrote:
Max value of long: 9223372036854775807
Max value of float: 3.4028235E38
Float range larger than long



Apologies. And you already know this, but just being really pedantic...

The range is more than just the max value. For example, the max value of a char (unsigned 16 bit) value is larger than the max value of a short (signed 16 bit) value, but implicit casting from short to char is not allowed because the char range does not encompass the range of the short.

Henry
 
Ganesh Patekar
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:but implicit casting from short to char is not allowed because the char range does not encompass the range of the short.  

Ohh yes, range should encompass rather than just greater than. Isn't It because char is unsigned which starts from 0 to 65,535 where char doen't have negative range but byte and short has positive as well as negative range?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ganesh Patekar wrote:Ohh yes, range should encompass rather than just greater than. Isn't It because char is unsigned which starts from 0 to 65,535 where char doen't have negative range but byte and short has positive as well as negative range?



Correct. While a char can't be implicitly cast to a byte or a short due to its max values; the other direction, a byte or a short can't be implicitly cast to a char due to its min values.

Henry
 
Ranch Hand
Posts: 234
12
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only a compile-time constant char (that is within range) can be implicitly cast to a byte or short
 
Ganesh Patekar
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry for noticing  and thank you Daniel for example  
 
reply
    Bookmark Topic Watch Topic
  • New Topic