0

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,

asked Dec 2, 2013 at 16:58
1
  • 1
    a String name ia a reference not an object. However, your debug messages burns a lot of performance if it's not actually logged. You should check that debug logging is on before you create the String it throws away. Commented Dec 2, 2013 at 19:27

6 Answers 6

2

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.

answered Dec 2, 2013 at 17:09
Sign up to request clarification or add additional context in comments.

3 Comments

Local variables do not reside on any stack. If they did, you could not randomly access them. Also, the JVM will most likely optimize away any differences of the two examples.
local variables do reside on thred's local cal stack. If not then where would they?
Inside a random access array from which you can push or pull values onto the operand stack. docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.6
2

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

answered Dec 2, 2013 at 17:18

1 Comment

Thanks a lot Ben i was looking for this info :)
0

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.

answered Dec 2, 2013 at 17:05

Comments

0

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.

answered Dec 2, 2013 at 17:06

Comments

0

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.

answered Dec 2, 2013 at 17:08

Comments

0
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.

answered Dec 2, 2013 at 17:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.