Encyclopedia
Encyclopedia

Reputation: 144

String performance- memory

While writing code i came across an interesting problem.

I have a PersonPOJO with name as one of its String members with its getters and setters

class PersonPOJO {

private String name;

public setName(Name) {
this.name = Name;
}
public getName() {
return name;
}
}  

Now i am using it in Test class. There are two approach of using String getters in it.

Approach 1 :-

class Test1 {

............
String name = personPojo.getName();
logger.debug("....."+name);
if (name.equals("ABC")) {
....
}
}

Approach 2 :-

class Test2 {
.............
logger.debug("...."+personPojo.getName());
if (personPojo.getName.equals("ABC")) {
..
}
}

Thus in second approach i am not creating intermediate String variable. Will not creating an extra String variable helps in performance like no extra String object creation , less load on GC etc. Please explain in detail which approach is better ?

Thanks,

Upvotes: 0

Views: 131

Answers (6)

Ben
Ben

Reputation: 1177

Since the other answers already answered your question, some additional information regarding strings in Java:

Strings in java are special. There are 2 ways of creatings Strings:

1. implicit via string literal

String literal = "Some text";

and

2. explicit via new()

String explicit = new String("Some text");

While string literals are kept in a so called string common pool, string objects created via new are kept in the heap like any other object. This means if you have three different string literals

String literal1 = "Test";
String literal2 = "Test";
String literal3 = "Test";

and each of em have the same content, they all share the same storage inside the string common pool.

Like I mentioned just some additional information, but always good to know. :D

Upvotes: 2

Trying
Trying

Reputation: 14278

String name = personPojo.getName();
logger.debug("....."+name);
if (name.equals("ABC"))


logger.debug("...."+personPojo.getName());
if (personPojo.getName.equals("ABC")) {

Both cases are same w.r.t. performance is concern. Because by String name = personPojo.getName(); you are not creating a new object rather you are just creating a new reference to the same string Object.

May be String name = personPojo.getName(); is little bit better because you not calling the function again and again rather using a local variable. Here it may not have much impact, may be negligible but it good not to call the same function again and again.

Upvotes: 0

Nazgul
Nazgul

Reputation: 1902

there is nothing extra getting created anywhere except the "ABC" literal that you have in your if check. See the 'name' is just a reference to a String object to which personPojo.getName() points. So in terms of memory there are no dents.

However calling personPojo.getName() again and again in second example does have a performance hit as compared to option 1. Local variables reside on stack and are often the fastest to access as compared to getting an object form heap and then calling a method on it.

Upvotes: 2

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44032

You are not creating an object in either example. Neither are you altering an object. You are merely creating a local variable reference.

As for the created reference, the difference is not as big as you might think. The Java run time will optimize this additional reference away eventually and even if it was not doing that, you would never notice the difference. When writing code, do not think about those things. Rather think about the readability of your code.

Upvotes: 0

TwoThe
TwoThe

Reputation: 14269

The only point where you create an intermediate String variable is your debug code. What you do is a reference copy, which is about the same as writing:

int myStringRef = personPojo.getName();

And that really doesn't take any significant time to execute, especially not since the JVM is likely to remove that extra variable anyways. Hint: If you want to make it even more likely, add a final in front of it.

If you want to improve it even further, make the PersonPOJO object immutable by declaring the name as final and remove the setter.

And then both suggestions will increase speed by such a low amount that even with the best tool you wouldn't be able to notice it.

Upvotes: 0

Vineet Kasat
Vineet Kasat

Reputation: 1014

In both the approaches you are not creating any new String object. When you say :

String name = personPojo.getName();

name is a reference to personPojo.name Object on heap. It doesnt creates intermediate string. Also this reference "name" becomes eligible for GC as program control goes out of scope

Thus both the cases are similar performance wise and memory wise.

Upvotes: 0

Related Questions