简体   繁体   中英

Is assigning to String variable costly in terms of memory and time?

String timeStamp = currentCommentObjectObj.getTimeStamp();

holder.timeStamp.setText(timeStamp);

or

holder.timeStamp.setText(currentCommentObjectObj.getTimeStamp());

Which is better from a time and space optimisation perspective?

More information :- This code is inside onBindViewHolder of recycler view.

Although I prefer the second one, I believe that there is no difference. Because somewhere in the compilation steps the compiler optimizes your code and would recognize such discrepancies if any.

Please refer to http://www.noesispoint.com/jsp/scjp/SCJPch0.htm for more information.

Apparently first javac (the Java Compiler) compiles the code to JavaByteCode and then the Java Virtual Machine's compiler JIT optimizes and compiles the Byte code to the machines language.

Hope that helps.

Regardless of the compiler/JVM behavior, this is an operation that should be essentially instantaneous.

On a machine level, the only possible difference between the two methods is

(a) saving a pointer to memory, then loading the pointer, or

(b) passing the pointer directly to the next method call.

The amount of time that is different between these two is so small that it essentially will never matter, for any Object. Even in a loop, Android UI code should not be executed enough times for this to ever possibly matter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM