-2

I have looked all over for this answer and I'm leaning toward thinking that the answer is yes:

I am creating a mini app to help with static code analysis. In order to make it run as fast as possible I need to understand how the language Im using groovy/java will deal with these strings. The problem I am having is understanding weather the code:

space.matcher(line.get(index)).replaceAll("")

or

line.get(index)

when used in comparisons in if and while statements will save the string created by the Pattern.matcher().replaceAll() or retrieved by the retrieve from list to the stack as in other litteral creation such as:

String foo = "foofoo"   //creates a string litteral, ref is saved to the stack
String fooTwo = "foofoo" //fooTwo==foo returns true since both have same ref to the litteral

I am Asking so that I may find out if when I make future refferences to the created or retrieved strings weather I am recreating the strings entirely or if I am just grabbing the refference to the litteral on the stack that I have already made so that future refferences to these same strings will be faster, or if I would be better off to save the strings locally as litterals at each step for faster access?

  • 1
    Why does this have so many downvotes with no comments?? +1 to offset the drive-by downvoters. – Dan Bechard Jul 07 '16 at 17:48
  • I will also +1 this to offset the unexplained downvotes. As for your question, read through the answers to this question: http://stackoverflow.com/questions/10759844/reusability-of-strings-in-java – Jon Peterson Jul 07 '16 at 18:03
  • Also, don't confuse `==` in Java and `==` in Groovy (.equals()). They are very different. – Jon Peterson Jul 07 '16 at 18:04
  • thank you for upvoting, I was very confused as well, my question is legitimate. @JonPeterson for recently moving to groovy thank you for pointing that equals desparity out, I was not aware that that was a change made in the language from java's refference based comparison. – WorkingOnBeingBetter Jul 07 '16 at 18:57
  • @JonPeterson thanks for the link but unfortunately thats not really what im looking for, I know about the string "pool" as they called it and about interning, I want to find out weather by calling the methods that I posted in the question if the strings returned were bieng interned automatically. – WorkingOnBeingBetter Jul 07 '16 at 19:02
  • `//fooTwo==foo returns true since both have same ref` this is wrong. each string object your have in your code are different values – injecteer Jul 08 '16 at 09:58

1 Answers1

0

Using the groovy console's AST browser to inspect the output of

'string a'.replaceAll("")

the generated byte code is

  // access flags 0x1
  public run()Ljava/lang/Object;
  L0
   INVOKESTATIC script1467995483671.$getCallSiteArray ()[Lorg/codehaus/groovy/runtime/callsite/CallSite;
   ASTORE 1
  L1
   LINENUMBER 1 L1
   ALOAD 1
   LDC 1
   AALOAD
   LDC "string a"
   LDC ""
   INVOKEINTERFACE org/codehaus/groovy/runtime/callsite/CallSite.call (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
   ARETURN
  L2
   ACONST_NULL
   ARETURN
   LOCALVARIABLE this Lscript1467995483671; L0 L2 0
   MAXSTACK = 3
   MAXLOCALS = 2

which shows the strings being added to the stack from the constant pool, see bytecode instructions

Dylan Bijnagte
  • 1,326
  • 9
  • 17
  • so when I call the code again, it does not re-perform this code but instead grabs the already stored litteral from the stack or will it repeat this proccess every time? – WorkingOnBeingBetter Jul 08 '16 at 17:14
  • It will use the same string, of course the memory will need to be accessed but it will not allocate any new memory or objects that need to be garbage collected. – Dylan Bijnagte Jul 08 '16 at 19:24