String in Java

String in Java

  • String is not primitive data type in Java. Char is primitive data type that is being used to construct String.
  • String is an object in java constructed from String Class. String class in java is having losts of constructors.
  • Interfaces like Comparable and Serializable are implemented by String Class.
  • String class is made final in Java because of Immutability. You can not subclass String class ie you can not extend String class.
  • You can initialise String in two ways

String Object :-

String s1 = new String("abc");

This goes to heap memory area.

String literal

String s1 = "abc";

This goes to String literal pool memory area. This is special area constructed by JVM. So if someone will again use "abc" string then new object will not be created the old "abc" object will be referred. 
Again if someone modifies "abc" then new object will be crated of modified string. Old object will be there. This will happen because of immutability.

This video will show you ways to create string in java, differences between them and cover some important points like how many objects will get created and memory locations.

Like this video if you find this useful. Put your questions in comment.