Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!

# Pls help me out!

Hima Mangal
Ranch Hand
Posts: 82
hi all..
could u pls help me out with this one??
public class certify{

public static void main(String args[])
{
int i=10;int j=8;
i=i++;
System.out.println(i);
System.out.println(i^j);
System.out.println(00001010^00001000);
}
}

the output is..
10 (why not 11?)
2 (this is fine)
8 (why not 2 again??)

------------------
Hima

a hui
Ranch Hand
Posts: 39
Hi,
The reason you're getting 10 from i = i++; because = operator has the lowest precedence and it's RHS while postfix operators are LHS.
As for your second question I also find it strange, but it seems that windows environment (if you work on win) interprates the binary differently. I'm not sure that reason for that but the following code shows that what we'd calculate as 8 from binary, in fact it prints 512.
public class certify{
public static void main(String args[])
{
int i=10;int j=8;
System.out.println(00001010);// prints 520
System.out.println(00001000);// prints 512
System.out.println(00001010^00001000); // prints 8
System.out.println(00000010);// prints 8
}
}
print out:
520
512
8
8

Hima Mangal
Ranch Hand
Posts: 82
hi..
thanx for the reply.. but i still don't understand.. could u pls elaborate on the first point?? ..
thanx again..
------------------
Hima

Navin Narayan
Greenhorn
Posts: 25
Hi Hima,
int i = 10;
i = i++;
It is better if you assume it as
i++;
i=10;
What should happen is that i should return its value
and then increment it.
But what seems to happen always is that i value is incremented
in the first cycle and then in the second cycle, i is given the value it had before it incremented thus wasting the increment.
I think you are assuming the
"System.out.println(00001010^00001000);" to be
binary numbers where in fact they are octal.
Navin

Naveed Hassan
Greenhorn
Posts: 17
Hi
Regarding 3)
The operands are being accepted as Octal and with the help of conversion system we know
00001010 in Octal = 512 in decimal System
00001000 in Octal = 520 in decimal System

Now the 3) statment becomes
System.out.println(512^520);
giving you 8.
(I hope that u wont ask for: how 512^520=8???)

a hui
Ranch Hand
Posts: 39
I guess I'd like to know why these numbers are octal and not binary, and also how can a number be represented in binary in a program?

Thanks
AH

Hima Mangal
Ranch Hand
Posts: 82
hi everybody..
thanx for your detailed and precise explanations,.. and a special thanx for naveen.. ur answer cleared my doubts perfectly..
------------------
Hima

Navin Narayan
Greenhorn
Posts: 25
Hi hui,
Originally posted by a hui:
I guess I'd like to know why these numbers are octal and not binary, and also how can a number be represented in binary in a program?

Thanks
AH