• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting numeric girid  RSS feed

 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When I run the preceding code, the output is:
The byte is -126


I know that the maximum of byte is 256, but I do not know why the output is: -126 when I cast long literal 130L into a byte
Can anyone explain to me?
 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that the maximum of byte is 256, but I do not know why the output is: -126 when I cast long literal 130L into a byte
Can anyone explain to me?


Sorry, the maximum of byte is 128. I have confused the value between the maximum value of byte and short
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The integral cast operators just cut off the most significant bytes when converting from a wider data type. If the most significant bit of the result happens to be a 1, it will be a negative number.
 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:The integral cast operators just cut off the most significant bytes when converting from a wider data type. If the most significant bit of the result happens to be a 1, it will be a negative number.


I got it!
Thanks Mr Stephan
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Duc Ta wrote:Sorry, the maximum of byte is 128.

No, the maximum value for the type byte is 127.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!