Java String Manipulation

Posted by Infocampus HR on December 11th, 2017

Let’s look at some benefits of String immutability, that will help in understanding why String is immutable in java.

  • String pool is possible only because String is immutable in java, this way Java Runtime saves a lot of java heap space because different String variables can refer to same String variable in the pool. If String would not have been immutable, then String interning would not have been possible because if any variable would have changed the value, it would have been reflected to other variables also.
  • If String is not immutable then it would cause severe security threat to the application. For example, database username, password are passed as String to get database connection and in socket programming host and port details passed as String. Since String is immutable it’s value can’t be changed otherwise any hacker could change the referenced value to cause security issues in the application.
  • Since String is immutable, it is safe for multithreading and a single String instance can be shared across different threads. This avoid the usage of synchronization for thread safety, Strings are implicitly thread safe.
  • Strings are used in java classloader and immutability provides security that correct class is getting loaded by Classloader. For example, think of an instance where you are trying to load java.sql.Connection class but the referenced value is changed to myhacked.Connection class that can do unwanted things to your database.
  • Since String is immutable, its hashcode is cached at the time of creation and it doesn’t need to be calculated again. This makes it a great candidate for key in a Map and it’s processing is fast than other HashMap key objects. This is why String is mostly used Object as HashMap keys.

Above are some of the reasons I could think of that shows benefits of String immutability. It’s a great feature of Java String class and makes it special.

What is Java String Pool?

As the name suggests, String Pool in java is a pool of Strings stored in Java Heap Memory. We know that String is special class in java and we can create String object using new operator as well as providing values in double quotes.

String Pool in Java

Here is a diagram which clearly explains how String Pool is maintained in java heap space and what happens when we use different ways to create Strings.

Java String subSequence

Java 1.4 introduced CharSequence interface and String implements this interface, this is the only reason for the implementation of subSequence method in String class. Internally it invokes the String substring method.

String to byte array, byte array to String in Java

Today we will learn how to convert String to byte array in java. We will also learn how to convert byte array to String in java.

String to byte array

We can use String class getBytes() method to encode the string into a sequence of bytes using the platform’s default charset. This method is overloaded and we can also pass Charset as argument.

String to char array, char array to String in Java

String to char array

String is a stream of characters. String class provides a utility method to convert String to char array in java.

String Concatenation in Java

I am sure that you might have heard that we should not use String “+” operator for string concatenation in java. Also we should use StringBuffer or StringBuilder for this purpose. It’s a very common java interview questions and you should be prepared for it.

If you have dug deeper, you might know that String internally uses StringBuffer (till java 1.4) or StringBuilder  for String “+” operator calls.

String Concatenation in java using + operator explained

Here are the steps involved in String concatenation using + operator:

  1. A new StringBuilder object is created
  2. String “Journal” is copied to the newly created StringBuilder object
  3. StringBuilder append() method is called to append “Dev !!” to the object
  4. StringBuilder toString() method is called to get the String object
  5. The new String object reference is assigned to str and older string “Journal” is now available for garbage collection.

String concatenation using append explained

If we are using StringBuffer or StringBuilder object, it’s done in following steps:

  1. A new StringBuffer object is created with value “Journal “
  2. append() method is called to to append “Dev !!” to the object
  3. StringBuffer toString() method is called to get the String object

Clearly second way is less time consuming and use less resources and produces less garbage collection.

Like it? Share it!


Infocampus HR

About the Author

Infocampus HR
Joined: December 10th, 2016
Articles Posted: 792

More by this author