0

I am new to stackoverflow which I am finding it very useful, thanks so much for a great community. While I've been understanding about Strings in java, I just wanted to know how are Strings literals source created? I understood most of the concept of Strings in java. It is a special kind of class that is final, I also understood when creating a String literal, an object will be created on the heap and its reference will be in the string constant pool. by using this.

String name = "Manchester";

I also understood that when a string is created using new operator, the reference will have the object on the heap and a reference in the string constant pool. By this way.

String name = new String("United");

As I did understand how the object was created on the heap using the new operator, what I didnt understand is how the object is created on the heap when we use double quotes " ". How is this accomplished? I went thought the source code of String.class too but it wasn't of any help for me.

Can anyone please let me know how this works? Thanks.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
15R6
  • 307
  • 1
  • 4
  • 14
  • You don't need to worry about that. The compiler handles it. – Kayaman Apr 21 '16 at 07:38
  • It's the closest thing to operator overloading in java, I think it's changed to new String, when it gets compiled – Bálint Apr 21 '16 at 07:39
  • @Bálint It doesn't, otherwise all literals with the same value would be a different instance. – Mark Rotteveel Apr 21 '16 at 07:45
  • @Kayaman Thank for your reply. So lets say i am creating a new class and I want String like functionality to it, since the compiler handles the literals I can't do anything? What I mean to say is this ` className = "Manchester United";` – 15R6 Apr 21 '16 at 07:46
  • Bálint and Mark Rotteveel thanks for your answers too. – 15R6 Apr 21 '16 at 07:48
  • @15R6 Yeah, that's reserved for `String` only. You'll be using `new` whenever you want to create a new object. – Kayaman Apr 21 '16 at 07:51
  • @MarkRotteveel thanks for the other link. I didn't get that result when I searched for it. – 15R6 Apr 21 '16 at 07:54
  • @Kayaman Thanks for your reply too. That clears my doubt. – 15R6 Apr 21 '16 at 07:55
  • @MarkRotteveel I read the other link. I knew everything they were talking about. That was not my question. In my question I went a little further and asked how the literal source code is created. I feel that this is not a duplicate question. Kayaman answered my question. – 15R6 Apr 21 '16 at 09:12
  • 2
    [This answer](http://stackoverflow.com/a/17489466/466862) on that question is what Kayaman says, with more explanation. – Mark Rotteveel Apr 21 '16 at 09:51
  • @MarkRotteveel got it. Thanks. – 15R6 Apr 21 '16 at 10:11

1 Answers1

1
String name = "Manchester";

will search in constant pool of string(in heap) for the same valued object and if found will use that, if not will do this :

String name = new String("Manchester");

Do note that String is immutable, so it tries to use already present objects rater than creating one.

You can check it in this way:

String x = "a";
String y = "a";

x==y //true

And

String x = new String("a");
String y = new String("a");

x==y //false
Himanshu Tyagi
  • 5,201
  • 1
  • 23
  • 43
  • Thanks for your answer. I knew this but wanted to know exactly how the literals are created. Got the answers from MarkRotteveel and Kayaman. – 15R6 Apr 21 '16 at 07:56