Spring Basics in Java

Java offers a final class java.lang.String which represents a sequence of characters as a 16 bit unicode. The string class offer various methods that can perform various operation on strings.

Creating strings in Java

There are three ways to create strings in Java

  • Using the new operator: String str = new String(“Hello World”); When we use the new operator every time a new string is created at runtime even if the same literal exists in the pool.
  • Using String Literals: String str = “Hello World”; When you use this approach to create a string, if the object still exists in memory that is the String Constant Pool ,it does not create a new object (string) rather it gives the reference to the existing object in memory.
  • Using two or more literals together: String str = “Hello” + “World”; or equivalently String str = new String(“Hello”+”World”); This will create a new object in memory even if the object still exists in the String constant pool.

What is String Constant Pool?

String constant pool is an area in heap memory where java stores literal string values.Heap is a memory area in Java which is used for dynamic memory allocation for Java and JRE classes at runtime.New objects are created in heap memory and references to this object are stored in stack memory.These objects have global access and can be accessed from anywhere in the application.

Immutability of Strings

Strings created by the String class are immutable that means once they are created ,you can not change them.However , you can change the reference variable that refers to a String, which can make it refer to another string.

When we concatenate two string , a new string is created in memory and the old string exists in the memory.

String str1= “Dog”;

String str2 = “Cat” ;

String str3 = Str1.concat(str2);

In the above example 3 strings are created in memory. The first String “Dog”, the second string “Cat” and the third string “DogCat” because strings are immutable.

String Example

In the above example 3 objects will be created , s1 and s2 will refer to “My String Class” and two other objects will be created “MY STRING CLASS” and “my string class”

Output

s1 and s2 still refers to the “My String Class” the two new objects created ,the upper case string and the lower case string does not have a reference.

Methods in String Class

There are several methods in String class to handle/modify Strings in an application.We can use these methods to perform operations on a string.Some of these operations are:

  • Concatenating a string to another : String concat(String str)
  • Comparing String:- int compareTo(String str)
  • Extracting a substring from a string: String substring(int beginIndex)
  • Create a copy of the string with all lowercase characters: String toLowerCase()
  • Create a copy of the string with all uppercase characters: String toUpperCase()
  • Search for a character or substring in java: int indexOf(int ch)

There are many other methods in String class to perform various operations on strings.You can read about them in details at Oracle Spring Docs

Leave a comment

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