Why does Java allow to assign a string literal to a String object? -


string class in java. while declaring , assigning string, correct string name = "paul" though instantiate object java class string name = new string(); taking name object, i'm wondering why can assign series of characters "paul" object. under concept 1 works , how it?

that's because implicitly strings - "..." treated objects well.

under cover jvm looks similar "objects" in called string pool , if finds there returns object (instead of creating new one) otherwise creates new object , puts string pool.

this memory efficiency, , "paul" new string("paul") not same.

this possible because know, strings immutable in java.

you can read more behavior search keyword "string pool".


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

How can I fetch data from a web server in an android application? -

jquery - How can I dynamically add a browser tab? -