I’m wondering if a new object is creating here:
String obj;
if(obj == "") {
}
and here:
if(obj.equals("")){}
I mean is the object like new String(“”) instantiating for those both cases?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Kind of.
You’re right that the program will actually reference a full
Stringobject, containing the value"". However, this isn’t strictly created at the point the method is invoked. The Strings for (compile-time constant) string literals are created in a JVM-wide constant pool when the class is loaded into the VM, and identical constants share the same strings.Since there’s almost certainly a class in the JVM itself that references the empty string literal, the string pool will already contain the object corresponding to
""and so your class won’t actually cause a new object to be created.