{"id":234,"date":"2020-11-14T14:25:49","date_gmt":"2020-11-14T14:25:49","guid":{"rendered":"https:\/\/vukieuanh.com\/?p=234"},"modified":"2020-11-14T14:25:49","modified_gmt":"2020-11-14T14:25:49","slug":"lop-wrapper-va-kieu-nguyen-thuy","status":"publish","type":"post","link":"https:\/\/vukieuanh.net\/index.php\/2020\/11\/14\/lop-wrapper-va-kieu-nguyen-thuy\/","title":{"rendered":"L\u1edbp Wrapper v\u00e0 ki\u1ec3u nguy\u00ean th\u1ee7y"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Danh s\u00e1ch l\u1edbp Wrapper t\u01b0\u01a1ng \u1ee9ng v\u1edbi ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-3a88641f wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><\/p>\n<\/div>\n<\/div>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Ki\u1ec3u g\u1ed1c<\/th><th>L\u1edbp Wrapper<\/th><\/tr><\/thead><tbody><tr><td>boolean<\/td><td>Boolean<\/td><\/tr><tr><td>char<\/td><td>Character<\/td><\/tr><tr><td>byte<\/td><td>Byte<\/td><\/tr><tr><td>short<\/td><td>Short<\/td><\/tr><tr><td>int<\/td><td>Integer<\/td><\/tr><tr><td>long<\/td><td>Long<\/td><\/tr><tr><td>float<\/td><td>Float<\/td><\/tr><tr><td>double<\/td><td>Double<\/td><\/tr><\/tbody><\/table><figcaption>B\u1ea3ng n\u00e0y \u1edf \u0111\u00e2u c\u0169ng c\u00f3, m\u00ecnh \u0111i cop kh\u00f4ng bi\u1ebft c\u00f3 c\u1ea7n nghi ngu\u1ed3n ko nh\u1ec9<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">L\u1edbp Wrapper cho ph\u00e9p chuy\u1ec3n c\u00e1c gi\u00e1 tr\u1ecb nguy\u00ean th\u1ee7y th\u00e0nh ki\u1ec3u tham chi\u1ebfu, t\u1eeb \u0111\u00f3 c\u00e1c gi\u00e1 tr\u1ecb c\u00f3 th\u1ec3 nh\u1eadn gi\u00e1 tr\u1ecb null v\u00e0 d\u1ec5 d\u00e0ng thao t\u00e1c h\u01a1n.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Java Compiler c\u00f3 2 c\u01a1 ch\u1ebf chuy\u1ec3n \u0111\u1ed5i gi\u1eefa 2 d\u1ea1ng n\u00e0y. <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>C\u01a1 ch\u1ebf <code>autoboxing<\/code> l\u00e0 c\u01a1 ch\u1ebf chuy\u1ec3n \u0111\u1ed5i ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y sang Wrapper t\u01b0\u01a1ng \u1ee9ng: <\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Integer a = 1;\ns\u1ed1 1 l\u00e0 ki\u1ec3u s\u1ed1 nguy\u00ean int \u0111\u01b0\u1ee3c chuy\u1ec3n th\u00e0nh m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng Wrapper Integer t\u01b0\u01a1ng \u1ee9ng.<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>C\u01a1 ch\u1ebf <code>unboxing<\/code> l\u00e0 c\u01a1 ch\u1ebf chuy\u1ec3n \u0111\u1ed5i \u0111\u1ed1i t\u01b0\u1ee3ng thu\u1ed9c l\u1edbp Wrapper sang ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Integer a = 5;\nint c = a;\nbi\u1ebfn a l\u00e0 ki\u1ec3u thu\u1ed9c l\u1edbp Wrapper, khi \u0111\u01b0\u1ee3c g\u00e1n v\u1edbi bi\u1ebfn c n\u00f3 t\u1ef1 \u0111\u1ed9ng \u0111\u01b0\u1ee3c unboxing th\u00e0nh ki\u1ec3u nguy\u00ean th\u1ee7y int.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">V\u00ec 2 c\u01a1 ch\u1ebf n\u00e0y ta ho\u00e0n to\u00e0n so s\u00e1nh \u0111\u01b0\u1ee3c 2 gi\u00e1 tr\u1ecb thu\u1ed9c 2 ki\u1ec3u nguy\u00ean th\u1ee7y v\u00e0 wrapper t\u01b0\u01a1ng \u1ee9ng khi ch\u00fang c\u00f3 gi\u00e1 tr\u1ecb b\u1eb1ng nhau.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">N\u00f3 v\u1eabn \u0111\u00fang k\u1ec3 c\u1ea3 v\u1edbi tr\u01b0\u1eddng h\u1ee3p sau: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    int x = 1;\n    Integer a = new Integer(1);\n    System.out.println(\"x == a: \" + (x == a));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Tuy nhi\u00ean, khi b\u1ea1n th\u1ef1c hi\u1ec7n t\u1ea1o ra 2 \u0111\u1ed1i t\u01b0\u1ee3ng thu\u1ed9c l\u1edbp Wrapper nh\u01b0 sau:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        Integer a = new Integer(5);\n        Integer b = new Integer(5);\n        System.out.println(a==b);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Ph\u00e9p so s\u00e1nh tr\u00ean s\u1ebd tr\u1ea3 v\u1ec1 false v\u00ec bi\u1ebfn a v\u00e0 b l\u01b0u gi\u00e1 tr\u1ecb \u00f4 nh\u1edb c\u1ee7a 2 v\u00f9ng nh\u1edb kh\u00e1c nhau tr\u00ean Heap.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Trong tr\u01b0\u1eddng h\u1ee3p n\u00e0y b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng<code> a.equals(b)<\/code> \u0111\u1ec3 so s\u00e1nh gi\u00e1 tr\u1ecb <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Integer a = new Integer(5);\nInteger b = new Integer(5);\nSystem.out.println(a.equals(b));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">tuy nhi\u00ean ki\u1ec3u d\u1eef li\u1ec7u Wrapper c\u0169ng c\u00f3 m\u1ed9t s\u1ed1 \u0111\u1eb7c bi\u1ec7t nh\u01b0 sau: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> Integer a = 1;\n Integer b = 1;\n Integer c = 128;\n\nInteger d = 128;\n\/\/ d\u00f2ng l\u1ec7nh sau cho k\u1ebft qu\u1ea3 true\nSystem.out.println(\"a == b: \" + (a == b));\n\/\/ d\u00f2ng l\u1ec7nh sau cho k\u1ebft qu\u1ea3 false\nSystem.out.println(\"c == d: \" + (c == d));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Tr\u01b0\u1eddng h\u1ee3p c\u1ee7a nh\u1eefng s\u1ed1 nguy\u00ean kh\u00f4ng n\u1eb1m trong kho\u1ea3ng t\u1eeb -128 \u0111\u1ebfn 127 s\u1ebd \u0111\u1ec1u nh\u1eadn k\u1ebft qu\u1ea3 false. C\u00f3 th\u1ec3 gi\u1ea3i th\u00edch nh\u01b0 sau. JVM c\u00f3 m\u1ed9t v\u00f9ng nh\u1edb trong Heap cho s\u1ed1 nguy\u00ean (\u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 caches) \u0111\u1ec3 l\u01b0u c\u00e1c gi\u00e1 tr\u1ecb \u0111\u01b0\u1ee3c autoboxing t\u1eeb wrapper. \u0110\u1ed1i v\u1edbi ki\u1ec3u Integer th\u00ec caches s\u1ebd l\u01b0u c\u00e1c gi\u00e1 tr\u1ecb trong kho\u1ea3ng t\u1eeb \u201c-128\u201d \u0111\u1ebfn \u201c+127\u201d. V\u00ec v\u1eady v\u1edbi nh\u1eefng s\u1ed1 kh\u00f4ng n\u1eb1m trong kho\u1ea3ng \u0111\u00f3 n\u00f3 ko th\u1ec3 autoboxing \u0111\u01b0\u1ee3c n\u00ean k\u1ebft qu\u1ea3 c\u1ee7a ph\u00e9p so s\u00e1nh tr\u1ea3 v\u1ec1 false.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Danh s\u00e1ch l\u1edbp Wrapper t\u01b0\u01a1ng \u1ee9ng v\u1edbi ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y Ki\u1ec3u g\u1ed1c L\u1edbp Wrapper boolean Boolean char Character byte Byte short Short int Integer long Long float Float double Double B\u1ea3ng n\u00e0y \u1edf \u0111\u00e2u c\u0169ng c\u00f3, m\u00ecnh \u0111i cop kh\u00f4ng bi\u1ebft c\u00f3 c\u1ea7n nghi ngu\u1ed3n ko nh\u1ec9 L\u1edbp Wrapper cho ph\u00e9p chuy\u1ec3n c\u00e1c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-234","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/posts\/234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/comments?post=234"}],"version-history":[{"count":10,"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/posts\/234\/revisions"}],"predecessor-version":[{"id":264,"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/posts\/234\/revisions\/264"}],"wp:attachment":[{"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/media?parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/categories?post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vukieuanh.net\/index.php\/wp-json\/wp\/v2\/tags?post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}