{"id":128,"date":"2007-09-15T02:54:38","date_gmt":"2007-09-14T18:54:38","guid":{"rendered":"http:\/\/learn-house.idv.tw\/?p=128"},"modified":"2010-01-17T19:16:36","modified_gmt":"2010-01-17T11:16:36","slug":"javaautoboxing%e8%88%87unboxing","status":"publish","type":"post","link":"https:\/\/learn-house.idv.tw\/?p=128","title":{"rendered":"[Java]autoboxing\u8207unboxing"},"content":{"rendered":"<p><strong>\u5167\u5bb9\u4f86\u6e90<\/strong><a href=\"http:\/\/caterpillar.onlyfun.net\/Gossip\/JavaGossip-V1\/AutoBoxingUnBoxing.htm\" target=\"_blank\">\u826f\u845b\u683c\u5b78\u7fd2\u7b46\u8a18<\/a>&nbsp;&nbsp;&nbsp;  <strong>(\u6b64\u7bc7\u6587\u7ae0\u50c5\u4f9b\u7ad9\u9577\u5099\u4efd\u53c3\u8003\u7528\uff0c\u56fa\u70ba\u52a0\u5bc6\u6587\u7ae0)<\/strong><br \/>&nbsp; <\/p>\n<p>\u5728Java\u4e2d\uff0c\u6240\u6709\u60a8\u8981\u8655\u7406\u7684\u6771\u897f\u300c\u5e7e\u4e4e\u300d\u90fd\u662f<span style=\"font-weight: bold\">\u7269\u4ef6\uff08Object\uff09<\/span>\uff0c\u4f8b\u5982\u60a8\u4e4b\u524d\u6240\u4f7f\u7528\u7684Scanner\u662f\u7269\u4ef6\uff0c\u5b57\u4e32\uff08String\uff09\u662f\u7269\u4ef6\uff0c\u60a8\u4e4b\u5f8c\u9084\u6703\u770b\u5230\u66f4\u591a\u7684\u7269\u4ef6\u3002<\/p>\n<p>    \u7136\u800c\u6709\u4e9b\u662f\u4f8b\u5916\uff0c\u57fa\u672c\uff08Primitive\uff09\u8cc7\u6599\u578b\u614b\u4e0d\u662f\u7269\u4ef6\uff0c\u4e5f\u5c31\u662f\u60a8\u4f7f\u7528int\u3001double\u3001boolean\u7b49\u5ba3\u544a\u7684\u8b8a\u6578\uff0c\u4ee5\u53ca\u60a8\u5728\u7a0b\u5f0f\u4e2d\u76f4\u63a5\u5beb\u4e0b\u7684\u5b57\u9762\u5e38\u91cf\u3002<\/p>\n<p>    \u4f7f\u7528Java\u6709\u4e00\u6bb5\u6642\u9593\u7684\u4eba\u90fd\u77e5\u9053\uff0c\u6709\u4e9b\u6642\u5019\u60a8\u9700\u8981\u5c07\u57fa\u672c\u578b\u614b\u8f49\u63db\u70ba\u7269\u4ef6\uff0c\u4f8b\u5982\u4f7f\u7528Map\u6642\uff0c\u8981\u4f7f\u7528put()\u65b9\u6cd5\u6642\u9700\u8981\u7684\u662f\u7269\u4ef6\u800c\u4e0d\u662f\u57fa\u672c\u578b\u614b\u3002<\/p>\n<p>\u60a8\u8981\u4f7f\u7528<span style=\"font-weight: bold\">\u5305\u88f9\u578b\u614b\uff08Wrapper Types\uff09<\/span>\u624d\u80fd\u5c07\u57fa\u672c\u8cc7\u6599\u578b\u614b\u5305\u88dd\u70ba\u7269\u4ef6\uff0c\u4f8b\u5982\u5728 <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/download.html\" target=\"_blank\">J2SE 1.4.2<\/a>&nbsp;\u4e4b\u524d\uff0c\u60a8\u8981\u5982\u4e0b\u624d\u80fd\u5c07int\u5305\u88dd\u70ba\u4e00\u500bInteger\u7269\u4ef6\uff1a                         <\/p>\n<div style=\"margin-left: 40px\"><span style=\"font-weight: bold; font-family: Courier New,Courier,monospace\">Integer integer = new Integer(10);<\/span>          <\/div>\n<p>    \u5728 <a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/download.jsp\" target=\"_blank\">J2SE 5.0<\/a> \u4e4b\u5f8c\u60a8\u53ef\u4ee5\u9019\u9ebc\u5beb\uff1a                <\/p>\n<div style=\"margin-left: 40px\"><span style=\"font-weight: bold; font-family: Courier New,Courier,monospace\">Integer integer = 10;<\/span>          <\/div>\n<p>    \u4e8b\u5be6\u4e0a\u7de8\u8b6f\u5668\u5728\u80cc\u5f8c\u81ea\u52d5\u6839\u64da\u60a8\u5beb\u4e0b\u7684\u9673\u8ff0\uff0c\u70ba\u60a8\u9032\u884c<span style=\"font-weight: bold\">\u81ea\u52d5\u88dd\u7bb1\uff08Autoboxing\uff09<\/span>\u52d5 \u4f5c\uff0c\u540c\u6a23\u7684\u52d5\u4f5c\u53ef\u4ee5\u9069\u7528\u65bc boolean\u3001byte\u3001short\u3001char\u3001long\u3001float\u3001double\u7b49\u57fa\u672c\u578b\u614b\uff0c\u5206\u5225\u6703\u4f7f\u7528\u5c0d\u61c9\u7684\u5305\u88f9\u578b\u614b\uff08Wrapper Types\uff09Boolean\u3001Byte\u3001Short\u3001Character\u3001Integer\u3001Long\u3001Float\u6216Double\u3002<\/p>\n<p>    \u81ea\u52d5\u88dd\u7bb1\u904b\u7528\u7684\u65b9\u6cd5\u9084\u53ef\u4ee5\u5982\u4e0b\uff1a                   <\/p>\n<div style=\"margin-left: 40px\"><span style=\"font-weight: bold; font-family: Courier New,Courier,monospace\">int i = 10;<\/span><br style=\"font-weight: bold; font-family: Courier New,Courier,monospace\" \/>        <span style=\"font-weight: bold; font-family: Courier New,Courier,monospace\">Integer integer = i;<\/span>        <\/div>\n<p>        &nbsp;<br \/>    \u60a8\u4e5f\u53ef\u4ee5\u4f7f\u7528\u66f4\u4e00\u822c\u5316\u7684Number\uff0c\u4f8b\u5982\uff1a                <\/p>\n<div style=\"margin-left: 40px\"><span style=\"font-weight: bold; font-family: Courier New,Courier,monospace\">Number number = 3.14f;<\/span>          <\/div>\n<p>    3.14f\u6703\u5148\u88ab\u81ea\u52d5\u88dd\u7bb1\u70baFloat\uff0c\u7136\u5f8c\u6307\u5b9a\u7d66number\u3002<\/p>\n<p>    J2SE 5.0\u4e2d\u53ef\u4ee5\u81ea\u52d5\u88dd\u7bb1\uff0c\u4e5f\u53ef\u4ee5<span style=\"font-weight: bold\">\u81ea\u52d5\u62c6\u7bb1\uff08Unboxing\uff09<\/span>\uff0c\u4f8b\u5982\u4e0b\u9762\u9019\u6a23\u5beb\u662f\u53ef\u4ee5\u7684\uff1a                   <\/p>\n<div style=\"margin-left: 40px\"><span style=\"font-weight: bold; font-family: Courier New,Courier,monospace\">Integer fooInteger = 10;<\/span><br style=\"font-weight: bold; font-family: Courier New,Courier,monospace\" \/>          <span style=\"font-weight: bold; font-family: Courier New,Courier,monospace\">int fooPrimitive = fooInteger;<\/span>          <\/div>\n<p>          &nbsp;<br \/>    \u5728\u904b\u7b97\u6642\uff0c\u4e5f\u53ef\u4ee5\u81ea\u52d5\u88dd\u7bb1\u8207\u62c6\u7bb1\uff0c\u4f8b\u5982\uff1a                <\/p>\n<div style=\"margin-left: 40px\"><span style=\"font-weight: bold; font-family: Courier New,Courier,monospace\">Integer i = 10;<\/span><br style=\"font-weight: bold; font-family: Courier New,Courier,monospace\" \/>          <span style=\"font-weight: bold; font-family: Courier New,Courier,monospace\">System.out.println(i + 10);<\/span><br style=\"font-weight: bold; font-family: Courier New,Courier,monospace\" \/>          <span style=\"font-weight: bold; font-family: Courier New,Courier,monospace\">System.out.println(i++);<\/span>          <\/div>\n<p>    \u4e0a\u4f8b\u4e2d\u6703\u986f\u793a20\u820710\uff0c\u7de8\u8b6f\u5668\u6703\u81ea\u52d5\u5e6b\u60a8\u9032\u884c\u81ea\u52d5\u88dd\u7bb1\u8207\u62c6\u7bb1\uff0c\u537310\u6703\u5148\u88ab\u88dd\u7bb1\uff0c\u7136\u5f8c\u5728i + 10\u6642\u6703\u5148\u62c6\u7bb1\uff0c\u9032\u884c\u52a0\u6cd5\u904b\u7b97\uff1bi++\u8a72\u884c\u4e5f\u662f\u5148\u62c6\u7bb1\u518d\u9032\u884c\u905e\u589e\u904b\u7b97\u3002\u518d\u4f86\u770b\u4e00\u500b\u4f8b\u5b50\uff1a                <\/p>\n<div style=\"margin-left: 40px\"><span style=\"font-weight: bold; font-family: Courier New,Courier,monospace\">Boolean boo = true;<\/span><br style=\"font-weight: bold; font-family: Courier New,Courier,monospace\" \/>          <span style=\"font-weight: bold; font-family: Courier New,Courier,monospace\">System.out.println(boo &amp;&amp; false);<\/span>          <\/div>\n<p>    \u540c\u6a23\u7684\uff0c\u5148\u5c07boo\u62c6\u7bb1\uff0c\u518d\u8207false\u9032\u884cAND\u904b\u7b97\uff0c\u7d50\u679c\u6703\u986f\u793afalse\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5167\u5bb9\u4f86\u6e90\u826f\u845b\u683c\u5b78\u7fd2\u7b46\u8a18&nbsp;&nbsp;&nbsp; (\u6b64\u7bc7\u6587\u7ae0\u50c5\u4f9b\u7ad9\u9577\u5099\u4efd\u53c3\u8003\u7528\uff0c\u56fa\u70ba\u52a0\u5bc6\u6587\u7ae0)&#038;nb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-128","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/128"}],"collection":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=128"}],"version-history":[{"count":0,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/128\/revisions"}],"wp:attachment":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}