2
\$\begingroup\$

Using the eclipse formatting on a class with some @NamedQueries created concatenations.

While I have been doing this for years without paying attention I just realised that may be it is not a good usage.

So basically Which one of these Strings inside the queryis better ? Is the second one creating too many Strings? Should I be bothered at all?

My answer would be yes second one is creating extra Strings but I am not sure I should be concerned.

First Query

 @NamedQuery(
      name = SomeConstant,
      query="SELECT person FROM Person person Where person.name = :name ")

Second Query

 @NamedQuery(
      name = SomeConstant,
      query="SELECT person FROM Person person"+" "+
               "Where person.name = :name ")
\$\endgroup\$
10
\$\begingroup\$

Enter the mad world of optimization!

The Java compiler and HotSpot just-in-time compiler are programmed with a great many rules for optimizing performance of Java code.

In general, it is NOT worth your time to fret about the performance impact of these kinds of details as they are often taken care of. Rather, just try to write clean readable code that solves the problem. If performance is an issue, profile the code and then optimize the bottlenecks.

Now, to address this specific issue, I am virtually certain that constant expressions are resolved by the Java compiler at compile time. I would be very surprised if the byte code for your two queries wasn't identical.

\$\endgroup\$
  • \$\begingroup\$ Good call I will check the byte code. I should have done that first. \$\endgroup\$ – Shahzeb May 22 '12 at 6:14
  • \$\begingroup\$ Byte code editor confirms that Strings are exactly the same. \$\endgroup\$ – Shahzeb May 23 '12 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.