What does the following program snippet print?
System.out.println ( 'a' + '\r' );
System.out.println ( 'a' + "\r" );
System.out.println ( "a" + '\r' );
System.out.println ( "a" + "\r" );
for bonus points, what will it print on the Eclipse console?
Be honest. post your answer before you test.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Luc The Perverse - 27 Jan 2006 00:44 GMT
> What does the following program snippet print?
>
[quoted text clipped - 6 lines]
>
> Be honest. post your answer before you test.
Is this the test you give to "astute" Java programmers?
--
LTP
:)
slippymississippi@yahoo.com - 27 Jan 2006 01:23 GMT
I'd guess that adding 'a' + '\r' promotes the result to an int.
Adding 'a' + String probably results in a String, as does a String +
'\r' and String + String.
So whatever int value 'a' + '\r' becomes, plus:
a
a
a
Max - 27 Jan 2006 01:38 GMT
I don't really understand how knowing this code-snippet corresponds to
skill. It seems more like... trivia?
slippymississippi@yahoo.com - 27 Jan 2006 02:51 GMT
Actually, some of the crap that they ask you on the Sun Certification
exam is not much different than this.
Finding out the hard way that an arithmetic operator will cause a
character to be promoted to (I think) an integer has been branded onto
my frontal lobe for life. Who thought up *that* nonsense!?!? :)
Roedy Green - 27 Jan 2006 03:16 GMT
>I don't really understand how knowing this code-snippet corresponds to
>skill. It seems more like... trivia?
It is a gotcha, a flat in the design of the language where you get
astonishing results and even experienced people can trip. It helps to
be made aware of them from time to time. I have collected some at
http://mindprod.com/jgloss/gotchas.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Jeffrey Schwab - 27 Jan 2006 02:56 GMT
> What does the following program snippet print?
>
[quoted text clipped - 6 lines]
>
> Be honest. post your answer before you test.
Well, my theory *was* a compile-time error on 'a' + "\r". Live and learn.
Jim Korman - 27 Jan 2006 03:01 GMT
>What does the following program snippet print?
>
[quoted text clipped - 6 lines]
>
>Be honest. post your answer before you test.
Ok,
110
a
a
a
from my Windows console I expected......
Where is Eclipse putting in the extra linefeeds???????
Which made the following test not work at all
System.out.println ( 'a' + "\r" + "B");
Jim
Noodles Jefferson - 27 Jan 2006 04:08 GMT
> What does the following program snippet print?
>
> System.out.println ( 'a' + '\r' );
> System.out.println ( 'a' + "\r" );
> System.out.println ( "a" + '\r' );
> System.out.println ( "a" + "\r" );
a
\r is a carriage return and with no line feed it just overwrites what
was there before.
>
> for bonus points, what will it print on the Eclipse console?
No idea.
> Be honest. post your answer before you test.

Signature
Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM
NP: "Icicle" (Tour Rehearsal) -- Tori Amos
"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.
Roedy Green - 27 Jan 2006 06:41 GMT
On Thu, 26 Jan 2006 22:08:38 -0600, Noodles Jefferson
<silverbells@tacoshells.com> wrote, quoted or indirectly quoted
someone who said :
>\r is a carriage return and with no line feed it just overwrites what
>was there before.
on windows yes. But on the eclipse console it is a linefeed, as on
the mac.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Noodles Jefferson - 29 Jan 2006 20:00 GMT
> On Thu, 26 Jan 2006 22:08:38 -0600, Noodles Jefferson
> <silverbells@tacoshells.com> wrote, quoted or indirectly quoted
[quoted text clipped - 4 lines]
> on windows yes. But on the eclipse console it is a linefeed, as on
> the mac.
Interesting. I know that '\u007B' will produce an opening curly brace
and '\u007D' will give you a closing brace on XP machines. Comes in
handy if you want to use styles without making a .css file. (I know,
like that's hard but I'm experimenting here.)
I don't know if this will work on other systems.

Signature
Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM
NP: "Crazy On You" -- Heart
"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.
Danno - 27 Jan 2006 06:49 GMT
My guess (without running it or looking at answers, or looking up the
unicode/ascii values)
is:
1. an integer with the unicode value of a + unicode value of a carriage
return (probably around the early 100s) with one return on any OS.
2. an integer with the value of a (probably late 90s) with two
returns(on a mac & linux) one return (on windows)
3. the letter a with two returns (on a mac & linux) one return (on
windows)
4. the letter a with two returns (on a mac & linux) one return (on
windows)
To hell with the bonus,. Eclipse is 4 chumps!
;)
Danno - 27 Jan 2006 06:53 GMT
Damn, missed the second one. I thought that since from left to right
the String wasn't established that it would still consider it a number.
nice Roedy.
Roedy Green - 27 Jan 2006 10:52 GMT
>Damn, missed the second one. I thought that since from left to right
>the String wasn't established that it would still consider it a number.
>
>nice Roedy.
One of the annoying things about this is the compiler does not
complain. You might do this in obscure error messages that don't get
tested, so it can hide in there for years to baffle your customers.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Dag Sunde - 27 Jan 2006 07:26 GMT
> What does the following program snippet print?
>
[quoted text clipped - 6 lines]
>
> Be honest. post your answer before you test.
I was working on the excact same solution you got from Gordon
Beaton, when he beat me to it... :-)
In Eclipse, the code above will print:
a#
a#
a#
a#
(Where # = a square (unprintable char))
but it works ok when run in a console window under windows...

Signature
Dag.
Dag Sunde - 27 Jan 2006 07:33 GMT
>> What does the following program snippet print?
>>
[quoted text clipped - 18 lines]
>
> but it works ok when run in a console window under windows...
I'll be damned...
(embarrased...)

Signature
Dag.
Peter Davies - 27 Jan 2006 09:05 GMT
> What does the following program snippet print?
>
[quoted text clipped - 4 lines]
>
> for bonus points, what will it print on the Eclipse console?
We don't do people's homework for them here.

Signature
Peter Davies
:D
hilz - 27 Jan 2006 22:04 GMT
>>What does the following program snippet print?
>>
[quoted text clipped - 6 lines]
>
> We don't do people's homework for them here.
That is hilarious!
Now Roedy is being accused of posting his homeworks!
LOLLLLLLLLLL!
Please see http://mindprod.com/jgloss/homework.html
Sorry... couldn't resist!
Roedy Green - 28 Jan 2006 09:13 GMT
>Now Roedy is being accused of posting his homeworks!
It would be fun to be rich enough and unencumbered enough to go back
to university. I'm sure I would enjoy it much more this time, seeing
it for what it is, a chance to indulge in my curiosity habit with few
other pressing needs on my time.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Lion-O - 27 Jan 2006 14:35 GMT
> What does the following program snippet print?
>
> System.out.println ( 'a' + '\r' );
> System.out.println ( 'a' + "\r" );
> System.out.println ( "a" + '\r' );
> System.out.println ( "a" + "\r" );
I'm rather new to Java myself so I welcome these brain teasers. Having made the
mistake a few times to use 'test' instead of "test" when I needed a String its
my idea that it will do something funny with the '' encapseling.
cr/lf = 0d0a, but I'm not too sure what the ascii value of 'a' is. So my idea
is that it won't pick up the 'a' and '\r' as being Strings (perhaps a char) and
as such add them all up. No idea what the end result may be, probably an ASCII
value (if my theory is correct then it can't turn into high ascii) so I think
it'll start to print a letter in the range of ASCII value 70 - 100 (decimal).
Next the "" does denote a String but iirc \r doesn't resemble a cr/lf, thats
what \n does in a String. My guess is that it will simply print a character
here as well without anything special about it.
Since the 3rd line is basicly the same as the 2nd the same theory applies.
Last you have 2 Strings but since I'm positive \n to be cr/lr I think it'll
just print the 'a' as well.
I can't go for the bonus I guess since I'm using Netbeans. I haven't done much
with something specific like this on stdout so I'll have to guess as well; I
hope it behaves in the same was as the program does on the console.
Right, and now to test this later on 8-)
Thanks for the teaser, its a nice one IMO.

Signature
Groetjes, Peter
.\\ PGP/GPG key: http://www.catslair.org/pubkey.asc
Lion-O - 27 Jan 2006 21:09 GMT
> No idea what the end result may be, probably an ASCII value (if my theory is
> correct then it can't turn into high ascii) so I think it'll start to print a
> letter in the range of ASCII value 70 - 100 (decimal).
Well, not a character but a specific ASCII value (110) was printed. I guess I
have still some things to learn on this issue, never messed with char's so far.
> My guess is that it will simply print a character here as well without
> anything special about it.
And I have to admit being pleased that I got this part correct. And the theory
about this applying to the next 3 lines was correct as well :-)
> I haven't done much with something specific like this on stdout so I'll have
> to guess as well; I hope it behaves in the same was as the program does on
> the console.
I'm using Linux myself (as workstation) but one of the first things I did was
ditch the JVM which was shipped with it and replaced it with Sun's native JDK.
In my experience the 'open source variant' isn't very usefull and my personal
opinion on it is even worse.
Alas, on my box the output which was produced matches that of the rest. What I
do find interesting is that according to some posters Eclipse produced a rather
funny output, but Netbeans (on Linux anyway, I think I'll have a look see on
Windows later this evening) produced the exact same output in the output window
as the native jvm did on the console. I know I'm biased being a Netbeans user
myself but thats an extra point for Netbeans IMHO ;-)

Signature
Groetjes, Peter
.\\ PGP/GPG key: http://www.catslair.org/pubkey.asc
Oliver Wong - 27 Jan 2006 21:43 GMT
Couldn't see the original post on my newsgroup server, so I found it on
Google Groups. Sorry if this headers of this message implies that I'm
replying to someone other than the OP.
</quote>
What does the following program snippet print?
System.out.println ( 'a' + '\r' );
System.out.println ( 'a' + "\r" );
System.out.println ( "a" + '\r' );
System.out.println ( "a" + "\r" );
for bonus points, what will it print on the Eclipse console?
Be honest. post your answer before you test.
</quote>
I don't know what the ascii value of a and \r are, so I'll just pretend
they are 42 and 10. Adjust the answers I post accordingly for the real ASCII
values of those characters.
'a' + '\r' should be seen as a char plus a char, so the two operands
should get promotoed to integers, and you'd get "52" (or 42 + 10) on the
first line.
'a' + "\r" should do string conversion, and I think (but am not sure)
that 'a' would actually get converted to a string representation of its
ASCII value, rather than to the string "a", so I'd say "42" would be printed
on the next line, followed by an extra blank line due to the "\r".
Similarly for "a" + '\r', I expect to see "a10" on the next line.
For "a" + "\r", this should just be string concatenation, so "a" with a
extra newline.
So my final answer is:
<quote>52
42
a10
a
</quote>
Of course, depending on the console, maybe sending \r alone might mess
things up if it's not considered a line terminator, and you get question
marks or other strange characters. I think Eclipse will handle newlines in
an "intelligent" manner, and so would produce the output I gave above.
<tries it out>
Damn. I got 2 out of 4.
- Oliver