• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Question on byte

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for (byte bv = 0; bv < (byte)255; bv++) {
System.out.println(bv);
}

When I execute this block of within the main(), it is not printing anything. Please let me know the reason for it.

I thought it will narrow the value and print from 1 to 127.

Thanks
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The loop never enters because the condition 0 < (byte)255 is not met.
Try this:
System.out.println((byte)255);
and you will see that it is always less than zero (the initial value).

Here's something related:
http://qa.jtiger.org/GetQAndA.action?qids=74
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
255 is outside the range of byte. The int representation of 255 is:

11111111

When cast to a byte you end up with -1 so your loop is
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just wanted to know

how can we know that 11111111 is equlant to -1 ..how do u calculate..?

and will bv<125 give error ? i think now because..it retuns boolen value...
and comparsion b/w byte and int is ablsolutely fine ...i m right ?
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit wrote:

"how can we know that 11111111 is equlant to -1 ..how do u calculate..?"

For bytes, shorts, ints, and longs, negative numbers are represented using 2's-complement notation. To take the negative of a number, invert all the bits and then add 1. (Don't worry if there's overflow.) So to take the negative of 1, start with

00000001

Then invert all bits:

11111110

And add 1:

11111111

Note that if you invert all these bits and add 1, you've taken the negative of -1, and you're back at +1. There's an animated illustration that lets you play with this mechanism in Chapter 2 of "Ground-Up Java".

And you're right that bv<125 is ok.
 
amit taneja
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so u have written the book java groundup..
and thats y naming it...
any way...
thanx and goodluck for ur book

is there is some pdf version available or any part of it u give for publicity ...so we can atleast get some info about book and to review the book
regards,
[ June 05, 2005: Message edited by: amit taneja ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic