Java puzzles (puzzle 22)

Source: Internet
Author: User

Java puzzles (puzzle 22)

Puzzle 22: URL Spoofing

This puzzle utilizes a little-known feature in a java programming language. Consider what the following program will do?

public class BrowerTest{public static void main(String[] args){System.out.ptintln("iexplore");http://www.google.com;System.out.println(":maximize");}}


This is a strange problem. When we see this program for the first time, we may think that the program will be compiled incorrectly; But the strange thing is that after you run it, we will find that this program will not do anything special, instead, print the iexplore: maximize directly. Then, let's take a closer look at this program, and you will find the clues. When we learned the C language, we had access to the goto statement, and the label of the goto statement can be expressed by http:, while // can be regarded as a comment line, therefore, the URL in the middle of the program is a statement label followed by the end comment. Because java does not have a goto statement, this causes fewer labels to be used in java, in this puzzle, the reference "little-known features in java programming language" is actually a label that can be placed before any statement. This program marks an expression Statement, which is legal but does not work.

Its value is to remind you that if you really want to use labels, this will be a more reasonable formatting method:

public class BrowerTest{public static void main(String[] args){System.out.ptintln("iexplore");http://www.google.com;System.out.println(":maximize");}}


In fact, this also illustrates that there is no need or reason to use and Add labels and annotations irrelevant to the program. So the lesson we can learn from this puzzle is that misunderstanding of comments and irrelevant code can cause confusion. Write comments carefully and keep them up with the times; remove obsolete code. In addition, if something looks so strange that it is not right, it is very likely to be wrong.

Related Article

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.