String In JAVA With Example

String is the most commonly used class in Java programming language. In java every string we create is an object of String Class that implements SerializableComparable and CharSequence interfaces. It is nothing but a character array for example “AbhiAndroid” is a string of 11 characters as shown.

char[] test= { 'A' , 'b' , 'h' , 'i' , 'A' , 'n' , 'd' , 'r' , 'o' , 'i' , 'd' };
String  testString= new String(test);

Strings are Immutable in nature, which means once string is created its value cannot be altered, but we can create a new Instance of it.

Immutable: Any object whose state cannot be altered once created are called Immutable objects. String, Integer, Byte, Short, all other wrapper class objects are immutable in nature.


Creating a String In JAVA:

There are two ways by which String can be created:

  1. Using String literal
  2. Using New keyword

Using String Literal

Strings can be created very easily, by assigning a string value to the string literal as shown:

String string1 = "Welocome to AbhiAndroid";
String string2 = "Welocome to AbhiAndroid";

As, discussed above String is a class, but we have not created any object above using new keyword, so how new object is created? Don’t worry compiler did your task. But problem here is that, if the object is already present in the memory compiler do not create a new object rather it assigns the same old object to the new instance created, which means even though we have two string instances above string1 and string2, compiler only creates one string object having the value “Welocome to AbhiAndroid” and assigns the same to both the instances String1 and String2.

Using New keyword:

As we saw above, by using only String literal compiler assigned the same string object to two different string literals, To overcome this approach we can create string using new keyword as shown

String string1 = new String(" Welocome to AbhiAndroid ");
String string2 = new String(" Welocome to AbhiAndroid ");

In this approach compiler will create two different objects in memory having the same value.


String Important Points In JAVA:

  1. Strings in Java unlike C, C++ are not null Terminated as String is a Class which is backed by Character array.
  2. Strings are Immutable and Final in Java, which means once created we cannot modify its content.
  3. Strings are maintained in a String pool, which is a special memory location. Any time we create a new string using String Literal, JVM first checks String pool and if any object which is similar to the content is available, then it returns the same and doesn’t create a new object. However JVM doesn’t perform String pool check if object is created using new operator.
  4. For comparing two String objects or we can say to check whether they are same or not, equals()method is used instead of equality(==) operator. String class overrides equals() method that provides content equality, which checks characters, case and order.
  5. Avoid storing sensitive data in String like passwords, bank account information etc. As Strings are immutable, there is no way to erase the content from String because data is stored in java heap for longer time and which exposes risk of being seen by anyone who has access to Java memory, like reading from memory dump.

String Quick Revision Points:

  • String is a sequence of characters, or we can say similar to character array
  • String is a java is not null terminated
  • Strings are immutable and final
  • Strings are maintained in a String pool
  • To compare contents of String equals() method is used instead of == operator

Use character array for storing sensitive information likes passwords instead of Strings.