String obj = null;
obj= new String("Samuel");
//vs
String obj = null;
obj="Samuel";
Is there any difference between these two ways of initializing a String?
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.
//vs
in the first case
obj==obj1returns falsein the second case
obj==obj1returns true.The reason for that is that in the first case you have two references to two different Objects. In the second case you have one object since Strings are immutable they are interned and drawn from the same pool.