Java string puzzles test your understanding of Java

Source: Internet
Author: User
Here are two interesting examples:

1. Is Sun's treatment for Google special if URL is fooled ??

Public class browsertest {
Public static void main (string [] ARGs ){
System. Out. Print ("iexplore :");
Http://www.google.com;
System. Out. println (": Maximize ");
}
}

Can this example be run? Are you sure you want? Yes, I'm sure, this example can run completely normally. Why? This example is equivalent:

Public class browsertest {
Public static void main (string [] ARGs ){
System. Out. Print ("iexplore :");
HTTP:

// Www.google.com;
System. Out. println (": Maximize ");
}
}

Haha, HTTP is only used as a label. Because Java does not have a GOTO statement, it is simulated through the label and continue statements. And // www.google.com; is just a comment. I was confused when I first saw this example.

2. Programs fully represented by UNICODE: Put the following "code" into a uugly. Java file, compile and run the program, and output the old Hello world.

/U0070/u0075/u0062/u006c/u0069/u0063/u0020/u0020/u0020/u0020
/U0063/u006c/u0061/u0073/u0073/u0020/u0055/u0067/u006c/u0079
/U007b/u0070/u0075/u0062/u006c/u0069/u0063/u0020/u0020/u0020
/U0020/u0020/u0020/u0020/u0073/u0074/u0061/u0074/u0069/u0063
/U0076/u006f/u0069/u0064/u0020/u006d/u0061/u0069/u006e/u0028
/U0053/u0074/u0072/u0069/u006e/u0067/u005b/u005d/u0020/u0020
/U0020/u0020/u0020/u0020/u0061/u0072/u0067/u0073/u0029/u007b
/U0053/u0079/u0073/u0074/u0065/u006d/u002e/u006f/u0075/u0074
/U002e/u0070/u0072/u0069/u006e/u0074/u006c/u006e/u0028/u0020
/U0022/u0048/u0065/u006c/u006c/u006f/u0020/u0077/u0022/u002b
/U0022/u006f/u0072/u006c/u0064/u0022/u0029/u003b/u007d/u007d

Haha, it's fun. You can watch the program, so try to avoid using escape characters.

3. Let's take a look at the example below and print what?

Public class lastlaugh {
Public static void main (string ARGs []) {
System. Out. Print ("H" + "");
System. Out. Print ('H' + 'A ');
}
}
Haha? I couldn't smile in the second half, and the printed content was ha169. The principle is very simple. The "+" operator only reloads the string type, but the char type does not. Therefore, the 2nd output statements convert Char to int and then perform the "+" operation, that is, 72 + 97. To solve this problem, add an empty string:

System. Out. Print ("" + 'H' + 'A ');

Or use printf, system. Out. printf ("% C", 'h', 'A') in JDK 5 ');

 

4. String Cheese:

Public class stringcheese {
Public static void main (string ARGs []) {
Byte bytes [] = new byte [256];
For (INT I = 0; I <256; I ++)
Bytes [I] = (byte) I;
String STR = new string (bytes );
For (INT I = 0, n = Str. Length (); I <n; I ++)
System. Out. Print (INT) Str. charat (I) + "");
}
}
You may think it will print from 0 to 255, but the result is not necessarily, depending on the default Character Set of your platform. On my machine, the default is GBK, which is only available when the character set is ISO-8859-1, so it can be changed:

String STR = new string (bytes, "ISO-8859-1 ");

The default platform string is queried in JDK 5 using Java. NiO. charset. charset. defaultcharset.

 

5. Another thing to note is that the first parameter of the string replaceall method is a regular expression rather than a constant string. You can use the string replace method instead.

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.