Java String ManipulationPosted 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.
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 JavaHere 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 subSequenceJava 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 JavaToday 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 arrayWe 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 JavaString to char arrayString is a stream of characters. String class provides a utility method to convert String to char array in java. String Concatenation in JavaI 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 explainedHere are the steps involved in String concatenation using + operator:
String concatenation using append explainedIf we are using StringBuffer or StringBuilder object, it’s done in following steps:
Clearly second way is less time consuming and use less resources and produces less garbage collection. Like it? Share it!More by this author |