对于字符串比较,我的习惯用法是 变量.equals(常量)
比如: a.equals("a")
今天看视频才知道变量在前面与后面有很大影响,正确的写法是常量放前面(可以避免变量为null引起的异常),变量放后面,则上面需要改为("a").equals(a)。
对于JDK API上字符串的equals的官方解释是:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+YAAAEHCAIAAACshct+AAAgAElEQVR4nO3d7XHjuNYuUAXljLpqqt5gOoebhLOYCE4yuj88o6EJYGMDpGRQvVa5TkkUiC+C5NM8suf2/wAAgIXd/g8AAFjY7Q4AACxMZAcAgKVdILL/7/fH7Xb79fnT/QAAgJ+wcmT/yur/ENkBAPgzrRzZ//H5S2QHAODPJbIDAMDSRHYAAFhaMrJ/+1r57eP3/1of/vr8/PVPgf82f8Xtx/v/dv9ebVnz/X4PInuxt1wPAMD7yUT2r8z8b5r+/u7f3Pz1/t8Q/fh0F7e/Pv9e0+PT7x/umy/S+G7z569G4gcAgEvrR/YiSf+Ty7/C8i7A70uHkX2vkbrrkT2uCgAA3kU/sv/zLLzw8ft/xYPyici+/3ZL+in7t575RgwAAO8qG9kbj7OLD0ci+/5rNENP2YsCnrkDAPCWsl+M+RaZ//f74/uX16eeshdxfzay1xoGAIA3kf/108bD8G9fjWn8+um3773vIvu/Wfyx68fHLnlXI/tX8c1GfwoSAID3lPwjj7svtH9PxpsPP3792j3s3u756/f3v/P4/XvsX38fssz0W/9VXPuKvcAOAMD7Ofs/peT7KQAAcKpTI/v2ybfUDgAAZzj7KTsAAHAqkR0AAJYmsgMAwNJEdgAAWJrIDgAASxPZAQBgaSI7AAAsTWQHAICliewAALA0kR0AAJYmsgMAwNJEdgAAWJrIDgAASxPZAQBgaSI7AAAsTWQHAICliewAALA0kR0AAJYmsgMAwNJEdgAAWJrIDgAASxPZAQBgaSI7AAAsTWQHAICliex/qNvNoT/KHAIAr3Fa5rjdbtUEc/tX9aMTGxqt4Q3y1m1jdMdvb//6+/bX3623ld3DT19sN/zubJx73N9gFQEA63tRfFkwJ71N2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M19sy3bZEdgAA5iydIFuhfPvd8WCX3etgr235fFgvE3m38wAAMGrdTDn6fL1bLH4qf/wB/K2mWyEAAMRkSgAAWJrIDgAASxPZAQBgaSI7AAAsTWQHAICliewAALA0kf1t3f76+6e7cL8v0w1aFjlAi3QDANYksl/A7a+/tz/JXVo1TPdhbseD+z5PfkIm5v9cz2vdOgGASxDZL+Arymz/N7lL/vVTrRzF8n37qVE89cjkussAAAIUSURBVJBZJwBwCSL7ZUyHy9aOr0xIy6axxSP7Uxu1TgDgKkT2iurXA3Zbql+WCL5B0apw+6JV8rG91c9MydYAu13K9/zemLpql0bFs9rqdrx7tWP5IbRaP/HoB/OWmZCJsbdqu8o6AYC3JLLv7TJHd+P2f4d2vzeiTPV1/DaTe5LxqJrtqk2UowuC5sEoNnpQMhu7b5MlWwf0lKPfmrdnjD3Tq6BYq4lXrhMAeFci+143iExE9sfb8jFk2VDyAeRoFOvuFe+baf2pkb2clomEmsmj+TAat5gvUPbwtnk+XfYkGFF1gMkV1froQusEAN6VyL73pMieyZSt1qsfLRjZ7+m8O2r0oCQLBB3LHJo4BM9F9u6WuP5MK6MfXWidAMC7Etn38qkrH9nnQlscLufqmfhXRL71gfh7u91uA2tv9KDcv4fCU2Y1k/WfcfSfsbHbn0XWCQDwRWSvuCV+f/T2/asL27fx7vmScxsfH1Wbro40Lhn0vDUtqXQ7EtmDmlsd3o0unv9qsWAGql0ampYTj/7cinp81O3S/UfXCQBwF9lPt07seH1PdgEu7sZoZM83/RrVwV7RIlN36TkEgGcT2c8UPM78QyRn4PS8/iPT7nBPM3UAMERk/2b3f+77ecaPg+In8/OMExwALkpkBwCApYnsAACwNJEdAACWJrIDAMDSRHYAAFiayA4AAEsT2QEAYGn/H5fct9UJnTWEAAAAAElFTkSuQmCC" alt="" />
比如:
public class equalsTest {
@SuppressWarnings("rawtypes")
public static void main(String args[]) {
List list = new ArrayList();
String str = null;
System.out.println("0".equals(str));
System.out.println(str.equals("0"));
System.out.println("0".equals(list.get(2)));
System.out.println(list.get(2).equals("0"));
}
}
上面依次输出:
false
java.lang.NullPointerException
java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
对于判断是否为空 str!=null,这个应该变量放前面后面一般没影响,养成好的习惯的话变量也放在后面。一般对一个字符串进行判断首先进行非空判断,然后进行字符串内容判断,一个正确的例子:
null !=string && !("").equals(string.trim()) && !("null").equals(string);