

Point (1) should say you can get *a copy of* the character array used to represent String in Java by calling toCharArray().įrom Java 7u6 onwards, the substring() method does NOT use the underlaying char array anymore, it DOES copy the part of the array it needs.įrom Oracle's Java SE 7 Features and Enhancements: Your posts are very useful & understandable for me. i love java since i don't know what is java. I thank millions & thank alot & thank you "the writer" so much.
JAVA SUBSTRING START AND END INDEX EQUALS 0 CODE
It is fun and educational to learn about programming reading the source code of Java, such as the String class. I don't appreciate it before, until of course running into problems later due to concurrency. I love the fact that in Java, developers are more mindful of writing immutable classes. SSN on toString, because those information may end up on log files, compromising security and confidentiality.ģ) Prefer () over () for better formatting.Ĥ) Prefer StringBuilder over StirngBuffer over String concatenation. At the same time, encrypt, mask or simply don't include sensitive information e.g. While country.equals("USA") will throw NullPointerException, if country is null.Ģ) Always override toString() method, especially for value object, business and domain objects. "USA".equals(country) will return false if country is null. calling equals() on String literal rather than on String object e.g. Just to add on this article, I would like to share couple of best practices while I am here :ġ) While calling equals() method with String literal, prefer defensive approach e.g. Indeed String is a Special class and special knowledge of String helps a lot.

See here to read more about character encoding in Java Thankfully, Java allows you to specify default character encoding for your application using system property file.encoding. because our server was not using Unicode encodings like UTF-8 or UTF-16. I have faced instances where our program fail to interpret Strings from European language e.g. character encoding of your server, and believe me this can cause huge trouble if you are handling Unicode data, especially if you are converting byte array to XML String. By default Java uses platform encoding i.e.

If you have a String, in memory or stored in file, you must know what encoding it is in, or you cannot display it correctly. You can not assume that "plain" text is ASCII.

There is no way to interpret a String if you don't know the encoding it used. It does not make sense to have a String without knowing what encoding it uses. See Why char is more secure than String for storing passwords in Java for more details.ġ1) Character Encoding and String Apart from all these 10 facts about String in Java, the most critical thing to know is what encoding your String is using. Instead, char should be used to store passwords or sensitive information. Since String is immutable in Java there is no way you can erase contents of String and since they are kept in the String pool (in case of String literal) they stay longer on Java heap, which exposes the risk of being seen by anyone who has access to Java memory, like reading from the memory dump. String pose a security threat if used for storing sensitive data like passwords, SSN or any other sensitive information.
