assignment of int to Integer

S

snehapshinde

I came across following java snippet

int a=010;
Integer b=10;
System.out.println(b=a);

the output is 8

when i remove the leading 0 of variable 'a', the output changes to 10.
I am not getting how the assignment is being done.
I would be grateful if anybody can help me to understand it.
Thanks.
 
J

Joshua Cranmer

I came across following java snippet

int a=010;
Integer b=10;
System.out.println(b=a);

the output is 8

when i remove the leading 0 of variable 'a', the output changes to 10.
I am not getting how the assignment is being done.
I would be grateful if anybody can help me to understand it.
Thanks.

Welcome to the wonderful rarely-known feature of Java and most other
C-derived languages: octal notation. Any sequence of digits starting
with a `0' is treated as an octal number (including, I may note, 0
itself). So your `010' is the octal `10', i.e. 8.
 
S

snehapshinde

Welcome to the wonderful rarely-known feature of Java and most other
C-derived languages: octal notation. Any sequence of digits starting
with a `0' is treated as an octal number (including, I may note, 0
itself). So your `010' is the octal `10', i.e. 8.

Thanks for the information, Donald.
 
R

Roedy Green

when i remove the leading 0 of variable 'a', the output changes to 10.
I am not getting how the assignment is being done.
I would be grateful if anybody can help me to understand it.
Thanks.

lead 0 means octal constant.

See http://mindprod.com/jgloss/autoboxing.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

We are almost certainly going to miss our [global warming] deadline.
We cannot get the 10 lost years back, and by the time a new global agreement to
replace the Kyoto accord is negotiated and put into effect, there will probably
not be enough time left to stop the warming short of the point where we must not
go. ~ Gwynne Dyer
 
M

Mike Schilling

Joshua said:
Welcome to the wonderful rarely-known feature of Java and most other
C-derived languages: octal notation. Any sequence of digits starting
with a `0' is treated as an octal number (including, I may note, 0
itself).

What if I want a decimal 0?

(Actually, according to the JLS, an octal literal is "0" followed by one or
more octal digits, so "0" by itself is a normal integer literal. And yes,
this is of no importance whatsoever.)
 
J

Joshua Cranmer

Mike said:
(Actually, according to the JLS, an octal literal is "0" followed by one or
more octal digits, so "0" by itself is a normal integer literal. And yes,
this is of no importance whatsoever.)

Reading directly from the javac source code:
case '0':
scanChar();
if (ch == 'x' || ch == 'X') {
scanChar();
if (ch == '.') {
scanHexFractionAndSuffix(false);
} else if (digit(16) < 0) {
lexError("invalid.hex.number");
} else {
scanNumber(16);
}
} else {
putChar('0');
scanNumber(8);
}
return;
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
scanNumber(10);
return;

So, according to javac, the literal "0" is actually an octal literal.
Not that it really matters, since a "0" is 0 in whatever radix you want.
 
L

Lew

Mike said:
What if I want a decimal 0?

(Actually, according to the JLS, an octal literal is "0" followed by one or
more octal digits, so "0" by itself is a normal integer literal.  And yes,
this is of no importance whatsoever.)

Smart-aleck. :)

(N.b., "no importance whatsoever" == "no difference whatsoever".)

--
Lew
from Dilbert: "When I started out, we didn't have zeros. We had to
use Os."
"That's nothing, back in my day, we had to carve our own bits out of
wood."
not from Dilbert: They measured wooden memory in barqs, not bytes.
That was bad, because everyone knows the barq is worse than the byte.
 
M

Mike Schilling

Joshua said:
Reading directly from the javac source code:
case '0':
scanChar();
if (ch == 'x' || ch == 'X') {
scanChar();
if (ch == '.') {
scanHexFractionAndSuffix(false);
} else if (digit(16) < 0) {
lexError("invalid.hex.number");
} else {
scanNumber(16);
}
} else {
putChar('0');
scanNumber(8);
}
return;
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
scanNumber(10);
return;

So, according to javac, the literal "0" is actually an octal literal.

The language definition
(http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.1)
says:

DecimalNumeral:
0
NonZeroDigit Digits(opt)

OctalNumeral:
0 OctalDigits

OctalDigits:
OctalDigit
OctalDigit OctalDigits

OctalDigit: one of
0 1 2 3 4 5 6 7

That is, "0" by itself falls under DecimalNumeral. This would be simpler
if it read

DecimalNumeral:
NonZeroDigit Digits(opt)

OctalNumeral:
0 OctalDigits(opt)

but it doesn't.
 
B

blue indigo

The language definition
(http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.1)
says:

DecimalNumeral:
0
NonZeroDigit Digits(opt)

That is, "0" by itself falls under DecimalNumeral.

Of course, javac is still implementing the semantics required by the JLS,
since 0 octal equals 0 decimal. The compiler writer took a bit of a short
cut, is all.

Or as Mike indicated, perhaps the grammar writer took a bit of a long cut.
Perhaps he thought people would be confused if a bare 0 was an octal
constant? I'd have thought the target audience of the JLS wouldn't be.

Then again, over the years I've seen quite a lot of code with "long cuts"
or other strangenesses whose only plausible explanation was that the
programmer was drunk or otherwise under some sort of influence. Mild
inebriation could explain this grammer peculiarity, or just
unimaginitiveness. But if I had to place a bet, I'd put my money on sleep
deprivation. Programmers and technical writers are no strangers to pulling
all-nighters, and it is known that sufficiently severe sleep deprivation
has some of the same effects as intoxication.

This, if it is an instance of such, is a very mild one though. Some of the
bogosities I've seen have been horrendous. I've seen code that will turn
you white, make your hair fall out, and give *you* sleep deprivation for a
month! Worse, I've had to make changes to such code.

(I've also had to code to specs that had me tearing out my hair,
specs that made my co-workers of less sturdy constitution actually break
down and cry, and of course with a "we need this done yesterday!" deadline
accompanying. Usually web apps for a site whose ribbon-cutting date was
already announced with great fanfare to half the world. Half the time the
desired functionality required solving lots of general AI. At least those
can be cleverly solved by making the server outsource the hard bits to
humans, by presenting pending items to comment-posters and
guestbook-signers as captchas! This works until the spammers realize that
the captchas accept any response, and then the site has 2 problems. Most
spambots turn tail and run when they see a captcha form though, and the
rest are kept out by simpler means, like rejecting GET instead of POST
submissions, referrer checks, seeing if a captcha image was even
requested, requiring Javascript, requiring hidden form fields contain the
right values like a hash matching other page content, requiring things
like a 1x1 field named "subject" not be filled in, and such. There are
many, many ways to trip up a bot besides captchas.)

(If you ask me, nobody should be allowed to write specs, or be a
programmer's boss or manager, unless they have ten years experience
writing and maintaining and fixing and tweaking code themselves. What
happened to the days when programmers could get paid for being hackers?
Now we mostly take marching orders from MBAs that have never written so
much as a "Hello, World" themselves. Well, except for the lucky few that
get hired by Google, maybe.)

(Sorry about the OT digression. A couple of things have been bugging me
lately. No, don't ask. P.S. anyone ever seen pan hang with 100% cpu
utilization while composing a news post? 0.14.0 on very recent Fedora
Core, if it matters. Everything else in X11/Gnome is working fine, TTBOMK,
except gvim dumps core if I do some sufficiently obscure things. Perhaps a
library needs updating to a newer version somewhere? Console stuff, of
course, works flawlessly, though I hate text-mode vi/m (I miss my X
clipboard) and absolutely refuse to touch emacs or nano, the former in
or out of X and each for entirely separate reasons. So, it's probably an X
lib, GTK, or Gnome.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top