开发者

how to test a string in both emulator 1.6 and 2.2 in android as same

开发者 https://www.devze.com 2023-03-25 13:42 出处:网络
Hi guys I have a problem in testing a string while running on emulator1.6 and emulator2.2 in android . My problem is as开发者_Go百科 follows.

Hi guys I have a problem in testing a string while running on emulator1.6 and emulator2.2 in android . My problem is as开发者_Go百科 follows.

String phone="";


if(phone!=""&&!phone.equals(" ")){

}

In the android emulator1.6 it is working fine (i.e.,not entering into the if). But in the emulator2.2 it is entering into the if statement and executing the code. Please help me immediate response is needed.


String phone="";


if((!phone.equals(""))&&(!phone.equals(" "))){
    //
}


What ever it is.. You should never use "!=" or "==" comparison for Strings.. Thats because it compares the object value or the reference where as .equals compares the Actual Value/content of String.. so always use .equals


What you wrote will never enter in if block regardless of emulator/sdk version.Because it is the issue of java,not android.and what you wrote phone!=""&&!phone.equals(" ") is always false


I would guess that the earlier Android version must be doing some optimization:

Because the 'phone' variable and the literal String "" have the same value and are never changed, it's making them point at the same object to save memory, which means that it will pass an equality (==) check.

As others have said, you shouldn't ever be using == for String comparison.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号