Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integral vs integer

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • 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: 232
2
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 22113
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cool thanks guys.
 
Henry Wong
author
Marshal
Pie
Posts: 22113
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 22113
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Daniel Cox
Ranch Hand
Posts: 147
7
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry for noticing   and thank you Daniel for example  
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic