top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference when String is gets created using literal or new() operator ?

0 votes
373 views
What is the difference when String is gets created using literal or new() operator ?
posted Jan 13, 2017 by Karthick.c

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

When we create string with new() Operator, it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in PermGen area of heap.

String s = new String("Test");

does not put the object in String pool , we need to call String.intern() method which is used to put them into String pool explicitly. its only when you create String object as String literal e.g. String s = "Test" Java automatically put that into String pool.

answer Jan 23, 2017 by Dhaval Vaghela
Similar Questions
0 votes

Will the String literal be garbage Collected?

  1. String str="ansh";
  2. str=null;
  3. //More code here

If no reference of String str is there after line 2, will this string become eligible for garbage collection or will it remains in String pool during the entire execution?

0 votes

I am search for different approach.
Let see how many solution i can get

+1 vote
class test
 {
    static boolean check;
    public static void main(String args[])
   {
        int i;
        if(check == true)
            i=1;
        else
            i=2;
        if(i=2) i=i+2;
        else i = i + 4;
        System.out.println(i);
     }
}
...