It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why is it printing... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why is it printing..." Watch "why is it printing..." New topic

why is it printing...

Shubh Bhat
Ranch Hand

Joined: Sep 08, 2002
Posts: 32
"Welcome to Java" once. How come a byte if incremented becomes -128.
I am really not good at bits can you please show me the conversion? And could you please provide me some links to mock exams which deal with more on bits, ingeneral.

public class ByteI
public static void main (String [] args ) {
byte b = 1 ;
) ;
while ( ++b > 0 ) ;
System . out . println ( " Welcome to Java") ;

~-<br />Doing my best this time and everytime.<br />-- Me
Soum Sark
Ranch Hand

Joined: May 31, 2002
Posts: 50
that is because b gets incremented till it goes above 127 and since b++ is equivalent to b = (byte)b + 1 ; only the last 8 digits(bits) are taken so it becomes 1111 1111 which is a -ve no and so it prints 'welcome...' once.also not that there is a ";" after while so it does not have any merely keeps incrementing and breaks off the loop when the condition isn't true and goes to the next effect the print statement is not within the while loop
Shubh Bhat
Ranch Hand

Joined: Sep 08, 2002
Posts: 32
Oh my goodness, I didn't see that ; at all. Thanks for the reply.
I agree. Here's the link:
subject: why is it printing...
jQuery in Action, 3rd edition