Strings In Java

Strings In Java

Introduction To Strings

  1. Strings are treated as objects in Java
  2. As it is an object memory is allocated in the heap memory
  3. It is a series of Characters or in simple words, anything in double quotes will be treated as a String
  4. In some programming languages we can use double and single quotes
  5. In Java String is always enclosed within double quotes
  6. In Java we don't have a null terminator as we have in c language

Types Of String In Java

  1. Immutable Strings: Once created cannot be changed
    In the real world what we don't change is: the name For immutable string, we use the string class
  2. Mutable Strings: Once created they can be changed To manage mutable strings we use a string buffer class and a string builder class

Ways To Create A String

  1. String str= "Allien'
  2. String str= new String("Allien");

    Note: The Letter S in String should be capitalized as it represents the String Class which is an inbuilt class in Java & according to the naming condition all Classes are represented in Capital Letters.

Heap Area

  1. The heap area consists of something called as String Constant Pool
  2. Duplication is not allowed in String Constant Pool
  3. Duplicates are allowed in the strings which are not stored in the String Constant Pool
  4. Strings that are created using without the new keyword then memory is allocated outside the string constant pool
  5. Strings which are created using the new keyword then memory is allocated inside the string constant Pool 6.The string constant pool before Java 6 was a part of permanent generation

Code Snippet Of Creating Strings Using Both Methods

      class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 

        String str="jeevan";
        System.out.println(str);
        String str1= new String("jeevan");
        System.out.println(str1);
    }
}

Note : Here str1 stores the address / reference to the String.

Deeper Understanding About Duplication

      class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 
        String str="jeevan";
        System.out.println(str);
        String str1="jeevan";
        System.out.println(str1);
    }
}

We know that strings cant be duplicated in the string Constant pool, then how does the above code snippet work

  1. Here what is happening is that when we create a string in the string constant pool, first it is scanned to check if we have that string or not. If we have then and again we try to create the same string then what happens is we pass the reference of the string which was previously created and do not actually create a new string object
  2. Different ways to compare string values a. We can use == operator --- reference variables are compared example:
       class HelloWorld {
      public static void main(String[] args) {
       System.out.println("Hello, World!"); 
        String str="jeevan";
        String str1="jeevan";
        System.out.println(str1==str);
    }
}
  b. We can use equals()--- actual values of the string are compared
  c. We can use equalsIgnoreCase()-- we ignore the case sensitivity    
  d. ComapreTo()-- values are compared references

Concatenation In Java

  1. When we use concatenation and we use references then memory is allocated in the run time and hence they are allocated in the heap area but outside of the string constant pool
  2. However when we do not use references then the allocation will take place in the string constant pool

Immutability

  1. What happens is when we use concatenation then a new string is created. Based on what we have used while concatenation either it will be present in the string constant pool or it will be placed outside of the string constant pool but inside the heap

Using new keyword to create a string

  1. When we use a new keyword the string is created in the heap area outside of the string contant pool but inside the heap. Some more additional things happen.
  2. If that string is not present in the string constant pool, then an object for the string literal will also be created. As long as the same string is not present in the string constant poo it will also be created there as well.