Thursday, September 13, 2018

10 Things Every Java Programmer Should Know about String


Introduction

The string in Java is a very special class and the most used class. There is a lot to learn about the java chain that any other class, and a good knowledge of the various features of the chain, allows you to use it properly. Given the extensive use of Java String in almost all types of projects, it is even more important to know the fine details of the chain. Although I've already posted many articles about the channel on Javarevisited, this is an attempt to bring together some of the channel's resources. In this tutorial, we'll look at some important points about Java String that are worth remembering. 
Although I've tried to capture many things, there are certainly few things I could have lost; Please let me know if you have any questions or doubts regarding the java.lang.String feature and I will try to address them here.
Strings do not end in Java in null.
Unlike C and C ++, string in Java does not end with the null character. String is instead an object in Java and is compatible with the character array. You can get the character array to represent the string in Java by calling the toCharArray () method of the java.lang.String class of the JDK.
The chains are invariable and final in Java
Channels are fixed in Java, which means that you can not change the contents of the channel. If you change them to toLowerCase (), toUpperCase (), or any other method, the new string will always be returned. Since string is final, nobody can expand a string or replace a string feature. Well, if you're interested, because string in java is immutable or final. Because?
Strings are preserved in the string group
For example, as mentioned earlier, String is a special class in Java and the entire literal string. "abc" (all in quotation marks are string literals in Java) are stored in a separate set of sequences, a special memory area in Java memory, especially in the PermGen area. Whenever a new string object is created with a string literal, the JVM first checks the string group and, if an object with similar content is available, returns it and does not create a new object. , The JVM does not validate the channel pool when it creates an object with the new operator.
 If you do not know this string behavior, you may encounter subtle problems, here's an example
String name = "Scala"; //1st String object
String name_1 = "Scala"; //same object referenced by name variable
String name_2 = new String("Scala") //different String object

//this will return true
if(name==name_1){
System.out.println("both name and name_1 is pointing to same string object");
}

//this will return false
if(name==name_2){
System.out.println("both name and name_2 is pointing to same string object");
}


if you compare name and name_1 using equality operator "==" it will return true because both are pointing to same object. While name==name_2 will return false because they are pointing to different string object. It's worth remembering that equality "==" operator compares object memory location and not characters of String. By default Java puts all string literal into string pool, but you can also put any string into pool by calling intern() method of java.lang.String class, like string created using new() operator.

Use Equals methods for comparing String in Java

String class overrides equals method and provides a content equality, which is based on characters, case and order. So if you want to compare two String object, to check whether they are same or not, always use equals() method instead of equality operator. Like in earlier example if  we use equals method to compare objects, they will be equal to each other because they all contains same contents. Here is example of comparing String using equals method.
String name = "Java"; //1st String object
String name_1 = "Java"; //same object referenced by name variable
String name_2 = new String("Java") //different String object

if(name.equals(name_1)){
System.out.println("name and name_1 are equal String by equals method");
}

//this will return false
if(name==name_2){
System.out.println("name_1 and name_2 are equal String by equals method");
}


You can also check my earlier post difference between equals() method and == operator for more detail discussion on consequences of comparing two string using == operator in Java.

Use indexOf() and lastIndexOf() or matches(String regex) method to search inside String

String class in Java provides convenient method to see if a character or sub-string or a pattern exists in current String object. You can use indexOf() which will return position of character or String, if that exist in current String object or -1 if character doesn't exists in String. lastIndexOf is similar but it searches from end. String.match(String regex) is even more powerful, which allows you to search for a regular expression pattern inside String. here is examples of indexOf, lastIndexOf and matches method from java.lang.String class.

String str = "Java is best programming language";

if(str.indexOf("Java") != -1){
     System.out.println("String contains Java at index :" + str.indexOf("Java"));
}

if(str.matches("J.*")){
     System.out.println("String Starts with J");
}

str ="Do you like Java ME or Java EE";

if(str.lastIndexOf("Java") != -1){
      System.out.println("String contains Java lastly at: " + str.lastIndexOf("Java"));
}


As expected indexOf will return 0 because characters in String are indexed from zero. lastIndexOf returns index of second “Java”, which starts at 23 and matches will return true because J.* pattern is any String starting with character J followed by any character because of dot(.) and any number of time due to asterick (*).

Remember matches() is tricky and some time non-intuitive. If you just put "Java" in matches it will return false because String is not equals to "Java" i.e. in case of plain text it behaves like equals method. 

Apart from indexOf(), lastIndexOf() and matches(String regex) String also has methods like startsWith() and endsWidth(), which can be used to check an String if it starting or ending with certain character or String.

Use SubString to get part of String in Java

Java String provides another useful method called substring(), which can be used to get parts of String. basically you specify start and end index and substring() method returns character from that range. Index starts from 0 and goes till String.length()-1. By the way String.length() returns you number of characters in String, including white spaces like tab, space. One point which is worth remembering here is that substring is also backed up by character array, which is used by original String. This can be dangerous if original string object is very large and substring is very small, because even a small fraction can hold reference of complete array and prevents it from being garbage collected even if there is no other reference for that particular String. Read How Substring works in Java for more details. Here is an example of using SubString in Java:

String str = "Java is best programming language";
    
//this will return part of String str from index 0 to 12
String subString = str.substring(0,12);
    
System.out.println("Substring: " + subString);


"+" is overloaded for chain chaining

Java does not support operator overload, but String is special and the + operator can be used to concatenate two strings. It can even be used to convert int, char, long, or double to convert to string by simply concatenating with the empty string "". internal + is implemented with StringBuffer before Java 5 and StringBuilder as of Java 5. This also brings up the point of using StringBuffer or StringBuilder to manipulate string. Since both represent changeable objects, they can be used to reduce the drop of chains generated due to the time chain. Read more about StringBuffer vs. String Builder.

Use trim () to remove string spaces

String in Java provides the trim () method to remove the whitespace at the end of the string. If trim () removes whitespace, it returns a new string, otherwise the same string is returned. Along with trim () String also provides the replace () and replaceAll () methods to replace strings. The replaceAll method supports the regular expression. Read here how to replace the string in Java.

Use split () to divide the string using the regular expression

The sequence in Java is rich in resources. this has methods like split (regex) that can accept any string as a regular expression and share the string based on it. Especially useful if you want to treat the file separated by a comma (CSV) and have a single part in a string array. There are also other methods related to string splitting, see this java tutorial to divide the string for more details.

Do not store confidential data in a chain

Chains are a security threat when used to store sensitive data such as passwords, SSNs or other sensitive information. Since String is immutable in Java, there is no way to delete the contents of String, and because they are stored in the String pool (in the case of literal strings), there is more time in the Java heap, which runs the risk to be seen by anyone who has access to Java memory, eg. B. reading from the memory image. Instead, char [] should be used to store passwords or sensitive information. Learn more about why char [] is more secure than the string for storing passwords in Java.

Coding and string

In addition to all these 10 facts about string in Java, the most important thing to know is what is the encoding of your string you are using. It makes no sense to have a string without knowing which encoding is used. There is no way to interpret a string if you do not know the encoding you are using. You can not assume that the "plain" text is ASCII. If you have a string in memory or are stored in the file, you need to know what encoding it is in, or it can not be displayed correctly. By default, Java uses the platform encoding, ie the character encoding of your server, and you think that this can cause big problems when manipulating Unicode data, especially when you convert the byte array to XML string. I've seen cases where our program can not interpret strings from the European language, for example. German, French, etc. because our server did not use Unicode encodings like UTF-8 or UTF-16. Fortunately, in Java, you can specify the default character encoding for your application by using the file.encoding system property. Read here to learn more about character encoding in Java

This is all about the string in Java. As I said, the string is very special in Java, at some point it even has the kind of god. It has a unique feature, such as immutability, support for concatenation, caching, etc., and to become a serious Java programmer, detailed knowledge of the chain is very important. Do not forget the character encoding when converting a byte array to string in Java. Good knowledge of java.lang.String is necessary for good Java developers.





No comments:

Post a Comment

From Java 8 to Java 11

Switching from Java 8 to Java 11 is more complicated than most updates. Here are some of my notes on the process. Modules Java 9 i...