Java - String

What is String ? 
    It is a Java Object. String is a immutable. It is a collection of characters.
Immutable - Does not change.

Why String ?
  To create a immutable values.

When String?
  When the value should not change on the fly then we need to create a String. For Example, JDBC connection. Once we connect with one database if somebody changing again it should not change and connect another database. Example 2 : Connecting network

Usually when we are creating the string it will store in the string constant pool. In String constant pool there will not be duplicate values allowed. It means that when you are creating the string s1,s2 with same values it will refer the same string constant pool. And also using any method you cannot change the string value.



Ways to Create a String?
Literal -
String s = "Vinoth"; // It will store in String constant pool.

new operator -
String s = new String ("Vinoth"); // It will store in heap memory

Note: Objects are created in Heap memory

Comments

Popular posts from this blog

How to set Java Object into JaxBElement ?

GitLab