http://java67.blogspot.sg/2014/01/why-string-class-has-made-immutable-or-final-java.html ããããã¼ã«è¨³ããã
Why String Class is made Immutable or Final in Java - 5 Reasons
Stringã«ã¤ãã¦èãããªãJavaã®æ¡ç¨é¢æ¥ã¯ã»ã¨ãã©ç¡ã*1ããªãJavaã®Stringã¯ã¤ãã¥ã¼ã¿ãã«ãªã®ãï¼(Why String is Immutable in Java)ã¯ãã®æãããã®ã¨è¨ãã¾ãããªãJavaã®Stringã¯ã¤ãã¥ã¼ã¿ãã«ãªã®ãï¼(Why String is Immutable in Java)ã¯ãè¨ãæããã¨ããªãJavaã®Stringã¯ã©ã¹ã¯finalãªã®ãï¼(Why String class is made final in Java)ããããã¯åã«ããªãStringã¯finalãªã®ãï¼(Why String is final)ã¨ãªãã¾ãããããã®è³ªåã«çããã«ã¯ãJavaããã°ã©ãã¯Stringã®åä½ã«é¢ãã¦ããã®ã¯ã©ã¹ã®ç¹å¥ãªæ©è½ã¨åºæ¬çãªæ¦å¿µããååã«çè§£ãã¦ããå¿
è¦ãããã¾ããStringã¯Javaã§ã¯ç¥ã¯ã©ã¹(God class)ã§ãããã¤ãã®ä»ã¯ã©ã¹ã§ã¯å©ç¨åºæ¥ãªãç¹å¥ãªæ©è½ãæã¡ããã¨ãã°Stringãªãã©ã«ã¯ãã¼ã«ãããã+æ¼ç®åã§æååãé£çµã§ããããªã©ãããã¾ããJavaããã°ã©ãã³ã°ã«ãããæååã®éè¦æ§ãèæ
®ããJavaãã¶ã¤ãã¯Stringã¯ã©ã¹ãfinalã«ããããã¯java.lang.Stringãæ¡å¼µåºæ¥ãªããã¨ãæå³ããã¾ããStringãªãã¸ã§ã¯ããã¤ãã¥ã¼ã¿ãã«ã«ããã¦ãã¾ããæåã®è³ªåã«æ»ãã¾ãããªãJavaã®Stringã¯ã¤ãã¥ã¼ã¿ãã«ãªã®ãï¼(Why String is Immutable in Java?) å½ç¶ãªããå©ç¹ãã¡ãªãããããããã§ããããã§ã¯ããã®æ±ºå®ã«è³ã£ããå©ç¹ãæ©è½ã«ã¤ãã¦èãã¦ããã¾ãããããã®æ±ºå®ãè§£æå¯è½ãªOracleãSunã®å
¬å¼ããã¥ã¡ã³ãããããã©ããç§ã¯ç¥ãã¾ãããJavaã®ä½è
ã§ããJames GoslingãStringã¯ã©ã¹ãfinalã«ããäºã«ã¤ãã¦èãããéãã»ãã¥ãªãã£ã«é¢ãã¦çãã¦ãããã¨ãã©ããã§èªãã è¨æ¶ãããã¾ã*2ãã¯ã©ã¹ãfinalã«ããã¨æ¡å¼µãå¼·ãå¶éãããã¨ã«å¯¾ãå対æè¦ããããJamesã¯ãã®ç¹ã«ã¤ãã¦ã³ã¡ã³ããã¦ãããJavaã®ã»ãã¥ãªãã£ã®ä¿éã®ãã¼ã¨ãªãã¯ã©ã¹ã¯finalã«ãããã¨ã§*3ãæ¯ãèãã¨Javaãã©ãããã©ã¼ã ã«å¯¾ãã䏿£ãªæä½ã夿´ä¸å¯è½ã«ãªãã¾ã*4ã
5 Reasons of Why String is final or Immutable in Java
ãªãStringã¯ã©ã¹ãfinalã«ãªã£ãã®ãã®æ¬å½ã®çç±ã¯ãJames Goslingã示åããã»ãã¥ãªãã£é¢é£ã¯ã¨ãããã¨ãã¦ãJavaãã¶ã¤ããä¸çªè¯ãç¥ã£ã¦ãã¾ãããã®ããã以ä¸ã«è¿°ã¹ãçç±ã¯ãç§èªèº«ããªãJavaã®Stringãfinalãªããã¤ãã¥ã¼ã¿ãã«ãªã®ãããèå¯ãããã®ã«ãªãã¾ãã
1) String Pool
Javaãã¶ã¤ãã¯ãããã種é¡ã®Javaã¢ããªã±ã¼ã·ã§ã³ã§æãè¯ã使ããããã¼ã¿åã¯Stringã«ãªãã§ãããã¨äºæ¸¬ãã¦ããã®ã§ãå½åããæé©åã®è¦æ±ãããã¾ãããæåã®ã¢ã¤ãã¢ã¯Stringãã¼ã«ã«Stringãªãã©ã«ãæ ¼ç´ãããã¨ã§ããããªãã¸ã§ã¯ããå
±æãããã§ã䏿çãªStringãªãã¸ã§ã¯ãã忏ãããã¨ãç®çã§ããã®ããã«ã¯Immutable classã§ããå¿
è¦ãããã¾ããäºãã«ä¸å¯ç¥ãªäºã¤ã®é åã«ãããã¥ã¼ã¿ãã«ãªãªãã¸ã§ã¯ãã®å
±æã¯åºæ¥ã¾ãããä¾ãèãã¦ã¿ã¾ããäºã¤ã®åç
§å¤æ°ãåä¸ã®Stringãªãã¸ã§ã¯ããæãã¦ããã¨ãã¾ãã
String s1 = "Java"; String s2 = "Java";
ãã¾ãs1ã"Java"ãã"C++"ãªãã¸ã§ã¯ãã«å¤æ´ããã¨ãåç
§å¤æ°ã¯s2="C++"ã¨ãªãã¾ããStringãã¤ãã¥ã¼ã¿ãã«ã«ãããã¨ã§ãStringãªãã©ã«ã®å
±æãå¯è½ã¨ãªãã¾ããè¦ããã«ãStringãã¼ã«ã®ã¢ã¤ãã¢ã¯Stringãfinalãããã¯ã¤ãã¥ã¼ã¿ãã«ã«ããªããã°Javaã§ã¯å®è£
åºæ¥ã¾ããã
2) Security
Javaã¯ã©ã®ãããªãµã¼ãã¹ã¬ãã«ã«ããã¦ãã»ãã¥ãªãã£ç°å¢ãæä¾ããã¨ããæç¢ºãªç®æ¨ããããStringã¯ã»ãã¥ãªãã£å ¨ä½ã«ããã¦éè¦ã§ããStringã¯å¤æ°ã®Javaã¯ã©ã¹ã§ãã©ã¡ã¼ã¿ã¨ãã¦åºã使ããã¦ããããã¨ãã°ãããã¯ã¼ã¯æ¥ç¶ã®éå§ã®ãã¹ãã¨ãã¼ããããã¡ã¤ã«ã¨ãã£ã¬ã¯ããªã®ãã¹ã§Javaã§ãã¡ã¤ã«ãèªã¿è¾¼ãã ãããã¼ã¿ãã¼ã¹æ¥ç¶ã«æååã¨ãã¦URLãæ¸¡ãããããè¡ãã¾ããããStringãã¤ãã¥ã¼ã¿ãã«ã§ãªãã£ãã¨ããããã¦ã¼ã¶ã¯ã·ã¹ãã ã®ç¹å®ãã¡ã¤ã«ã¸ã®ã¢ã¯ã»ã¹è¨±å¯ãå¾ãå¾ã«PATHã夿´å¯è½ã§ãããã¯é大ãªã»ãã¥ãªãã£åé¡ãå¼ãèµ·ããã¾ããåæ§ã«ããããã¯ã¼ã¯ä¸ã®ãã·ã³ããã¼ã¿ãã¼ã¹ã¸ã®æ¥ç¶ä¸ã«ããã¥ã¼ã¿ãã«ãªæååå¤ã¯ã»ãã¥ãªãã£ä¸ã®è å¨ã«ãªãã¾ããã¾ãããã¥ã¼ã¿ãã«ãªæååã弿°ã«åããªãã¬ã¯ã·ã§ã³ã§ãåæ§ã«ã»ãã¥ãªãã£ä¸ã®åé¡ãå¼ãèµ·ããã¾ãã
3) Use of String in Class Loading Mechanism
Stringãfinalãªããã¤ãã¥ã¼ã¿ãã«ã«ããä»ã®çç±ã¨ãã¦ã¯ãclass loading mechanism.ã§é »ç¹ã«ä½¿ããããããã¨ããç¹ãããã¾ããStringãã¤ãã¥ã¼ã¿ãã«ã§ãªãã¨ããã¨ãæ»æè
ã«ã¯å©ç¹ãä¸ãããã¨ã«ãªããjava.io.Readerãªã©Javaã®æ¨æºã¯ã©ã¹ã®ãã¼ããæªæã®ããcom.unknown.DataStolenReaderã«å¤æ´ãå¯è½ã§ããStringãfinalãã¤ãã¥ã¼ã¿ãã«ã«ä¿ã¤ãã¨ã§ãå°ãªãã¨ãJVMã¯æ£ããã¯ã©ã¹ããã¼ãå¯è½ã§ãã
4) Multithreading Benefits
ã³ã³ã«ã¬ã³ã·ã¼ã¨ãã«ãã¹ã¬ããã¯Javaã®ä¸æ ¸æ©è½ãªã®ã§ãStringãªãã¸ã§ã¯ãã®ã¹ã¬ããå®å ¨æ§ã«ã¤ãã¦èæ ®ããã®ã¯å½ç¶ã®ãã¨ã¨ãªãã¾ãããStringã¯åºã使ãããããã«ãªããã¨ãäºæ¸¬ãããã®ã§ãã¤ãã¥ã¼ã¿ãã«ã«ãããã¨ã§å¤é¨ããã®åæåã®å¿ è¦ãç¡ãããè¤æ°ã¹ã¬ããéã§Stringãå ±æããã³ã¼ããã¯ãªã¼ã³ã«ãªãã¾ãããã®æ©è½ã«ããè¤éã§ã¨ã©ã¼ã«ãªããããã³ã³ã«ã¬ã³ã·ã¼ãªã³ã¼ããç°¡æåããã¾ããStringã¯ã¤ãã¥ã¼ã¿ãã«ãªã®ã§ã¹ã¬ããéã§å ±æå¯è½ã§ãçµæã¨ãã¦èªã¿ãããã³ã¼ãã«ãªãã¾ãã
5) Optimization and Performance
ã¯ã©ã¹ãã¤ãã¥ã¼ã¿ãã«ã«ããå ´åã®å©ç¹ã¯ãã¯ã©ã¹ãä¸åº¦çæãããã¨å¤æ´ãä¸å¯è½ã«ãªãç¹ã§ãããã®ãã¨ã¯ãã£ãã·ã¥ãªã©ã®å¤ãã®ããã©ã¼ãã³ã¹æé©åã®éãéããã¨ã«ãªãã¾ããStringèªèº«ã夿´ãããªããã¨ãç¥ã£ã¦ããã®ã§ãStringã¯ããã·ã¥ã³ã¼ãããã£ãã·ã¥ãã¾ããããã·ã¥ã³ã¼ãã®è¨ç®ã¯é
å»¶ãã¦è¡ãããä¸åº¦ä½æãããã¨ãã£ãã·ã¥ããã¾ããåç´ãªå ´åã§ã¯ãStringãªãã¸ã§ã¯ãã®hashCode()ã¡ã½ããã®ååå¼ã³åºãæã«ããã·ã¥ã³ã¼ããè¨ç®ããã¦ããã以éã®hashCodeã¯è¨ç®æ¸ã¿ã®ãã£ãã·ã¥å¤ãè¿ãã¾ããããã«ãããStringã¯Hashtableã¨HashMapãªã©Mapãã¼ã¹ã§ããã·ã¥ãé »ç¹ã«ä½¿ãããå ´åã«ã¯ããã©ã¼ãã³ã¹ãåä¸ãã¾ããããã·ã¥ã³ã¼ãã®ãã£ãã·ã¥ã¯ãStringèªèº«ã®å
容ã«ä¾åããã®ã§ãã¤ãã¥ã¼ã¿ãã«ããã³finalç¡ãã«ã¯ä¸å¯è½ã§ãã
Pros and Cons of String being Immutable or Final in Java
ä¸è¿°ã®å©ç¹ã«ãããJavaã§Stringãfinalã«ãããã¨ã«ããããä¸ã¤ã®å©ç¹ãçã¾ãã¾ããæååã¯HashMapã¨Hashtableãªã©ããã·ã¥ãã¼ã¹ã®ã³ã¬ã¯ã·ã§ã³ã®ãã¼ã¨ãã¦æã使ããã¾ããã¤ãã¥ã¼ã¿ãã«ã¯HashMapãã¼ã®å¿
é è¦æ±ã§ã¯ããã¾ãããããã¥ã¼ã¿ãã«ãããã¤ãã¥ã¼ã¿ãã«ãªãã¸ã§ã¯ããä½¿ãæ¹ãããå®å
¨ã§ããã®çç±ã¯ããããã¥ã¼ã¿ãã«ãªãã¸ã§ã¯ãã®ç¶æ
ãHashMapå
ã§å¤æ´ãããå ´åãequals()ã¨hashCode()ã¡ã½ããã¯å¤æ´å¾ã®å±æ§ã«åºã¥ãã®ã§ãå
ã«æ»ãã®ã¯å°é£ã§ããã¯ã©ã¹ãã¤ãã¥ã¼ã¿ãã«ãªå ´åãããã·ã¥ãã¼ã¹ã³ã¬ã¯ã·ã§ã³å
ã«æ ¼ç´æã«ãç¶æ
夿´ã®ãªã¹ã¯ã¯ããã¾ãããå¥ã®éè¦ãªå©ç¹ã¨ãã¦ã¯ãæ¢ã«ã¹ã¬ããå®å
¨æ§ã®ã¨ããã§è§¦ãã¾ãããStringã¯ã¤ãã¥ã¼ã¿ãã«ãªã®ã§ãå¤é¨ããã®åæåãèæ
®ãããã¨ãªãã¹ã¬ããéã§å®å
¨ã«å
±æå¯è½ã§ããããã«ããã³ã³ã«ã¬ã³ããªã³ã¼ãã®å¯èªæ§ãé«ã¾ãã¨ã©ã¼ã®å¯è½æ§ãæ¸ããã¾ãã
å¤ãã®å©ç¹ãããã¾ãããã¤ãã¥ã¼ã¿ãã«ã«ã¯æ¬ ç¹ããããä¾ãã°ã³ã¹ãããããã¾ããStringã¯ã¤ãã¥ã¼ã¿ãã«ãªã®ã§ä¸æçã«ä½¿ç¨ãããªãã¸ã§ã¯ãã夿°çæããGCã«å½±é¿ãä¸ãã¾ããJavaãã¶ã¤ãããã®ç¹ã¯èªèãã¦ãããã¼ã«ã«æ ¼ç´ããStringãªãã©ã«ã«ããStringã®ã´ããªãã¸ã§ã¯ãã忏ã§ããã¨èãã¦ãã¾ãããããããStringãã¼ã«ãããªãã¸ã§ã¯ããåå¾ããªãnew String()ã³ã³ã¹ãã©ã¯ã¿ã使ããã«Stringãçæããããæ³¨æããå¿
è¦ãããã¾ããä¸è¬çãªJavaã¢ããªã±ã¼ã·ã§ã³ã¯å¤æ°ã®ãªãã¸ã§ã¯ããçæãã¾ãããã¼ã«ã«æ ¼ç´ããStringã¯GCã«é¢é£ãã¦é ãããªã¹ã¯ãããã¾ããStringãã¼ã«ã¯Java Heapã®ãJava Heapã«æ¯ã¹ã¦ããªãå¶éããããPermGen空éã«é
ç½®ããã¾ãã夿°ã®Stringãªãã©ã«ãæã¤ã¨ããã«ç©ºéãæºããã¦ãã¾ããçµæã¨ãã¦java.lang.OutOfMemoryError: PermGen Spaceãå¼ãèµ·ããã¾ããããããããã¨ã«ãJavaè¨èªã®ããã°ã©ãã¯ãã®åé¡ãèªèãã¦ããJava 7以éã§ã¯Stringãã¼ã«ã¯é常ã®ãã¼ã空éã«ç§»åããã¾ããããã¡ãã¯PermGenã«æ¯ã¹ãã¨é常ã«å·¨å¤§ã§ããStringãfinalã«ããå¥ã®æ¬ ç¹ã¨ãã¦ã¯ãæ¡å¼µæ§ã®å¶éã§ããæ»
å¤ã«å¿
è¦ã«ãªããªãã¨ã¯ãããæ©è½æ¡å¼µã®ããã«Stringãæ¡å¼µãããã¨ã¯åºæ¥ããjava.lang.Stringã¯ã©ã¹ãæ¡å¼µãããã¨ãæã人ã
ã«ã¨ã£ã¦ã¯ä¾ç¶ã¨ãã¦å¶éäºé
ã®ä¸ã¤ã§ãã
ä¸è¨5ã¤ã®çç±ã¯ééããªããªãJavaã®Stringã¯ã©ã¹ã¯finalã§ã¤ãã¥ã¼ã¿ãã«ãªã®ã(Why String class has been made Final and Immutable in Java)ã«å¯¾ãããã³ãã«ãªãã¾ããOf-course it's decision of Java designers but looks like above points contributes to take them this decision.*5åæ§ãªçç±ã«ããInteger, Long, Double, Floatãªã©ã®ã©ããã¼ã¯ã©ã¹ãã¤ãã¥ã¼ã¿ãã«ã§finalã§ãã
*1:https://twitter.com/ttddyy/status/651296322079838208 ãå ã«ä¿®æ£
*2:https://twitter.com/cocoa_ruto/status/650984456530989056 ãåèã«ä¿®æ£
*3:https://twitter.com/cocoa_ruto/status/650984389589864449 ãåèã«è¨³ãä¿®æ£
*4:https://twitter.com/cocoa_ruto/status/650987487469301760 ãåºã«ä¿®æ£
*5:ãããããã