0

As known String is immutable in Java. I have the following method's body which return String:

Partner partner = context.getComponent(ComponentNames.PARTNER_COMPONENT_NAME);
String lastAccesDate = partner.getLastAccessDate();
if(lastAccesDate == null) {
    return "";
}
lastAccesDate = new SimpleDateFormat(DATE_PATTERN).format(); //1
return lastAccesDate;

The thing is because of string immutability, a new String object will be created at //1, so actually I'll have two String Objects, the first one contains partner.getLastAccessDate();, the second one new SimpleDateFormat(DATE_PATTERN).format();. The overhead is not good, how can I avoid it?

4
  • Use StringBuilder instead of String Commented Dec 24, 2014 at 11:19
  • @Sharpedge Why don't a StringBuffer? One's mutable, so we can modify the string it contains. Commented Dec 24, 2014 at 11:20
  • from java docs "Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger. As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization." Commented Dec 24, 2014 at 11:22
  • What does lastAccesDate = new SimpleDateFormat(DATE_PATTERN).format(); do? The format method must have a Date as an argument. Also, although it is true that a new String is created when using the format (I looked at the Java source code) this is not because of String immutability, but despite immutability. It is theoretically possible that the format method would call String.intern(String) in order to save memory since the String cannot be changed anyway. Commented Dec 24, 2014 at 12:46

2 Answers 2

1

Use StringBuffer in case of multithreading(i.e. if you need a thread-safe, mutable sequence of character) otherwise use StringBuilder

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

2 Comments

This method is from the web application, so I do use StringBuffer, don't I?
@St.Antario it depends if you are creating the Object inside the method something like public void foo() {StringBuilder sb=//....} then StringBuilder is the choice to go because the object will be created on stack and every thread will have it own so no problem. But if you need to declare the object outside the method i.e. on the heap then StringBuffer is must
0

see when you assign second time string to the String object lastAccessDate, there is no overhead as automaticaly garbage collector will free the space which occupied by first object because no object has reference to the same. so no need to worry about overhead

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.