[Java obfuscation] string, [Java] string

Source: Internet
Author: User

[Java obfuscation] string, [Java] string
Statement:Original work. When reposted, please indicate that the article is fromSAP EngineerBlog, and the original source of the article in the form of hyperlink, otherwise it will be held legally responsible!

String... 13
19. char type addition... 13
20. Unicode escape characters in the program... 14
21. Unicode escape characters in the comments... 14
22. Line End identifier on Windows and Linux... 14
23. ISO8859-1 character between 0-output... 14
24. String replace () and replaceAll () 15
25. Three bugs in a program. 16

String 19. char type Addition


System.Out. Println ('A' + 'A'); // 162
The above result is 162 instead of aA.
If at least one of the operands of the + operator is of the String type, the String join operation is executed. Otherwise, the addition is executed. If none of the values to be connected belong to the string type, you can choose one or more options: preset an empty string ("" + 'A '); use String as the first value. valueOf () is displayed as a String (String.ValueOf('A') + 'A'); Use a string buffer (sb. append ('A'); sb. append ('A'); or If JDK5.0 is used, you can use printf (System. out. printf ("% c", 'A', 'A '));

20. Unicode escape characters in the program


// \ U0022 is the Unicode representation of double quotation marks
System.Out. Println ("a \ u0022.length () + \ u0022b". length (); // 2

Unicode characters are converted to common characters during compilation. They are different from common escape characters (for example, they process escape character sequences only after the program is parsed into various symbols.

21. Unicode escape characters in comments


If the program contains the following comments: // d: \ a \ B \ util, the program cannot be compiled because \ u is not followed by four hexadecimal numbers, however, during compilation, the compiler regards the characters starting with \ u as Unicode characters.

Note: The comments also support Unicode escape characters.

Another problem is that \ u000A cannot be included in the comments line to indicate Unicode characters for line breaks, because when \ u000A is read during compilation, the line ends, then the subsequent characters will be treated as program code rather than comments.

22. Line End identifier on Windows and Linux


String line = (String) System.GetProperties(). Get ("line. separator ");
For(IntI = 0; I <line. length (); I ++ ){
System.Out. Println ((Int) Line. charAt (I ));
}

Running results on Windows:
13
10
Result of running on Linux:
10

On Windows, the line separator is composed of a carriage return (\ r) and a line break (\ n) after pressing it, but on Unix platforms, a separate line feed (\ n) is usually used).

23. The ISO8859-1 character between 0 and is output


ByteBts [] =New Byte[256];
For(IntI = 0; I <256; I ++ ){
Bts [I] = (Byte) I;
}
// String str = new String (bts, "ISO8859-1"); // correct practice
String str =NewString (bts); // use the default encoding method of the Operating System (xp gbk)
For(IntI = 0, n = str. length (); I <n; I ++ ){
System.Out. Print ((Int) Str. charAt (I) + "");
}
The above will not output a numeric String between 0 and, the correct way to use new String (bts, "ISO8859-1") to decode.

The ISO8859-1 is the only missing character set that allows the program to print integers from 0 to 255 sequentially, which is also the only one-to-one ing character set between characters and bytes.

Use java to obtain the default encoding method of the operating system:
System.GetProperty("File. encoding"); // jdk1.4 or a previous version
Java. nio. charset. Charset.DefaultCharset(); // Jdk1.5 or a later version

24. String replace () and replaceAll ()


System.Out. Println (".". replaceAll (". class", "\ $ "));
The above program will. replace with \ $, but an exception is reported during running. The second parameter of the original replaceAll contains two special characters (\ $), which have special meanings (\ used to transfer \ and $, $ followed by a number indicates reverse reference ). In addition, the first parameter of replaceAll is a regular expression, so pay attention to special characters. There are three correct methods:
System.Out. Println (". class". replaceAll ("\\.", "\\\\\$ "));
System.Out. Println (". class". replaceAll ("\ Q. \ E", "\\\\\\$ "));
System.Out. Println (". class". replaceAll (Pattern.Quote("."), Matcher.QuoteReplacement("\ $ ")));

API explanations for \, \ Q, and \ E:
\ Reference (escape) Next character
\ Q references all characters until \ E
\ E end reference starting from \ Q

JDK5.0 has added some new methods to solve this problem:
Java. util. regex. Pattern. quote (String s): Use\ QAnd \ E will cause parameters. These referenced strings are common characters, even if they contain special regular-expression characters.
Java. util. regex. Matcher. quoteReplacement (String s): converts the \ and $ values into strings that can be applied to the second replaceAll parameter.

String replace (CharOldChar,CharNewChar) method does not use regular expressions, but they only support characters, rather than strings, which are restricted in use:
System.Out. Println (".". replace ('.', '\'); // replace. \
System.Out. Println (".". replace ('.', '$'); // replace. With $

25. Three bugs in a program


Random rnd =NewRandom ();
StringBuffer word =Null;
Switch(Rnd. nextInt (2 )){
Case1:
Word =NewStringBuffer ('P ');
Case2:
Word =NewStringBuffer ('G ');
Default:
Word =NewStringBuffer ('M ');
}
Word. append ('A ');
Word. append ('I ');
Word. append ('n ');
System.Out. Println (word );
The above program aims to print Pain, Gain, and Main words with equal probability, but multiple running programs find that they will always print ain. Why?

The first problem is: rnd. nextInt (2) returns only numbers 0 and 1. Therefore, the preceding statement uses the branch Statement of case 1:. case 2: it will never go normally.

The second problem is that if the case statement does not end with a break, it will continue to run until the case statement of break is executed, therefore, the preceding statement runs the default branch statement every time.

The third problem is that the StringBuffer constructor has two acceptable parameters: StringBuffer (int capacity) and StringBuffer (String str ), the StringBuffer (char) constructor is used above. In essence, the character type is converted to the int type during runtime, so that the character is treated as the initial capacity of the StringBuffer, rather than the character itself.

The modified program fragment is as follows:
Random rnd =NewRandom ();
StringBuffer word =Null;
Switch(Rnd. nextInt (3 )){
Case1:
Word =NewStringBuffer ("P ");
Break;
Case2:
Word =NewStringBuffer ("G ");
Break;
Default:
Word =NewStringBuffer ("M ");
Break; // Optional

}
Word. append ('A ');
Word. append ('I ');
Word. append ('n ');
System.Out. Println (word );

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.