1

I have two JAVA code snippets below, want to know which is better in terms of memory/performance.

First snippet:

String s1 = "USER.DELETE";
String s2 = "RESOURCE.DELETE";
String s3 = "ENTITY.DELETE";

Second snippet: one static final variable

private static final String DELETE = ".DELETE";

and then using this variable

String s1 = "USER" + DELETE;
String s2 = "RESOURCE" + DELETE;
String s3 = "ENTITY" + DELETE;
2
  • 2
    The difference in performance between the two is insignificant to the point of irrelevance. Benchmark your code and fix the real problems. Commented Feb 7, 2017 at 12:29
  • Not sure if, with the constant, the variable will be "USER.DELETE" or "USER" + ".DELETE" The constants are replaced in the code (this lead to fun thing with reflection) Commented Feb 7, 2017 at 12:39

4 Answers 4

2

First approach will create 3 String object instance in memory. The second approach will create 4 String object instance in memory.

Performance impact: There will not be any impact from performance point of view as string concatenation will be done at compile time in given scenario as value is already known.

Java spec: Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals. http://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.5

Memory Impact : There will be one extra string created inside the java heap memory space with second approach.

From code maintainability point of view I will go with second approach. Suppose later if we want to change .DELETE to .ASYNCDELETE. We have to make only one place change with second approach. But with first approach we have to make 3 modification.

Sign up to request clarification or add additional context in comments.

Comments

1

Actually there is no any difference. Compiler will make concatenation and store resulting string.

So choose according to your style.

3 Comments

I think snippet 1 will create 3 strings and Snippet 2 will create 4. Are you sure there won't be any change in memory/performance?
Yes, you're right about memory, final static will also be in second case. But result strings sX will be the same after compilation.
You answer is generic, there is not much explanation.
0

The second snippet will store 4 strings in memory while the first will store three. You'll "waste" the space required to store the ".DELETE". You have a good article about String concatenation here

Comments

0

Little difference in this scenario, as others described above.

However if the subject is of interest to you in a wider usage, for example if you were to be creating lots of strings dynamically based on more combinations of static data, check out the String intern() method. It helps use the string class as a factory so you'll get the same string object for the same string contents, hurts performance a bit but can save a lot of memory usage and garbage collection overhead if you're working with lots of data, and can also make hash lookups faster if you always intern the keys, in specific situations you can override equals and hashCode / comparators to only use the builtin Object '==' comparison, so the comparator does not need to compare the string contents.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.