Puzzle 15: confusing hello

Source: Internet
Author: User

The following program is a version after a slight change in the conventional example. So what does it print?

/** * Generated by the IBM IDL-to-Java compiler, version 1.0 * from F:\TestRoot\apps\a1\units\include\PolicyHome.idl * Wednesday, June 17, 1998 6:44:40 o’clock AM GMT+00:00 */public class Test{    public static void main(String[] args){        System.out.print("Hell");        System.out.println("o world");    }}

This puzzle looks quite simple. The program contains two statements: the first prints hell, and the second prints in the same line. To effectively connect the two strings. Therefore, you may expect the program to print Hello world. However, it is a pity that you have made a mistake. In fact, it cannot be compiled at all.

The problem is that the third line of the comment contains the character \ units. These characters begin with a backslash (\) and followed by the letter U, and it (\ U) represents the beginning of a unicode escape character. Unfortunately, these characters are not followed by four hexadecimal numbers. Therefore, this Unicode escape character is a constructor, And the compiler is asked to reject the program. Unicode escape characters must be properly constructed, even in comments.

It is legal to insert a properly constructed Unicode Escape Character in the annotation, but there is almost no reason to do so. Programmers sometimes use Unicode escape characters in javadoc comments to generate special characters in the document.

// Usage of Unicode escape characters that are problematic in javadoc comments/*** this method callitself recursively, causing a * stackoverflowerror to be thrown. * The algorithm is due to Peter von der Ah \ u00e9. */

This technique represents a useless usage of Unicode escape characters. In javadoc annotations, HTML Entity escape characters should be used to replace Unicode escape characters:

/** * This method calls itself recursively, causing a * StackOverflowError to be thrown. * The algorithm is due to Peter von der Ahé. */

Both of the preceding annotations should be named "Peter der Ah é" in the document, but the latter annotation is understandable in the source file.

You may be surprised. In this puzzle, the problem lies in commenting on this information from an actual bug report. This program is generated by machines, which makes it difficult for us to track the source of the problem-IDL-to-Java compiler. To prevent other programmers from getting into this situation, if the Windows file name is not pre-processed to eliminate the backslash, the tool should ensure that the Windows file name is not placed in the comments of the generated Java source file.

In short, make sure that the character \ U does not appear outside the context of a valid Unicode escape character, even in comments. Pay special attention to this issue in the code generated by the machine.

Puzzle 15: confusing hello

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.