This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why is it printing..." Watch "why is it printing..." New topic
Author

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 body..it merely keeps incrementing and breaks off the loop when the condition isn't true and goes to the next line..in 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: http://aspose.com/file-tools
 
subject: why is it printing...
 
Similar Threads
java.lang.IllegalArgumentException: im == null!
Doubt with ++ operator
overflow and underflow
Assignments
complement in byte if range exceeds