英文:
usage of direct strings vs creating a variable in local method
问题
我正在努力理解的是,在使用直接字符串与在方法内部创建临时变量并仅使用一次之间,是否存在任何区别(性能/最佳编程实践等)。这里有一个示例:
Class Sample {
public boolean compareString(String str){
String test = "Test";
return test.equalsIgnoreCase(str);
vs
return "Test".equalsIgnoreCase(str);
}
}
在我看来,两者是相同的。可以直接使用字符串,而不是创建变量。如果存在其他差异,请告诉我哪种方法更可取。
英文:
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?
答案1
得分: 2
没有任何区别,因为字符串字面量(例如"one"
,"two"
)被存储在字符串池中并被重用。
从你的例子中:
String test = "Test";
// "Test" - 字符串字面量被存储在字符串池中
// test - 引用指向字符串字面量 "Test" 在字符串池中
return test.equalsIgnoreCase(str);
return "Test".equalsIgnoreCase(str);
// 在这两种情况下,你都在使用存储在字符串池中的相同字符串字面量
附:
String str = new String("abc");
// "abc" - 字符串字面量被创建(或重用),并存储在字符串池中
// new String() - 字符串对象被存储在堆中,包含字符串字面量 "abc" 的(单独的副本)
// str - 引用指向堆中的字符串对象
如果你想从字符串池中获取引用:
```java
String str1 = str.intern();
// str1 - 引用指向字符串池中的字符串字面量
英文:
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
答案2
得分: 1
这里是不同之处。
String str = null;
String test = "Test";
boolean a = test.equalsIgnoreCase(str);
boolean b = "Test".equalsIgnoreCase(str);
字节码部分
2 ldc <String "Test"> [16] // 将常量推入堆栈
4 astore_2 [test] // 存储到内部列表中
5 aload_2 [test] // 检索常量,推入堆栈
6 aload_1 [str] // 比较的目标
7 invokevirtual java.lang.String.equalsIgnoreCase(java.lang.String) : boolean [18]
10 istore_3 [a] // 存储布尔结果
11 ldc <String "Test"> [16] // 将常量推入堆栈
13 aload_1 [str] // 比较的目标
14 invokevirtual java.lang.String.equalsIgnoreCase(java.lang.String) : boolean [18]
17 istore 4 [b] // 存储布尔结果
英文:
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
答案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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论