June 2005
Beginner to intermediate
312 pages
6h 24m
English
This chapter contains puzzles that concern strings, characters, and other textual data.
What does the following program print?
public class LastLaugh { public static void main(String args[ ] ) { System.out.print("H" + "a"); System.out.print('H' + 'a'); }}
If you are like most people, you thought that the program would print HaHa. It looks as though it concatenates H to a in two ways, but looks can be deceiving. If you ran the program, you found that it prints Ha169. Now why would it do a thing like that?
As expected, the first call to System.out.print prints Ha: Its argument is the expression "H" + "a", which performs the obvious string ...