- Views: 8
- Report Article
- Articles
- Computers
- Programming
Java String Manipulation
Posted: Dec 11, 2017
Why String is immutable in java is one of the popular interview question. String is one of the most used classes in any programming language. We know that String is immutable and final in java and java runtime maintains a String pool that makes it a special class.
Let’s look at some benefits of String immutability, that will help in understanding why String is immutable in java.
- a href="http://infocampus.co.in/python-training-in-bangalore.html">String pool
- 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 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 Java String 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:
- A new StringBuilder object is created
- String "Journal" is copied to the newly created StringBuilder object
- StringBuilder append() method is called to append "Dev!!" to the object
- StringBuilder toString() method is called to get the String object
- The new String object reference is assigned to str and older string "Journal" is now available for garbage collection.
If we are using StringBuffer or StringBuilder object, it’s done in following steps:
- A new StringBuffer object is created with value "Journal "
- append() method is called to to append "Dev!!" to the object
- 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.
The Best Training Institute in Bangalore for IT course is Infocampus. We offer courses on Web designing, JAVA, iOS, Digital Marketing, Software development and so on. Highly Talented With 8+ Years Experienced Mentors.100% Job Assistance.