java string isempty,java – String.isEmpty()和String.equals(“”)之间的区别
我認為isEmpty()更有效率。然而,智能編譯器可能會優(yōu)化equals(“”)調(diào)用。從
OpenJDK source:
671 public boolean isEmpty() {
672 return count == 0;
673 }
1013 public boolean equals(Object anObject) {
1014 if (this == anObject) {
1015 return true;
1016 }
1017 if (anObject instanceof String) {
1018 String anotherString = (String)anObject;
1019 int n = count;
1020 if (n == anotherString.count) {
1021 char v1[] = value;
1022 char v2[] = anotherString.value;
1023 int i = offset;
1024 int j = anotherString.offset;
1025 while (n-- != 0) {
1026 if (v1[i++] != v2[j++])
1027 return false;
1028 }
1029 return true;
1030 }
1031 }
1032 return false;
1033 }
還有answer here關(guān)于是否使用str.isEmpty()或“”.equals(str)是現(xiàn)成的:
The main benefit of "".equals(s) is you don’t need the null check (equals will check its argument and return false if it’s null), which you seem to not care about. If you’re not worried about s being null (or are otherwise checking for it), I would definitely use s.isEmpty(); it shows exactly what you’re checking, you care whether or not s is empty, not whether it equals the empty string
總結(jié)
以上是生活随笔為你收集整理的java string isempty,java – String.isEmpty()和String.equals(“”)之间的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java quartz tomcat_Q
- 下一篇: 标准C语言各种时间函数用法,使用c语言标