• Post Reply Bookmark Topic Watch Topic
  • New Topic

program output  RSS feed

 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
i executed this program:

class exercise1b
{
public static void main(String[] args)
{
int x=1;
while(x<10)
{
x=x-1;
if(x>3)
{
System.out.print("big x");
}
}
}
}



The output was supposed to be nothing but big x came.
anyone knows reason???
thanks
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Try this and you'll see why.
Class names should begin with an upper case letter.
Please indent your code and use code tags when posting.
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this and you'll see why.


Correct me if I'm wrong here: So 'x' gets decremented to its least possible value and then on the next iteration when it is decremented again it becomes the largest possible value that 'x' can hold.
Is that correct?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote:
Try this and you'll see why.


Correct me if I'm wrong here: So 'x' gets decremented to its least possible value and then on the next iteration when it is decremented again it becomes the largest possible value that 'x' can hold.
Is that correct?

Yes it wraps around from smallest to largest when decrementing and from largest to smallest if incrementing.
 
Raj Gurung
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what it means is:
for example if x holds the least possible value as -12020203. then in the next iteration it becomes 12020203.
is that correct???
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj Gurung wrote:so what it means is:
for example if x holds the least possible value as -12020203. then in the next iteration it becomes 12020203.
is that correct???

Yes (except the min possible value of an int is -2147483648 and the max is 2147483647.)


 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj Gurung wrote:so what it means is:
for example if x holds the least possible value as -12020203. then in the next iteration it becomes 12020203.
is that correct???

Try
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Yes (except the min possible value of an int is -2147483648 and the max is 2147483647.)

Erm...so that would be "no" wouldn't it?

@Ray: The reason why it "wraps around" is that:
1. A Java int is NOT unlimited (specifically, it contains 32 bits).
2. It's signed (ie, the first bit contains the "sign" of the number it holds). For more information, read up on 2's complement.
3. Integer arithmetic in computing (and not just for Java) does NOT require that an operation give an error for anything except division by 0; so "overflow" and "underflow" (exceeding the maximum negative value) are silent. It's not ideal, but I suspect it's better than the alternatives.

But specifically, there is no reason why ANY integer would "cycle" at -12020203 unless you told it to.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!