text 14 Sep Java Gotcha: type auto-conversion

If you want to have the escape character (0x1b in hex or 27  in decimal) into a string, you might do it this way:

String bold_tag = 0x1b+"|bC";

However, you will end up with the string bold_tag = “27|bC” because Java will auto-convert the char to string (0x1b to 27) before appending it to the rest of the string, which is absolutely not what you want.

To avoid this, you can do the following:

char ESC = 0x1b;
String bold_tag = ESC + "|bc";

This will force Java to use char as the type.




blog comments powered by Disqus
Design crafted by Prashanth Kamalakanthan. Content powered by Tumblr.