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

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

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -