直接使用字符串 vs 在本地方法中创建变量的用法

huangapple go评论93阅读模式
英文:

usage of direct strings vs creating a variable in local method

问题

我正在努力理解的是,在使用直接字符串与在方法内部创建临时变量并仅使用一次之间,是否存在任何区别(性能/最佳编程实践等)。这里有一个示例:

  1. Class Sample {
  2. public boolean compareString(String str){
  3. String test = "Test";
  4. return test.equalsIgnoreCase(str);
  5. vs
  6. return "Test".equalsIgnoreCase(str);
  7. }
  8. }

在我看来,两者是相同的。可以直接使用字符串,而不是创建变量。如果存在其他差异,请告诉我哪种方法更可取。

英文:

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

  1. Class Sample {
  2. public boolean compareString(String str){
  3. String test = "Test";
  4. return test.equalsIgnoreCase(str);
  5. vs
  6. return "Test".equalsIgnoreCase(str);
  7. }
  8. }

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?

答案1

得分: 2

没有任何区别,因为字符串字面量(例如"one""two")被存储在字符串池中并被重用。

从你的例子中:

  1. String test = "Test";
  2. // "Test" - 字符串字面量被存储在字符串池中
  3. // test - 引用指向字符串字面量 "Test" 在字符串池中
  4. return test.equalsIgnoreCase(str);
  5. return "Test".equalsIgnoreCase(str);
  6. // 在这两种情况下,你都在使用存储在字符串池中的相同字符串字面量

附:

  1. String str = new String("abc");
  2. // "abc" - 字符串字面量被创建(或重用),并存储在字符串池中
  3. // new String() - 字符串对象被存储在堆中,包含字符串字面量 "abc" 的(单独的副本)
  4. // str - 引用指向堆中的字符串对象
  5. 如果你想从字符串池中获取引用
  6. ```java
  7. String str1 = str.intern();
  8. // str1 - 引用指向字符串池中的字符串字面量
英文:

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

From your example:

  1. String test = "Test";
  2. // "Test" - string literal is stored in the StringPool
  3. // test - reference to string literal "Test" from StringPool
  4. return test.equalsIgnoreCase(str);
  5. return "Test".equalsIgnoreCase(str);
  6. // in both cases you use the same string literal stored in StringPool

P.S.

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

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

  1. String str1 = str.intern();
  2. // str1 - reference to the string literal from the String Pool

答案2

得分: 1

这里是不同之处。

  1. String str = null;
  2. String test = "Test";
  3. boolean a = test.equalsIgnoreCase(str);
  4. boolean b = "Test".equalsIgnoreCase(str);

字节码部分

  1. 2 ldc <String "Test"> [16] // 将常量推入堆栈
  2. 4 astore_2 [test] // 存储到内部列表中
  3. 5 aload_2 [test] // 检索常量,推入堆栈
  4. 6 aload_1 [str] // 比较的目标
  5. 7 invokevirtual java.lang.String.equalsIgnoreCase(java.lang.String) : boolean [18]
  6. 10 istore_3 [a] // 存储布尔结果
  7. 11 ldc <String "Test"> [16] // 将常量推入堆栈
  8. 13 aload_1 [str] // 比较的目标
  9. 14 invokevirtual java.lang.String.equalsIgnoreCase(java.lang.String) : boolean [18]
  10. 17 istore 4 [b] // 存储布尔结果
英文:

Here is the difference.

  1. String str = null;
  2. String test = &quot;Test&quot;;
  3. boolean a = test.equalsIgnoreCase(str);
  4. boolean b = &quot;Test&quot;.equalsIgnoreCase(str);

The bytecode

  1. 2 ldc &lt;String &quot;Test&quot;&gt; [16] // push the constant on stack
  2. 4 astore_2 [test] // stores in internal list
  3. 5 aload_2 [test] // retrieves constant, pushes on stack
  4. 6 aload_1 [str] // target of comparison
  5. 7 invokevirtual java.lang.String.equalsIgnoreCase(java.lang.String) : boolean [18]
  6. 10 istore_3 [a] // store boolean result
  7. 11 ldc &lt;String &quot;Test&quot;&gt; [16] // push the constant on stack
  8. 13 aload_1 [str] // target of comparison
  9. 14 invokevirtual java.lang.String.equalsIgnoreCase(java.lang.String) : boolean [18]
  10. 17 istore 4 [b] // store boolean result

答案3

得分: 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).

huangapple
  • 本文由 发表于 2020年10月14日 04:59:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64343088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定