programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Casting numeric girid

Duc Ta
Ranch Hand
Posts: 46
2

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
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
• 1
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
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
• 1
Duc Ta wrote:Sorry, the maximum of byte is 128.

No, the maximum value for the type byte is 127.