Effective Java ѧϰ±Ê¼Ç(9)
ͨ³£,ÓÐÒ»ÀàÎÊÌâµÄ²úÉú¶¼ÊÇÓÉÓÚûÓÐÖØÔØhashCodeÕâ¸ö·½·¨. µ±ÄãÖØÔØequalsºó,hashCode±ØÐëÖØÔØ,·ñÔòÔÚʹÓÃHashMap, HashSet, HashtableµÄʱºò,»á³ö´í.
¶ÔÓÚhashCodeÓм¸¸öÔÔò.
1. ²»¹Üʲôʱºòµ÷ÓÃhashCode,Èç¹ûͬһ¸ö¶ÔÏóÓëhashCodeÏà¹ØµÄ×Ö¶ÎûÓÐÐ޸ĵĻ°,Õâ¸ö·½·¨µÄ·µ»ØÖµ±ØÐëÏàͬ. µ«²»±ØºÍÁíÒ»¸öÓ¦ÓóÌÐòÖеÄÖµÏàµÈ.
2. µ±Á½¸ö¶ÔÏóequalsµÄʱºò,ÆähashCodeÒ²±ØÐëÏàͬ.
3.ûÓÐÇ¿ÖÆÁ½¸ö²»Ò»ÑùµÄ¶ÔÏó²»ÄÜ·µ»ØÏàͬµÄhashCode, µ«ÊÇҪעÒâµÄ,Èç¹ûHash±íÖгåͻ̫¶à,»á½µµÍ³ÌÐòÐÔÄÜ.
Ò»¸öºÃµÄhashCodeº¯Êý£¬ÄܶԲ»Í¬µÄ¶ÔÏ󷵻ز»Í¬µÄÖµ¡£Ð´Ò»¸öºÃµÄhashCode²»ÊÇÏëÏñµÄÄÇôÀ§ÄÑ£¬Èç¹û²ÎÕÕÒÔϵķ½·¨µÄ»°£º
1. ´¢´æÒ»¸ö·ÇÁã³£Á¿£¬Èç17£¬×÷Ϊ·µ»ØÖµ¡£
2. ¶Ôÿһ¸öÓбȽÏÒâÒåµÄ³ÉÔ±£¬²ÎÕÕÒÔÏ£º
a. ¼ÆËãÊý×ÖÐ͵Ähash code
i. Èç¹ûÊÇboolean, ¼ÆËã(f?1:0)
ii. Èç¹ûÊÇbyte char,short or int ¼ÆË㣬(int)f
iii. Èç¹ûÊÇlong, ¼ÆËã(int) ( f ^ (f >>>32))
iv. Èç¹ûÊÇfloat, ¼ÆËãFloat.floatToIntBits(f);
v. Èç¹ûÊÇdouble, ¼ÆËãDouble.doubleToLongBits(f), µÃµ½³¤ÕûÐÍÊý×ÖÇë²Î¼û iii.
vi. Èç¹ûÊǸö¶ÔÏóµÄÒýÓ㬾͵÷ÓÃÕâ¸ö¶ÔÏóµÄhashcode
vii,Èç¹ûÊÇÊý×飬¾Í¶Ôÿ¸öÔªËØ°´ÉÏÊöÔÔò×ö¡£
b. ¼ÆËãhashCode °´ÒÔϹ«Ê½¡£
result = 31 * result + c; // cÊÇ ÉÏÃæ¼ÆËãµÄÖµ£¬Ñ¡Ôñ31ÓÐÁ½¸öÔÒò£¬Ò»ÊÇËü×ã¹»´óµÄËØÊý£¬¶þÊÇËüÄܹ»±»ÐéÄâ»úÓÅ»¯ i * 31 = ( i << 5 -1)
3. ·µ»ØÖµ¡£
4. д²âÊÔÓÃÀý²âÊÔ¡£
ҪעÒâµÄÊÇ£¬Äã¿ÉÒÔÔÚhashCodeÖкöÂÔһЩֵ£¬µ«ÊÇͬÑù£¬ÕâЩֵҲ²»ÄÜÔÚequalsÖÐÒýÓá£
@Override public int hashCode() {
int result = 17;
result = 31 * result + areaCode;
result = 31 * result + prefix;
result = 31 * result + lineNumber;
return result;
}
Èç¹ûÒ»¸ö¶ÔÏóÊDz»¿É±äµÄ£¬Ôò¿É²ÉÓÃÁíÒ»ÖÖ·½Ê½¡£
private volatile int hashCode;
@Override public int hashCode() {
int result = hashCode;
if (result == 0) {
&nb
Ïà¹ØÎĵµ£º
³õѧJAVA£¬ºÃ²»ÈÝÒ×½â¾öÁËÕâ¸öÊý¾Ý¿âÁ¬½ÓÎÊÌ⣬Õâ¸öÖ÷ÒªÊÇÓÐÁ½¸öÎÊÌ⣬һ¸öÊÇJDBC-odbcµÄÎÊÌ⣬Èç¹ûûÓнâ¾öºÃÊý¾ÝÔ´ÅäÖ㬾ø¶ÔÊÇÓÐÈÝÒ׳öÎÊÌâµÄ£¬¼ÙÉèÄãʹÓõÄÊÇaccessµÄÊý¾Ý¿â£¬ÕâÊÇÍøÉϵÄÒ»¸öÀý×ÓµÄÓï¾ä
String dburl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=database.mdb";
Æäʵ²¢²»ÐèÒªÄÇô³¤£¬¼Ù ......
7£®1 Swing¼ò½é
7.1.1 ¼ò½é
¡¡¡¡µÚÎå½²ÖÐÎÒÃÇѧϰÁËAWT£¬AWTÊÇSwingµÄ»ù´¡¡£SwingµÄ²úÉúÖ÷ÒªÔÒò¾ÍÊÇAWT²»ÄÜÂú×ãͼÐλ¯Óû§½çÃæ·¢Õ¹µÄÐèÒª¡£
AWTÉè¼ÆµÄ³õÖÔÊÇÖ§³Ö¿ª·¢Ð¡Ó¦ÓóÌÐòµÄ¼òµ¥Óû§½çÃæ¡£ÀýÈçAWTȱÉÙ¼ôÌù°å¡¢´òÓ¡Ö§³Ö¡¢¼üÅ̵¼º½µÈÌØÐÔ£¬¶øÇÒÔÀ´µÄAWTÉõÖÁ²»°üÀ¨µ¯³öʽ²Ëµ¥»ò¹ö¶¯´°¸ñµÈ»ù±¾ÔªËØ¡£
¡¡¡¡´ËÍâAWT»¹´ ......
£¨²Î¿¼ “ibm nioÈëÃÅ”£©
ÔÚ NIO ¿âÖУ¬ËùÓÐÊý¾Ý¶¼ÊÇÓûº³åÇø´¦ÀíµÄ¡£ÔÚ¶ÁÈ¡Êý¾Ýʱ£¬ËüÊÇÖ±½Ó¶Áµ½»º³åÇøÖеġ£ÔÚдÈëÊý¾Ýʱ£¬ËüÊÇдÈëµ½»º³åÇøÖеġ£ÈκÎʱºò·ÃÎÊ NIO ÖеÄÊý¾Ý£¬¶¼Êǽ«Ëü·Åµ½»º³åÇøÖС£»º³åÇøʵÖÊÉÏÊÇÒ»¸öÊý×顣ͨ³£ËüÊÇÒ»¸ö×Ö½ÚÊý×飬µ«ÊÇÒ²¿ÉÒÔʹÓÃÆäËûÖÖÀàµÄÊý×é¡£µ«ÊÇÒ»¸ö»º³åÇø²»½ö½öÊÇÒ» ......
Ò»¡¢
ΪʲôҪʹÓÃ
JSP
ÔÚ¿Í»§¶ËÍøÒ³±à³ÌÖÐ
Java
µÄ½â¾ö·½°¸ÊÇ
Servlet/JSP
¡£¶ÔÓÚ¶¯Ì¬Ò³ÃæµÄÐγÉÎÒÃÇ¿ÉÒÔÀûÓÃÔÚ
Servlet
Öбàд×Ö·û´®ÊµÏÖ£¬µ«ÊÇÕâÑù»áʹ³ÌÐòÔ±µÄ¹¤×÷±äµÃÒì³£µÄ·±Ëö£¬ËùÒÔÎÒÃDzÉÓÃ
JSP
´ïµ½ÍøÒ³µÄ¶¯Ì¬ÏÔʾ¡£
¶þ¡¢
ʲôÊÇ
JSP
JSP
£¨
JavaServerPage
£©ºÍ
Servlet
ÊÇ
Java
ÖвúÉú¶¯Ì ......