• Post Reply Bookmark Topic Watch Topic
  • New Topic

How many String objects are created in each line  RSS feed

 
Greenhorn
Posts: 25
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me how many objects got created at each line (in String pool or in Objects memory):
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. Can't be done.

Strings are weird. The problem is that the first time a string literal is encountered, it is created in the String pool. So since we do not see an entire program, we have no way to know if the literal "test" has been seen before your line 1. An object may or may not be created there.
 
Marwen Trabelsi
Greenhorn
Posts: 25
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that those instructions are within a main method, I will update my post.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
generally speaking, we prefer it if you don't edit your post. Now my first reply doesn't make much sense to the next person who comes along...

having said that...

how many object do YOU think are created on each line?

I'm pushing because I want you to think about it, not just be handed an answer. post what you think and WHY you think it. Then someone can come along and confirm or correct your beliefs. You will learn much more by trying to figure it out yourself than if you were just told the answer.
 
Marwen Trabelsi
Greenhorn
Posts: 25
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@fred rosenberger I'm sorry for editing the OP, I believe it was to much selfish from my part!

My assumption was that:
  • Line 3: One object is created in string pool.
  • Line 4: a new String object is created using the constant in line 1.
  • Line 5: I read in some post that underline, a StringBuilder object is created to concat the Strings so that makes it one object along with a String one
  • Line 6: A StringBuilder object is created only is created and no String Objects.
  •  
    Sheriff
    Posts: 4293
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here's my understanding. Better minds can correct me if I'm wrong.

    Line 3: An object is created. s1 is a reference to that object
    Line 4: If that's supposed to be String s2 = new String("test"); then no new object is created, but a new object reference is in s2.
    Line 5: There's a new object created and s3 refers to this
    Line 6: No *String* object is created, because StringBuilder doesn't create a String until you use toString().

    So, four object references to three objects, two of them Strings.
     
    Bartender
    Posts: 2087
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum wrote:Line 4: If that's supposed to be String s2 = new String("test"); then no new object is created, but a new object reference is in s2.

    Not true. This creates a new String object.
    Try this:
    This prints false meaning s1 and s2 are different objects meaning line 4 (line 2 in my example) created new object.

    BTW, calling new always creates a new object.
     
    Marwen Trabelsi
    Greenhorn
    Posts: 25
    Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Any one can give a concrete and clear answer with a line by line analaysis please?
     
    Marshal
    Posts: 4053
    239
    Clojure IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well Fred has already said that you're unlikely to get a ready made answer handed to you. You'll not learn anything that way.

    What did you think about Knute and Pawel's posts? Do you think they're right? Do you think they're wrong? Anything you don't understand?
     
    Rancher
    Posts: 42972
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In addition to what what said before, in my opinion such questions are pointless, because they depend on how the JVM and its class libraries are implemented. Who can say how many strings are created by the "+" operation? Or any of the StringBuilder operations? One would have to analyze the source code of the JVM and the class libraries to know for sure (and then the answer would still depend on the JRE in question, and could even differ between release versions).
     
    Marwen Trabelsi
    Greenhorn
    Posts: 25
    Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Marwen Trabelsi wrote:@fred rosenberger I'm sorry for editing the OP, I believe it was to much selfish from my part!

    My assumption was that:
  • Line 3: One object is created in string pool.
  • Line 4: a new String object is created using the constant in line 1.
  • Line 5: I read in some post that underline, a StringBuilder object is created to concat the Strings so that makes it one object along with a String one
  • Line 6: A StringBuilder object is created only is created and no String Objects.


  • Hi Tim, I've already posted what I thought it was going under the hood and how many objects are created.
    As a comment to @Knute response, I fairly agree with what he said except that in line 4 I think a new object is created.
    I still need more hints about what happens in line 5, since as I already reported, I read that a StringBuilder object is created when using the + operator on Stringsbut that still not from a trusted source and still need someone to endorse it.

    One more thing, and which actually lead me to writting this post, when we write a String s = "some text"; as in line 3:
  • My understanding is that a new Object is created in the string pool with content "some text".
  • Then when executing String anotherString = new String("some text");, another object is created (OK for me since we call the new Operator as @Paweł said), but where? Another String in string pools, then what string interning is about? Or is it a special case where the String object goes to the normal heap space?


  • I would much appreciate a deep explanation as this staff gets me confused usually.
     
    Tim Cooke
    Marshal
    Posts: 4053
    239
    Clojure IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think the most important response here is from Ulf. The fact is that you will never be able to predict how many objects are created due to the differences between JVM implementations and even versions within those implementations.

    I've been programming professionally for nearly a decade now and I've never had to care much about this sort of thing.
     
    Marwen Trabelsi
    Greenhorn
    Posts: 25
    Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So if the your answer is
    Tim Cooke wrote:never be able to predict how many objects are created due to the differences between JVM implementations and even versions within those implementations.
    , why did you reclaimed my thoughts about the subjects:
    What did you think about Knute and Pawel's posts? Do you think they're right? Do you think they're wrong? Anything you don't understand?
    .

    Sorry dude, but this makes no sence to me and even if the behavior is implementation dependent, I think that there is always an explanation for some core concepts and how the JVM has objects to in and out.
     
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Marwen Trabelsi wrote:
    Sorry dude, but this makes no sence to me and even if the behavior is implementation dependent, I think that there is always an explanation for some core concepts and how the JVM has objects to in and out.


    I don't think Tim said that there isn't an explanation -- that would obviously make no sense. I think what Tim is saying, since the actual definition of how the String pool is supposed to behave is not defined (in the specification), the actual explanation may vary from JVM to JVM, and many vary between different versions of Java.

    Henry
     
    Tim Cooke
    Marshal
    Posts: 4053
    239
    Clojure IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, what Henry said.

    Also, please be mindful that calling a stranger "dude" doesn't always come across well.
     
    Marwen Trabelsi
    Greenhorn
    Posts: 25
    Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry Tim if your took my message not the way I mean it, and trust me I meant nothing bad when I said dude since I've always carred to pay all my respects to all forum users and was thankfull for every effort you and all forum boddies show to help others.

    What I intented to meet with my post is to get better understanding of how Strings are managed by the JVM since I sometimes get supprising results when executing some statements that come across Strings.
    And since all what I got as a response is that it is jdk/jre independant, so I will have to better sharpen my question to some restricted area.
    Meanwhile, that drive me another wodering, should I then carre about my code portability as Strings may not opera the same in all areas? Or does that stuff still far from the hood (how objects are created and where...) and I should not care about it as it will be only intern to the JVM?
     
    Ulf Dittmer
    Rancher
    Posts: 42972
    73
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I wouldn't worry about it. The important facts about how String objects behave are mentioned in the javadocs, and that applies to all versions of all JREs.
     
    Marwen Trabelsi
    Greenhorn
    Posts: 25
    Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Ulf, I appreciate your statement.
     
    Tim Cooke
    Marshal
    Posts: 4053
    239
    Clojure IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Don't worry about it.

    Marwen Trabelsi wrote:I sometimes get surprising results when executing some statements that come across Strings

    Well this sounds like a more interesting topic of discussion. Why don't you tell us what this behaviour was and why it was surprising to you?
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Marwen Trabelsi wrote:
    And since all what I got as a response is that it is jdk/jre independant, so I will have to better sharpen my question to some restricted area.
    Meanwhile, that drive me another wodering, should I then carre about my code portability as Strings may not opera the same in all areas? Or does that stuff still far from the hood (how objects are created and where...) and I should not care about it as it will be only intern to the JVM?


    Whether a string is interned or not, it just means whether references are pointing to a string that is also referred to by the string pool or not. Which in turns, means that whether string objects are being shared.

    So, yes, it may mean some portability issues. However, if you always use the equals() methods to compare string values -- instead of using the == comparison operator to check if they are the same instance or not, then it should not really matter if the string is interned or not. In other words, your application should be concerned with the value of the string, and not about whether they are the same string instance.

    Henry
     
    Paweł Baczyński
    Bartender
    Posts: 2087
    44
    Firefox Browser IntelliJ IDE Java Linux Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I guess you see some results you don't expect when using equality operator ==. Am I right?
    This page → AvoidTheEqualityOperator ← is worth reading.
     
    Marwen Trabelsi
    Greenhorn
    Posts: 25
    Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well this sounds like a more interesting topic of discussion. Why don't you tell us what this behaviour was and why it was surprising to you?

    As an example, I remember a small chunk of code I have tested:


    @Henry: Thanks for your reply, it should make things clearer to me and as a conclusion I should not care about references but only content whe it comes to Strings.
     
    Tim Cooke
    Marshal
    Posts: 4053
    239
    Clojure IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ah ok, so it's confusion about the equals equality operator and how it works with Strings. Pawel guessed right and I would recommend reading through the page linked to in his post. I'll put it here again as it's worth repeating: AvoidTheEqualityOperator.

    If you search this forum for "string equals operator" you will find that it's been asked and answered a bazillion times before.
     
    Marwen Trabelsi
    Greenhorn
    Posts: 25
    Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Paweł yes exactely, you have seen right. Through the provided page I can see that I'm following the rules since I only use '==' fro almost the three reasons specified in there. But also as stated in the article, it is an SCJP matter since I'm preparing my exam. That's only why I bother about Strings with the '==' equality operator, but in real life I only use equals() for String comparison.

    By the way, since I'm new to the ranch could I ask where to have all those "how-to" pages referrenced?
    I can only come across one when reading some posts but don't know how to browse for them.
     
    Knute Snortum
    Sheriff
    Posts: 4293
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And now we get the full picture. This should have been the first post.

    Basically whatever the SCJP says is right you should echo on the exam. This is the difference between hashing out a discussion on a forum and studying for a test.

    Howto is here: http://www.coderanch.com/how-to/java/FrontPage


     
    Tim Cooke
    Marshal
    Posts: 4053
    239
    Clojure IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Marwen Trabelsi wrote:By the way, since I'm new to the ranch could I ask where to have all those "how-to" pages referrenced?
    I can only come across one when reading some posts but don't know how to browse for them.

    Marwen, that is a great question and one that I have been wondering about myself. So I've asked it in the Ranch Office.
     
    Marwen Trabelsi
    Greenhorn
    Posts: 25
    Java Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Many thanks Tim for reporting the question, I will absolutely watch the new post and interact if I have any updates or new wonderings.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!