10 Things About String Every Java Programmer Should Know

A special class in Java which is most frequently used is called as String. You can find various things to understand about String in Java when compared to any other class and it is very important to have a good knowledge of various String functionalities for using it properly.

1) Strings do not have null termination in Java

There is a String termination in both C and C++ but it is not the case with Java. In Java, Strings are referred to as an object and is supported by character array. The character array can be retrieved by representing String in Java by calling the toCharArray() method of java.lang.String class of JDK.

2) In Java, Strings are both immutable and final

In Java, Strings are immutable which implies that after creating the string’s content cannot be changed. If at all you use toLowerCase(), toUpperCase() then a new String is created.

3) In a String Pool, Strings are kept

In Java, String is considered as a Special class and in a separate pool, all the String literals are maintained with special memory location inside Java memory more aptly inside PermGen Space. If a new string is created using String literal then the String pool is checked by JVM and if there is a similar object then you cannot create a new object.

Introduction Java String

4) Strings can be compared in Java using equal methods.

The equals method is overridden by String class and it offers a content equality and characters, order and case are the basis for it. You can use equals() to identify whether two string objects are same or not instead of equality operator.

5) lastIndexOf() and IndexOf() are used or matches(String regex) method to search inside String

A convenient method is offered by Java in String class to check if a sub-string or character or a pattern is present in a current String object. The position of character or String will be returned with the use of indexOf( ) if that is present in current String object or -1 and character is not present in String. The methodology of lastIndexOf is the same but it checks from an end.String.match(String regex) is more powerful which permits you to search for a regular expression pattern within the String.

6) To get part of String in Java, Use Substring

Another method is offered by Java String called substring () and it is used for getting parts of String. Start and end index and substring() method must be specified by returning character from the range. From 0, the index starts and continues till String.length()-1. Here the character array backs up the substring.

Do you know the Concept of Java String?

7) + is overloaded for String concatenation

Operator Overloading is not assisted by Java but String is special and + operator can be used to combine two Strings. Int, char, long, or double can be converted with the help of this into String by simply concatenating with empty string “”. With the help of StringBuffer + is internally implemented prior to Java 5 and StringBuilder from Java 5.

8) White spaces from the String will be removed using trim()

From both the ends of the String, trim() method can be used to remove the white spaces in Java Strings. If white spaces are removed with the help of trim() then a new String is returned or else it returns same String. Replace() and replaceAll() methods are also provided by String to replace the characters from String.

6 Points That Prove The Importance Of String In Java

9) With the help of Regular expression split(), methods can be used for splitting String

There are lots of features present in String in Java and it has methods like split(regex) and any String in form can be taken and it can be split too. It is quite useful if you are working on comma separated file (CSV).

10) Sensitive data cannot be stored in a String

Sensitive data like passwords, SSN always gives a threat in String. You cannot remove contents of String because String is immutable in Java and there is a longer duration of their stay in Java heap as they are kept in String pool.

Don't be shellfish...Email this to someonePrint this pageTweet about this on TwitterShare on StumbleUponShare on TumblrShare on LinkedInShare on RedditDigg thisShare on Google+Pin on PinterestShare on FacebookBuffer this page

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>