0

I'm trying to understand is there any difference (performance/best programming practice..etc) between using a direct string vs creating a temporary variable in side a method and used it exactly once. Here is an example

Class Sample {
   public boolean compareString(String str){
     String test = "Test";
     return test.equalsIgnoreCase(str);
           vs
     return "Test".equalsIgnoreCase(str);
   }
}

In my opinion, both are the same. Instead of creating a variable, one can directly use it. Please let me if there are any other differences and which one is preferred?

14
  • 3
    @GiorgiTsiklauri that is not a duplicate. Commented Oct 13, 2020 at 21:02
  • In your example, both strings are from string pool; however, you're question leads to the one I marked as duplicate. Have a look on that. Commented Oct 13, 2020 at 21:02
  • 2
    To answer the question: No, there is no difference. Commented Oct 13, 2020 at 21:03
  • 1
    To be pedantic, there is one less line of code in the second. Commented Oct 13, 2020 at 21:04
  • 1
    While in your case there is no difference, imagine if test had a chance of not being initialized. test.equalsIgnoreCase(str) will throw a NullPointer, while the other way will not. Commented Oct 13, 2020 at 21:11

3 Answers 3

2

No difference, because string literals (like "one", "two") are stored into String Pool and reused.

From your example:

String test = "Test";
// "Test" - string literal is stored in the StringPool
// test - reference to string literal "Test" from StringPool

return test.equalsIgnoreCase(str);
return "Test".equalsIgnoreCase(str);
// in both cases you use the same string literal stored in StringPool

P.S.

String str = new String("abc");
// "abc" - string literal is created (or reused) and stored in StringPool
// new String() - string object is stored in heap and contains (separate copy) of string literal "abc"
// str - reference to the string object in heap

In case you want to get a refernce to the string from StringPool:

String str1 = str.intern();
// str1 - reference to the string literal from the String Pool
Sign up to request clarification or add additional context in comments.

1 Comment

"because strings are stored into String Pool" - wrong. Strings are stored into string pool if they're created as String literals.
1

Here is the difference.

String str = null;
            
String test = "Test";
boolean a =  test.equalsIgnoreCase(str);
            
boolean b =  "Test".equalsIgnoreCase(str);

The bytecode

2  ldc <String "Test"> [16] // push the constant on stack
4  astore_2 [test]  // stores in internal list
5  aload_2 [test]   // retrieves constant, pushes on stack
6  aload_1 [str]    // target of comparison
7  invokevirtual java.lang.String.equalsIgnoreCase(java.lang.String) : boolean [18]
10  istore_3 [a]     // store boolean result

11  ldc <String "Test"> [16] // push the constant on stack
13  aload_1 [str]    // target of comparison
14  invokevirtual java.lang.String.equalsIgnoreCase(java.lang.String) : boolean [18]
17  istore 4 [b]     // store boolean result

Comments

0

I was looking for any differences (peformance/programming practice.. etc) between 2 approaches and found that there are no differences.

2nd approach is better in best programming practice (it make sense to create a temporary variables if it is used in other places with in that method).

3 Comments

You should not think about "less number of lines of code" but about what the code and each line does. The solution with a temporary variable makes no sense not because it has one more line, but because the temporary variable is only used for one single statement right after.
@akuzminykh agree. in my real development example, the temporary variable is used only once right after it's declaration. it make sense to create a temporary variable if it used multiple times with in the method.
Adding a temporary variable can make sense even if it is only used once, for example if it improves readability. For the code in the question, using a static final constant would make more sense though.

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.