• 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Guess what this prints:Can someone explain this...

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

It prints'the value of tiny is" -126
Can someone explain why??
[ Jess added UBB [code] tags to preserve whitespace, check 'em out! ]
[ February 08, 2003: Message edited by: Jessica Sant ]
 
Hemal Mehta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also I modified the code and tried to print this:

When I try to print the value of character it is not printing anything...
[ Jess added UBB [code] tags to preserve whitespace, check 'em out! ]
[ February 08, 2003: Message edited by: Jessica Sant ]
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A byte can hold values of -128 to 127. So when you try to set the value of tiny to 130 -- instead of giving you an overflow error like some other languages, the value actually wraps around to the negative side and you end up with -126.
When I try to print the value of character it is not printing anything...
So, you're trying to print the character whose value is 32.... well --- if you look up the character 32 in the ASCII table you'll see that 32 is a space!! so -- when you print it -- it looks like nothing -- but its really there.
[ February 08, 2003: Message edited by: Jessica Sant ]
 
Hemal Mehta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for answer. I understood the answer for 130. Now I did the following I changed the value to 650 and now
it prints -118.
I cannot quite understand how this logic is working.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
130 is beyond the range of byte values (max is 127), casting such value to byte will give such result.
130 = 0000 0000 0000 0000 0000 0000 (1000 0010)
after casting to byte, take right most 8 bits:
1000 0010 = -126

value of char (32 = 0x20) is the space character.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic