Java string of mystery, test your level of understanding of Java
To a two interesting examples:
1. URL duping Is google SUN on the very special treatment? ?
(Public class BrowserTest
Public static void main (String [] args) (
System.out.print ( "iexplore:");
Http://www.google.com;
System.out.println ( ": maximize");
)
)
The above example will be able to run? You identify? Yes, I am sure that this example can be completely normal operation. Why? This example is equivalent to:
(Public class BrowserTest
Public static void main (String [] args) (
System.out.print ( "iexplore:");
Http:
/ / Www.google.com;
System.out.println ( ": maximize");
)
)
Hey, just as http label (label), as JAVA no goto statements, it is through the label and continue to simulate the statement. And / / www.google.com just notes. The first time I saw this example was when the mixed.
2. UNICODE fully with the procedure said: Below this the "code" into a Ugly.java document, compile it to run, the output Laodiaoya 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
Hey, is fun to look at the expense of the procedure, so to avoid using escape at it.
3. Look at this example, print?
(Public class LastLaugh
Public static void main (String args []) (
System.out.print ( "H" + "a");
System.out.print ( 'H' + 'a');
)
)
Kazakhstan? I smile in the second half of the sound, print out the Ha169. The reason is very simple, +, only to do the heavy-duty type String, and no char type, the first two sentences output is the char to int and then + operator, or 72 +97. This problem can be resolved in the preceding empty string:
System.out.print (""+' H '+' a ');
Or the use of printf JDK5, System.out.printf ( "% c% c", 'H', 'a');
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 that will print from 0-255 Unfortunately, the results are not necessarily, it depends on your platform's default character set. My machine is GBK default, the process is only in the character set ISO-8859-1 that it is, we can read:
String str = new String (bytes, "ISO-8859-1");
The default platform in the use of string in JDK5 java.nio.charset.Charset.defaultCharset () to enquiries.
5. Another is the need to pay attention to the replaceAll String Method of a parameter is a regular expression, rather than constant string. String you can use the alternative method to replace.
Tags: java string, String






