permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Different output when string concatenaion is involved Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Different output when string concatenaion is involved" Watch "Different output when string concatenaion is involved" New topic
Author

Different output when string concatenaion is involved

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

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.
Scotty Mitchell
Ranch Hand

Joined: Aug 09, 2011
Posts: 46
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

Joined: Oct 02, 2005
Posts: 430

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

Joined: Aug 09, 2011
Posts: 46
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

Joined: Oct 02, 2005
Posts: 430

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
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

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

Joined: Aug 09, 2011
Posts: 46

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.
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
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:
http://www.coderanch.com/t/539616/java-programmer-SCJP/certification/String-Object-Comparison


OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Scotty Mitchell
Ranch Hand

Joined: Aug 09, 2011
Posts: 46
thanks for that link tommy...I can never find these topics lol
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Different output when string concatenaion is involved