Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Pls help me out! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Pls help me out!" Watch "Pls help me out!" New topic
Author

Pls help me out!

Hima Mangal
Ranch Hand

Joined: Feb 25, 2001
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


Hima<BR>Sun Certified Java Programmer
a hui
Ranch Hand

Joined: Oct 14, 2000
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

Joined: Feb 25, 2001
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

Joined: Jan 25, 2001
Posts: 25
Hi Hima,
As to your first doubt
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.
As for your second doubt,
I think you are assuming the
"System.out.println(00001010^00001000);" to be
binary numbers where in fact they are octal.
Hope that answers your question
Navin
Naveed Hassan
Greenhorn

Joined: Feb 23, 2001
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

Joined: Oct 14, 2000
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

Joined: Feb 25, 2001
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

Joined: Jan 25, 2001
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


The numbers are in Octal since they start with 0 .
If they started with 0x , they would be in hex.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pls help me out!