If it can be initiated with just
String s = "Hello";
then why is it a class? Where’s the parameters?
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.
Given that
Stringis such a useful and frequently used class, it has a special syntax (via a string literal representation: the text inside"") for creating its instances, but semantically these two are equivalent:Behind the hood both forms are not 100% equivalent, as the syntax using
""tries to reuse strings from Java’s string pool, whereas the explicit instantiation withnew String("")will always create a new object.But make no mistake, either syntax will produce a reference to an object instance, strings are not considered primitive types in Java and are instances of a class, like any other.