This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
To save lots of object creations and heap space (memory where objecs are stored)A concept called String pool in java is used. Because String once created are immutable,when ever a String is created in java it is placed in the String pool [you can assume as HashMap].
Example: String s="java"; This object s is placed in String pool.
String another = "java";//allocated from string pool
The next time you refer to the same String you created earlier , It is first looked up in String pool and the same string is used instead of creating a new String object.
when you say
String another = new String("java"); A new Object is created and placed in String pool with out prior checking whether that string literal already present in String pool.
Hope my explanation helps
Originally posted by dev pandya: what is the difference between String name="Java" and String name=new String("Java")
The correct term is : in Java the Strings are 'Interned'. i.e., if a string exists in the pool, a reference to that string will be returned when you use name="java" and when you use new String("java"), then a new string is created and returned regardless of its prior existence.