0

I'm studying about String in Java and I wonder, what is difference between:

String hello = "Hello Java";
System.out.println(hello);

and only use this:

System.out.println("Hello Java");

Are they the same ? Both go in string pool ? Thanks !

Second example:

if("dog" == "cat")
   return true;

Are this literals stored in java memory ? If they are store what are the references?

wartus
  • 21
  • 3
  • 2
    Both are absolutely identical with regard to what objects are created, what lands in the thread pool and runtime behaviour. – Joachim Sauer Oct 07 '19 at 13:38
  • 2
    Both are same. There is no difference in this strings. if you say String s = new String("Hello Java"); Then there may be difference – Mohinuddin Luhar Oct 07 '19 at 13:39
  • AFAIK String are constants in Java or at least should be considered as constants because it is enumeration of pointers of char values. That's why doing `String one = new String("one"); String one2 = new String("one");` is a possible memory leak and should be `static final String ONE = "one";`. – Paul Oct 07 '19 at 13:40
  • Both are same and may reuse an instance from the string constant pool if one is available already. – Nishant Lakhara Oct 07 '19 at 13:41
  • @Nishant Lakhara if both are the same what is the reference for second one ? – wartus Oct 07 '19 at 13:51
  • @wartus : both may have different references but there is one storage in string pool from where the value is picked up. – Nishant Lakhara Oct 08 '19 at 11:59

3 Answers3

2
String hello = "Hello Java";
  • "Hello Java" is the java literal. It's put into the String Pool
  • hello is the java reference to the value from String Pool
  • exactly ONE object is created here

String hello = new String("Hello Java");
  • "Hello Java" is the java literal. It's put into the String Pool
  • hello is the java reference to the Object from Hash that has a value from String Pool
  • exactly TWO objects are created here
  • String helloNew = hello.intern(); put the string from hello to the String Pool, release new String("Hello Java") and retrieves a reference to the value from String Pool.

System.out.println("Hello Java");
  • "Hello Java" is the java literal. It's put into the String Pool
  • exactly ONE object is created here
  • note: does not matter String hello = "Hello Java" or simple "Hello Java", every string literal goes to the String Pool.
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
  • I know this for new String. But I don't know when i write only the java literal what is happening. For example: if("dog"=="cat") the compiler make new String from dog and cat ? If this is true what is the java reference? – wartus Oct 07 '19 at 13:48
  • @wartus literal `"Hello Java"` will be stored in the `String Poll` not depending on did you make a reference to it or not. – Oleg Cherednik Oct 07 '19 at 13:52
  • Technically the object for the literal is not created "here" but at class initialization. – Lew Bloch Oct 07 '19 at 22:59
  • @wartus when you do `"dog" == "cat"`, keep in mind this is _not_ how strings are compared, this is solely comparing the memory addresses of the two values. That said, they'll be different simply by being different strings. If you were to do `"dog" == "dog"`, you'd get `true`, but `"dog" == new String("dog")` would return `false`. – Rogue Oct 08 '19 at 12:47
  • I think this not in the scope of this question. – Oleg Cherednik Oct 08 '19 at 12:49
0
String hello = "Hello Java";

I think this String variable hello will be in the string pool.

System.out.println("Hello Java");

Here argument passed in the argument of println method will not be stored permanently. It will be stored temporarily inside println method only and then removed after the method call completes.

N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32
  • All String literals are stored as objects in the string pool. You are correct that the parameter reference goes away upon return from the method, but the object will remain in the pool. – Lew Bloch Oct 07 '19 at 23:01
0

For indepth analysis, you can go through 1.
For second question, objects are stored on string pool