I know this seems to be a very basic questions but still want to clear my basic knowledge so i am asking this question What is String Pool in java?
what is difference between
String var="DANIWEB;
and
String var =new String("DANIWEB");

Recommended Answers

All 8 Replies

String var = "daniweb";
String val = "daniweb";

the above will only place one String in the pool, and the other String will point to that one as well.

String var = "daniweb";
String val = new String("daniweb");

since you here are explicitly saying you want a new Object, a new String will be created in the pool, and your second variable will point to that one.

http://en.wikipedia.org/wiki/String_interning

I've never heard of a reason why you would force allocation of a new String object with new String("daniweb" as opposed to allowing the compiler to intern it, and right now I can't think of a single reason why you would want to do that...

probably a school assignment to figure out the difference.

Thanks stultuske for your reply,
you said the other String will point to that one as well
String var = "daniweb";
String val = new String("daniweb");

amoung this val will point to var right? so why to create this object in the pool

no. since there you are declaring it as 'new String("daniweb");' it will not point to var.
if you just do String myVal = "daniweb"; it will just check whether a String object with the value "daniweb" already exists, and if so, point to the place in the memory where that String is stored.
by saying 'new String("daniweb")' you 'override' every check for existing instances, and you create a new one, storing the information in a new location in the memory, regardless whether there already is a String in the pool with that value.

Ohh that is very interesting

since you here are explicitly saying you want a new Object, a new String will be created in the pool, and your second variable will point to that one

This isn't correct AFAIK; instantiating new strings on demand doesn't send them to the "string pool" unless the newly created string is specifically/explicitly interned.

and right now I can't think of a single reason why you would want to do that

Creating a new String isn't a common occurrence though it might be required in some cases. Two reasons I can think of:

  • You are working with a mixed-mode binary protocol which has binary content interleaved with textual content. If you want to create a String out of bytes, you need to use the string constructor
  • You are reading large amounts of textual data from an external source (each line containing around 1K-10K words) but you need to work with just a small chunk of that text. The substring method returns a shallow copy of the string so the substring always refers to the char array of the original string. This is a problem because if you are doing substring on a very large string, you'll end up hanging around with more data than you require. In this case, creating a new String is a good way of making sure the large string is eligible for garbage collection.

As an example for the above explanation:

public class Testi {

    public static void main(String[] args) throws Exception {
        // showcase string interning
        String k1 = new String("howdy").intern();
        String k2 = "howdy";
        System.out.println("Is newly created 'howdy' with interning same as the literal 'howdy'?: " + (k1 == k2));

        // showcase need for new String() construct
        String p1 = "a very long string which takes up a lot of space";
        String p2 = p1.substring(0, 1); // naive substring returns a string which hangs on to original char array
        System.out.println("Original String char[]: " + getUnderlyingCharArrayAsString(p1));
        System.out.println("Substring char[]: " + getUnderlyingCharArrayAsString(p2));
        String p3 = new String(p1.substring(0, 1)); // create new string to make original eligible for GC
        System.out.println("Substring with new String() char[]: " + getUnderlyingCharArrayAsString(p3));
    }

    private static String getUnderlyingCharArrayAsString(String s) throws Exception {
        Field f = s.getClass().getDeclaredField("value");
        f.setAccessible(true);
        return Arrays.toString((char[])f.get(s));
    }

}

Thanks it is really helpful

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.