Java FAQ


1. Always Use length() Instead of equals() to Check Empty String

In your day-to-day programming activities, you must be coming across multiple situation where you need to check if a string is empty. There are various ways to do this and some use string1.equals(“”). NEVER do this.

Best way to check if string is empty or not is to use length() method. This method simply return the count of characters inside char array which constitutes the string. If the count or length is 0; you can safely conclude that string is empty.

public boolean isEmpty(String str)
{
    return str.equals("");        //NEVER do this
}

public boolean isEmpty(String str)
{
    return str.length()==0;        //Correct way to check empty
}

If you want to know the reason then continue reading further.
Lets see the source code of both methods inside String.java class.

Method length()

public int length() {
    return count;
}

Method equals()

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = offset;
            int j = anotherString.offset;
            while (n-- != 0) {
                if (v1[i++] != v2[j++])
                    return false;
            }
            return true;
        }
    }
    return false;
}
Analysis

As you can see that length() method is simply a getter method which return the count of characters in array. So it practically does not waste much CPU cycles to compute the length of string. And any String with length 0 is always going to be empty string.

Whereas equals() method takes a lot of statements before concluding that string is empty. It does reference check, typecasting if necessary, create temporary arrays and then use while loop also. So, its lot of waste of CPU cycles to verify a simple condition.

Do let me know if you think otherwise.

Update: From java 6 onwards, isEmpty() function is available in String class itself. Please use this function directly.

Happy Learning !!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末委可,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件麸粮,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)撵割,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來辙芍,“玉大人啡彬,你說我怎么就攤上這事」使瑁” “怎么了外遇?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長契吉。 經(jīng)常有香客問我跳仿,道長,這世上最難降的妖魔是什么捐晶? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任菲语,我火速辦了婚禮妄辩,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘山上。我一直安慰自己眼耀,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布佩憾。 她就那樣靜靜地躺著哮伟,像睡著了一般。 火紅的嫁衣襯著肌膚如雪妄帘。 梳的紋絲不亂的頭發(fā)上楞黄,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天,我揣著相機(jī)與錄音抡驼,去河邊找鬼鬼廓。 笑死,一個胖子當(dāng)著我的面吹牛致盟,可吹牛的內(nèi)容都是我干的碎税。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼馏锡,長吁一口氣:“原來是場噩夢啊……” “哼雷蹂!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起杯道,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤匪煌,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后蕉饼,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體虐杯,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年昧港,在試婚紗的時候發(fā)現(xiàn)自己被綠了擎椰。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡创肥,死狀恐怖达舒,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情叹侄,我是刑警寧澤巩搏,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站趾代,受9級特大地震影響贯底,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜撒强,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一禽捆、第九天 我趴在偏房一處隱蔽的房頂上張望笙什。 院中可真熱鬧,春花似錦胚想、人聲如沸琐凭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽统屈。三九已至,卻和暖如春牙躺,著一層夾襖步出監(jiān)牢的瞬間愁憔,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工述呐, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留惩淳,地道東北人蕉毯。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓乓搬,卻偏偏與公主長得像,于是被迫代替她去往敵國和親代虾。 傳聞我的和親對象是個殘疾皇子进肯,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,747評論 0 33
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理棉磨,服務(wù)發(fā)現(xiàn)江掩,斷路器,智...
    卡卡羅2017閱讀 134,662評論 18 139
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,511評論 0 23
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 8,867評論 0 6
  • 記得讀書時乘瓤,中央電視臺財經(jīng)頻道有一檔節(jié)目《我為shopping狂》环形,妙趣橫生地介紹購物的種種環(huán)節(jié)。當(dāng)時迷戀至極衙傀,每...
    清淺光陰閱讀 258評論 0 0