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?

6
  • 2
    Both are absolutely identical with regard to what objects are created, what lands in the thread pool and runtime behaviour. Commented Oct 7, 2019 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 Commented Oct 7, 2019 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";. Commented Oct 7, 2019 at 13:40
  • Both are same and may reuse an instance from the string constant pool if one is available already. Commented Oct 7, 2019 at 13:41
  • @Nishant Lakhara if both are the same what is the reference for second one ? Commented Oct 7, 2019 at 13:51

3 Answers 3

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.
Sign up to request clarification or add additional context in comments.

5 Comments

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 literal "Hello Java" will be stored in the String Poll not depending on did you make a reference to it or not.
Technically the object for the literal is not created "here" but at class initialization.
@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.
I think this not in the scope of this question.
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.

1 Comment

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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.