• Post Reply Bookmark Topic Watch Topic
  • New Topic

source code of String while using '+' and concat()  RSS feed

 
Ranch Hand
Posts: 500
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

source code for above code is:

according to this, whenever we concatenate strings using '+' operator, a new StringBuilder class object is created and then those two(multiple) Strings append to them using append()
and then that newly created StringBulder object is converted to String by using to.string() and finally that newly created object is returned (correct me wherever I am wrong)

But what if we concat two strings using concat(). Answer is, no StringBuilder class object is created (as I saw is the source code) and the concatenation is performed to the original string reference variable(as I saw is the source code) and original string reference is returned ( I know this statement is wrong as String object are immutable )
code using concat() :

source code:


May be I am not getting the source code correctly
guide me please
thank you
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you know from that analysis is that the String's concat() method is called. But you can't conclude just from that information anything about StringBuilder objects being created or not because you haven't looked into how the concat() method works. For all we know (and I haven't cared to look into it) the concat() method might use a StringBuilder to do its work.
 
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:according to this, whenever we concatenate strings using '+' operator, a new StringBuilder class object is created and then those two(multiple) Strings append to them using append()
and then that newly created StringBulder object is converted to String by using to.string() and finally that newly created object is returned (correct me wherever I am wrong)

I have to say I'm really surprised you have managed to compile class T successfully, because I would expect a compiler error when you try to compile this code (something like s2 can't be resolved to a variable)But you are correct in your analysis. This codewill be compiled to something likeSo a StringBuilder instance is created, the append() method is invoked and finally the result of the toString() method is assigned to s1. But once again: this code could be slightly (or totally) different if you used another compiler to compile this little code snippet. And that's one of the reasons why this is not on the OCA exams (and it's not on the OCP exams as well).

Puspender Tanwar wrote:But what if we concat two strings using concat(). Answer is, no StringBuilder class object is created (as I saw is the source code) and the concatenation is performed to the original string reference variable(as I saw is the source code) and original string reference is returned ( I know this statement is wrong as String object are immutable )

Looking at the source code of the concat() method will confirm that a StringBuilder class is not used. But a new char array is created with a length equal to the sum of the lengths of both strings. Then this array is filled with the characters of the "this" String and then the other String. Finally a new String is created using this array. So the original String reference is not returned! There is one case when the original String reference is returned: if you concatenate the empty String. And both can be easily illustrated using this little code snippet

But it will probably be more interesting to compare the concatenation operator and the concat() method and see if both function similar and result in the same outputAnd now we have the same examples using the concat() method

Now it's up to you: what's the output of these four examples?

Hope it helps!
Kind regards,
Roel

A very important disclaimer: for the OCA exam you do not need to know anything about the decompiled (byte)code and possible compiler optimizations at all! It's a good thing that you are interested in the "behind the scenes", but it's not required for the OCA exam (nor for the OCP exam). That's why I moved this topic to another forum, so other OCA aspirants don't get confused or worried. I hope that's ok with you!
 
Puspender Tanwar
Ranch Hand
Posts: 500
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Roel..


And now we have the same examples using the concat() method

hope I am correct in all four examples ??

Roel De Nijs wrote:A very important disclaimer: for the OCA exam you do not need to know anything about the decompiled (byte)code and possible compiler optimizations at all! It's a good thing that you are interested in the "behind the scenes", but it's not required for the OCA exam (nor for the OCP exam). That's why I moved this topic to another forum, so other OCA aspirants don't get confused or worried. I hope that's ok with you!

that's ok Roel and thank you for moving this post to its perfect place . Actually I was curious to check the behind the scene operation of '+' and concat() (not for OCA/OCP)

can you please tell me the compiler code for concat() that how its concat the strings(behind the scene )
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:hope I am correct in all four examples ??

Unfortunately you are not. You only answered 1 (example 3) correctly. The correct answers are:
  • example 1: nullJava
  • example 2: Javanull
  • example 3: a NullPointerException is thrown (on line 4)
  • example 4: a NullPointerException is thrown as well (from inside the concat() method of class String)


  • Puspender Tanwar wrote:can you please tell me the compiler code for concat() that how its concat the strings(behind the scene )

    I already explained in my previous post how the concat() method works: a new char array is created with a length equal to the sum of the lengths of both strings. Then this array is filled with the characters of the "this" String and then the other String. Finally a new String is created using this array.

    If you are interested in the actual source code (in Java 8), here it is:And now you clearly can see why the 4th example was throwing a NullPointerException

    Hope it helps!
    Kind regards,
    Roel
     
    Puspender Tanwar
    Ranch Hand
    Posts: 500
    2
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thank you for the concat() code
    but I have many doubts in my mind !!!
    let's first take '+' operator :
    1. how the null is converted is String "null" ?

    now for concat() code you provided :
    1. at //line4 , what is returned by this
    2. at //line6, there is no such value String reference variable declared in method, nor it came from arguments (is it the String on which we have applied the concat() ?? )
    3. at //line7, will depend on answer I will get for point 2.
    4. at//line 8, there is no such method getChars(char[] buf, int len) (having only 2 arguments) present in the String class ?
     
    Roel De Nijs
    Sheriff
    Posts: 11338
    177
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Puspender Tanwar wrote:1. how the null is converted is String "null" ?

    That's how the append(String) method of StringBuilder works. It's clearly mentioned in the javadoc of this method.

    Puspender Tanwar wrote:1. at //line4 , what is returned by this

    this is a special reference variable refering to the current object.

    Puspender Tanwar wrote:2. at //line6, there is no such value String reference variable declared in method, nor it came from arguments (is it the String on which we have applied the concat() ?? )
    3. at //line7, will depend on answer I will get for point 2.
    4. at//line 8, there is no such method getChars(char[] buf, int len) (having only 2 arguments) present in the String class ?

    value is one of the instance members of class String, it's the char array to store all characters of this string. There is no public getChars method, but there is a package-private one.

    If you are really keen on knowing what's happening inside the concat() method, just have a look at the source code of this method (from OpenJDK).

    Hope it heps!
    Kind regards,
    Roel
     
    Puspender Tanwar
    Ranch Hand
    Posts: 500
    2
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ok
    thank you Roel
     
    Roel De Nijs
    Sheriff
    Posts: 11338
    177
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One little additional question. Assume you change class T toWhen you compile this class and then run javap on the compiled class, will the output be similar to the one from your first post?
     
    Puspender Tanwar
    Ranch Hand
    Posts: 500
    2
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote:One little additional question. Assume you change class T toWhen you compile this class and then run javap on the compiled class, will the output be similar to the one from your first post?


    I think making s1 final will not effect the bytecode but when I ran javap on your code, I just got amazed (no ldc for final reference even string object "bye" is stored on heap ) .... but after spending plenty of time, I made a guess that this is happening because final variables get value at compile time and compiler is sure that this can never be changed ( actually I am confused in my words ,that what actually I am saying )
    Sorry Roel, but I have just made a guess, because I am not pretty much sure about what ldc , astore_1, aload_1 actually doing
    I just looked the source code again and again and guessed that:
    ldc it is an opcode(just like variable) that store the value
    # it is the index(just like reference variable stored in Content Table) which refers to the String object in the Heap
    no guess about the others ,that's why not sure for my answer
     
    Roel De Nijs
    Sheriff
    Posts: 11338
    177
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Puspender Tanwar wrote:but after spending plenty of time, I made a guess that this is happening because final variables get value at compile time and compiler is sure that this can never be changed
    Sorry Roel, but I have just made a guess, because I am not pretty much sure about what ldc , astore_1, aload_1 actually doing

    No need to be sorry! I have some years of experience as a Java developer and I don't know what ldc, astore_1, and aload_1 are all about.

    And your guess is spot-on! Because the compiler knows s1 can't change (it's final), the compiler already performs an optimization at compile time. The decompiled code of class T looks likeSo because s1 is a compile-time constant and "tata" is a String literal (which is a compile-time constant by definition), the compiler already performs the concatenation. So you'll see this is slightly more performant than the original code, because you don't need a StringBuilder and there's no need to invoke the append() and toString() method.

    Hope it helps!
    Kind regards,
    Roel

    PS. If you want to look at decompiled code of a class, you can use a Java decompiler (procyon, jad, and many more). But again this warning: the OCA exam focuses on the actual source code, not on the decompiled code, nor on bytecode!
     
    Puspender Tanwar
    Ranch Hand
    Posts: 500
    2
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Roel De Nijs wrote:
    And your guess is spot-on!

    yeeiipiii I won

    Roel De Nijs wrote: If you want to look at decompiled code of a class, you can use a Java decompiler (procyon, jad, and many more). But again this warning: the OCA exam focuses on the actual source code, not on the decompiled code, nor on bytecode!

    ok, thank you Roel
     
    Bartender
    Posts: 689
    17
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Since you're interested I'll tell you what the bytecode keywords mean:

    ldc: This pushes something from the constant pool into the stack. There are a couple of different variations of this code, but they all load from the constant pool. There are various different things in the constant pool, but among them are the String literals that your code uses. It will be getting the String "byetata" from there.

    astore_1: This pops an object reference from the top of the stack and stores it in a local variable. The prefix 'a' means that this is dealing with a reference type, and there are various different variations of the store code for the other types (lstore means longs, istore means ints etc). The suffix _1 is a shorthand to say that you want to store into local variable 1. There are variations for the commonly used variables (astore_1, astore_2 etc), and one that lets you specify any of the 65535 possible local variables.

    aload_1: This does the opposite of astore_1. It pushes the object reference in local variable 1 onto the top of the stack. However the code you posted does not produce any aload calls when compiled with my compiler. No local variables are referenced because s1 is optimised away (since the compiler knows what value it will have it does not need it any more). In fact the String "bye" is optimised out too since it is never used.

    However as Roel said you don't need to know any of this as a Java programmer, so don't worry about it and just treat it as something that is interesting.
     
    Puspender Tanwar
    Ranch Hand
    Posts: 500
    2
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thank you Mike. J. Thompson
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!