日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java string isempty,java – String.isEmpty()和String.equals(“”)之间的区别

發布時間:2025/3/12 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java string isempty,java – String.isEmpty()和String.equals(“”)之间的区别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我認為isEmpty()更有效率。然而,智能編譯器可能會優化equals(“”)調用。從

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關于是否使用str.isEmpty()或“”.equals(str)是現成的:

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

總結

以上是生活随笔為你收集整理的java string isempty,java – String.isEmpty()和String.equals(“”)之间的区别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。