• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Different output when string concatenaion is involved

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a few questions about strings which are doing my head in. I am trying to understand the difference between the following two expressions



Based on this article http://www3.ntu.edu.sg/home/ehchua/programming/java/J3d_String.html , in the first example a string object will be created on heap and a reference to it is placed in the string pool. In the second example, a String Object is placed on the heap but i am not clear as to whether a reference to it is added to the pool.

I tried a simple tests as shown below but the output confused me even more



The output of the above program is



* Why does the result of s1==s2 change depending on whether the output is concatenated to a String?
* since s1 == s4 is false, does that mean that strings created using the new keyword are not placed in the pool?
* If the above is true, does that mean that strings created using the new keyword are NOT immutable?

i am seeing confusing descriptions in other places. Some say two objects are created when you use "new" some say only one.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The + operator has higher precendence than the ==. Effectively, the concatenation is occuring before the == hence they are not pointing to the same references. You are creating a new string object "2: hello" and seeing its its equal to the reference "hello", obviously this is false. If you were to put parenthesis around (s1==s2) you would get true I'm pretty sure?

You should know this you read the precedence post from the other day this is why you need to know precedences.


And also, since s1 == s4 is false, does not mean that strings created using the new keywork are not placed in the pool?



If you use any constructor of string, the object will be created in the heap, and not find a reference in the string literal pool I'm pretty sure.
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scotty Mitchell wrote:The + operator has higher precendence than the ==. Effectively, the concatenation is occuring before the == hence they are not pointing to the same references. You are creating a new string object "2: hello" and seeing its its equal to the reference "hello", obviously this is false. If you were to put parenthesis around (s1==s2) you would get true I'm pretty sure?

You should know this you read the precedence post from the other day this is why you need to know precedences.


And also, since s1 == s4 is false, does not mean that strings created using the new keywork are not placed in the pool?



If you use any constructor of string, the object will be created in the heap, and not find a reference in the string literal pool I'm pretty sure.



I did read it but i need a few more reads for it to really sink in
I actually thought that since one of the elements in the expression is a string, the + operator will be used for concatenating.

Thanks
 
Scotty Mitchell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And btw Strings are Immutable regardless of how they are created. Only StringBuilder and StringBuffer are mutable string objects.

I actually thought that since one of the elements in the expression is a string, the + operator will be used for concatenating.



The + operator is an overloaded operator and its use depends on the arguments...for example if you come across the output would be "3string". This is because the arguments the operator recieves first are primitive ints (precedence is factored in here). However, if you had you would get "string12" because the + operator sees a string object and an int.

I think the way you have your code now looks like if you were to use the concat operator instead of +. Maybe, what you were intending to do is print
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scotty Mitchell wrote:And btw Strings are Immutable regardless of how they are created. Only StringBuilder and StringBuffer are mutable string objects.



According to this article http://www.javaranch.com/journal/200409/Journal200409.jsp#a1 when a string is created using new, a String object is created on the heap as shown in the diagram below:



The object reffered to by the reference variable "two" was created using the new keyword. As it is not refered to from the constant pool, what provides its immutability?

Thanks
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its immutability is provided by the design of the String class, which doesn't provide any methods which change the state of a String object. The fact that there exist references to a String from one place or another have nothing to do with that.
 
Scotty Mitchell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The object reffered to by the reference variable "two" was created using the new keyword. As it is not refered to from the constant pool, what provides its immutability?



I dont think I'm understanding the question you are asking...I do know that the reference to the string pool doesnt matter for immutability directly because if it did then there would be a problem with the wrapper class Integer immutability as there is no integer constant pool.

As far as my knowledge of immutable is concerned it just means if the reference variable two is pointing to "someString" then you can't change the value of "someString" without rerefencing the variable two to point to it. When I say change the value I kind of mean that loosely, as in you really cant get to it. It's not that a new object hasnt been created.

I.e If you have


Of course you can change the reference. Like if you wanted two to point to "someOtherString" in that case the "someString" object would be as they say "lost" because nothing else references it.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an addition to what left off, important material that will appear on the exam. See my responses for more info...

Check out this thread:
https://coderanch.com/t/539616/java-programmer-SCJP/certification/String-Object-Comparison
 
Scotty Mitchell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for that link tommy...I can never find these topics lol
 
reply
    Bookmark Topic Watch Topic
  • New Topic