Hey all,
the following codedoes not give the desired resultCode:public static void main(String[] args) {
System.out.println(50 - 050);
}
but givesCode:0
instead.Code:10
Any ideas?
Printable View
Hey all,
the following codedoes not give the desired resultCode:public static void main(String[] args) {
System.out.println(50 - 050);
}
but givesCode:0
instead.Code:10
Any ideas?
050 is the octal representation of the number 5*8+0*1 == 40. That explains the result.
kind regards,
Jos
you beat me to it!
Oh thanks,
This explains a lot :)
Strange that Java uses a leading 0 instead of an o to express octals. This leads in my opnion to the wrong result because leading 0's in decimal numbers should just be discarded.
There's nothing strange about it. Every language needs a way of easily defining octal integers, and this is how it is done with Java. Note that this is for integer constants, something you the programmer have complete control over, so if you understand Java, and code carefully, you should be fine. Also note that where user input is concerned and you parse the integer via Integer.parseInt, a leading 0 will in fact be discarded. Again, you the programmer has complete control over this, and in fact you can parse the number as an octal by simply changing the parser's radix.
Corrections to the above most welcome.
Also, I want to wish a hearty welcome to Jos, a well-known and highly regarded contributer in the Sun Java forums.
It's a C thingy which Java adopted; the reason was that no integral number has leading zeros so a zero introduces an octal number; the 0x or 0X notation is an extension to that lexical rule. They didn't use the 0b or 0B notation for binary numbers ... go and complain at Gosling, Kernighan and Ritchie for that ;-)
kind regards,
Jos
I saw that joined date, compared it with the number of posts made and failed to make sense out of anything as well.
Don't make fun of my name.
I'm respected here, see.
Well for hexadecimal the x must be there so it is different.
I merely stated some of my special gifts.
When I asked someone to hit their head on a desk I didn't mean to hit it there hard enough so it would crack or anything.
r035198x(<---- Good boy.
Tsk, tsk, making fun of innocent stupid newbies; they should be treated with respect and we should do their homework so they can party during the weekends and get a good grade without studying or working for it. Shame on you ;-)
kind regards,
Jos
ps. I removed your links in my reply because this newbie is not allowed to post any links yet ;-)
A few, pbrockway, Darryl, masijade, camickr.
No conspiracy I know of.
pbrocks away too, DB, camickr ...
Edit: Finally Phil posts earlier than me!