Effective Java ѧϰ±Ê¼Ç£¨5£©
¾¡Á¿¸´ÓöÔÏ󣬶ø²»ÊÇ´´½¨ÐµĶÔÏó£¬ÌرðÊǵ±Ò»¸ö¶ÔÏóÊÇimmutable£¨²»¿É¸Ä±ä£©µÄʱºò¡£
ÈçString¶ÔÏó£¬
String s= new String(“String”); ǧÍò²»ÒªÕâÑù×ö£¬ÒòΪÕâÀïʵ¼ÊÉÏ´´½¨ÁËÁ½¸ö¶ÔÏó¡£
Òª±ÜÃâ³öÏÖÕâÑùµÄÇé¿ö£¬
1ÊÇ¿ÉÒÔÓþ²Ì¬¹¤³§º¯Êý£¬À´½â¾ö£¬ÈçÀà¿âÖеÄBoolean.valueOf(“true”)£¬ÕâÖÖ·½Ê½ÒªÓÅÓÚnew Boolean(“true”);
2.ÊǾ¡Á¿¸´ÓöÔÏó¡£
¿´ÊéÖеÄÀý×Ó¡£
public class Person {
private final Date birthDate;
public boolean isBabyBoomer() {
Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
Date boomStart = gmtCal.getTime();
gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
Date boomEnd = gmtCal.getTime();
return birthDate.compareTo(boomStart) >= 0 && birthDate.compareTo(boomEnd) < 0;
}
}
ÕâÀÿ´Îµ÷ÓÃisBabyBoomer¾Í»á´´½¨Á½¸öData¶ÔÏó£¬Ò»¸öCalendar¶ÔÏóºÍTimeZone¶ÔÏó.
¸Ä½ø°æ£º
class Person {
private final Date birthDate;
private static final Date BOOM_START;
&nb
Ïà¹ØÎĵµ£º
Beanshell ¡¾Java¿ªÔ´ ¶¯Ì¬ÓïÑÔ¡¿
BeanshellÊÇÓÃJavaд³ÉµÄ£¬Ò»¸öСÐ͵ġ¢Ãâ·ÑµÄ¡¢¿ÉÒÔÏÂÔصġ¢Ç¶ÈëʽµÄJavaÔ´´úÂë½âÊÍÆ÷£¬¾ßÓжÔÏó½Å±¾ÓïÑÔÌØÐÔ¡£BeanShellÖ´Ðбê×¼JavaÓï¾äºÍ±í´ïʽ£¬ÁíÍâ°üÀ¨Ò»Ð©½Å±¾ÃüÁîºÍÓï·¨¡£Ëü½«½Å±¾»¯¶ÔÏó¿´×÷¼òµ¥±Õ°ü·½·¨£¨simple method closure£©À´Ö§³Ö£¬¾ÍÈçͬÔÚPerlºÍJavaScri ......
ÔÚ Java ³ÌÐòÖÐʹÓöàÏß³ÌÒª±ÈÔÚ C »ò C++ ÖÐÈÝÒ׵ö࣬ÕâÊÇÒòΪ Java ±à³ÌÓïÑÔÌṩÁËÓïÑÔ¼¶µÄÖ§³Ö¡£±¾ÎÄͨ¹ý¼òµ¥µÄ±à³ÌʾÀýÀ´ËµÃ÷ Java ³ÌÐòÖеĶàÏß³ÌÊǶàôֱ¹Û¡£¶ÁÍê±¾ÎÄÒÔºó£¬Óû§Ó¦¸ÃÄܹ»±àд¼òµ¥µÄ¶àÏ̳߳ÌÐò¡£
¡¡¡¡ÎªÊ²Ã´»áÅŶӵȴý£¿
¡¡¡¡ÏÂÃæµÄÕâ¸ö¼òµ¥µÄ Java ³ÌÐòÍê³ÉËÄÏî²»Ïà¹ØµÄÈÎÎñ¡£ÕâÑùµÄ³ÌÐòÓе¥¸ö¿ØÖ ......
½ñÌìÔÚ¹äÂÛ̳µÄʱºò¿´µ½ÓÐÈ˽ò½òÀÖµÀ£º´ÓC++¸Éµ½×ªÐÍΪjavaÊÇÍ˲½
ÎÒ²»ÈÏΪÊÇÕâÑù£¬Ê×ÏÈÓ¦¸ÃÒ»¸öÈ˵ÄÐËȤ£¬¼ÓÉÏÕâÃÅÓïÑÔ£¬ÕâÃŹ¤×÷ÊÇ·ñ·ûºÏÄã
¸É×Ô¼ºÏ²»¶µÄÓïÑÔ±àÂë²ÅÊÇ×îÖØÒªµÄ£¬µ±È»java±È½ÏÈÝÒ׵㣬ÎÒÒ²ÊÇÕâÑùÈÏΪµÄ
µ«ÊÇjava²¢²»ÊÇ´ó¼ÒËù˵µÄÄÇôÈÝÒ×£¬µ±½ñÉç»á˲»¸Ò˵×Ô¼º»ássh£¬µ±ÊÇ×Ô¼ºÎÊÐÄ×Ô¼ºÑ§µÃÓжàºÃ£¬
ÔÚ ......
ÓÐʱ£¬ÎÒÃÇÔÚдһ¸ö¹¹Ô캯Êýʱ£¬¾³£ÒòΪËü°üº¬ÖÚ¶àµÄ²ÎÊý¶ø¿àÄÕ,Õâʱ¿ÉÒÔ¿¼ÂÇÓÃBuilderģʽÀ´´´½¨¶ÔÏó¡£
È磬ÎÒÃÇÒªÉè¼ÆÒ»¸öÓªÑø³É·ÝµÄÀ࣬°üº¬ÄÜÁ¿£¬µ°°×ÖÊ£¬Ö¬·¾£¬¸Æ£¬Ìú£¬Ð¿£¬Î¬ÉúËØA, άÉúËØB1 ... µÈ£¬µ«ÔÚ¹¹ÔìµÄʱºò£¬²»Ò»¶¨Ã¿´Î¶¼ÐèÒªÕâЩ²ÎÊý£¬Èç¸Æ£¬Ìú£¬Ð¿ºÍάÉúËصÈÊÇ¿ÉÑ¡µÄ£¬ÎªÁËÊÊÓ¦¶àÖÖ¿ÉÄܵĴîÅ䣬±È½ÏÔʼ ......
Õâ¸öÔÔòºÜ¼òµ¥£¬¾ÍÊÇʹÓÃ˽Óй¹Ô캯Êý·ÀÖ¹À౻ʵÀý»¯¡£
ÓÐʱ£¬ÎÒÃÇ»áдһЩ¹¤¾ßÀ࣬Èçjava.lang.Math »òÊÇ java.util.Arrays, ÕâЩÀàÖж¼ÊǾ²Ì¬º¯Êý£¬ÔÚÉè¼ÆµÄʱºò£¬ÕâЩÀಢ²»ÊÇÓÃÀ´±»ÊµÀý»¯µÄ¡£Õâ¸öʱºò£¬¾ÍºÜÓбØÒª°Ñ¹¹Ô캯Êý˽Óл¯¡£
public class UtilityClass {
private UtilityClass() {
throw new Assertion ......