Firstly apologies if this a very basic question, I’m just curious to know the difference between the following string definitions
String x= "hello";
String y = new String("hello");
I knew that in java String is a Class its neither a primitive nor a Wrapper(Correct me if this a misconception). Consider a class A, I’ve seen the following declarations for any class so far. i think A c; is valid and A a = new A(); is also valid. I’m confused with A a ="xyz"; this is how we declared a String as in above first type of definition. I’m sure that the above two definitions are absolutely different, like if i say x==y it returns false. I understand that y is the reference to the String object. What is x there, how is it stored in memory, interestingly i found that both x and y can access all the methods of String class.
Then what is the advantage of one over other.Can i know the applicability of each.
From the 2nd edition of Joshua Bloch’s “Effective Java”: