{"id":83642,"date":"2024-12-31T21:06:42","date_gmt":"2024-12-31T14:06:42","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=83642"},"modified":"2024-12-31T21:06:42","modified_gmt":"2024-12-31T14:06:42","slug":"bien-trong-java","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/bien-trong-java\/","title":{"rendered":"Bi\u1ebfn trong Java: Chi ti\u1ebft h\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng \u0111\u00fang c\u00e1ch"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">N\u1ed9i dung b\u00e0i vi\u1ebft<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itviec.com\/blog\/bien-trong-java\/#Bien_trong_Java_la_gi_Cach_khai_bao_va_khoi_tao_bien_trong_Java\" >Bi\u1ebfn trong Java l\u00e0 g\u00ec? C\u00e1ch khai b\u00e1o v\u00e0 kh\u1edfi t\u1ea1o bi\u1ebfn trong Java<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itviec.com\/blog\/bien-trong-java\/#Quy_tac_dat_ten_bien_trong_Java\" >Quy t\u1eafc \u0111\u1eb7t t\u00ean bi\u1ebfn trong Java<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itviec.com\/blog\/bien-trong-java\/#Phan_loai_bien_trong_Java\" >Ph\u00e2n lo\u1ea1i bi\u1ebfn trong Java<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/itviec.com\/blog\/bien-trong-java\/#Cac_kieu_du_lieu_cua_bien_trong_Java\" >C\u00e1c ki\u1ec3u d\u1eef li\u1ec7u c\u1ee7a bi\u1ebfn trong Java<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itviec.com\/blog\/bien-trong-java\/#Cac_tu_khoa_lien_quan_den_bien_trong_Java\" >C\u00e1c t\u1eeb kh\u00f3a li\u00ean quan \u0111\u1ebfn bi\u1ebfn trong Java<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/itviec.com\/blog\/bien-trong-java\/#Pham_vi_va_thoi_gian_song_cua_bien_trong_Java\" >Ph\u1ea1m vi v\u00e0 th\u1eddi gian s\u1ed1ng c\u1ee7a bi\u1ebfn trong Java<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/itviec.com\/blog\/bien-trong-java\/#Cac_loi_thuong_gap_khi_su_dung_bien_trong_Java\" >C\u00e1c l\u1ed7i th\u01b0\u1eddng g\u1eb7p khi s\u1eed d\u1ee5ng bi\u1ebfn trong Java<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/itviec.com\/blog\/bien-trong-java\/#Meo_va_luu_y_khi_lam_viec_voi_bien\" >M\u1eb9o v\u00e0 l\u01b0u \u00fd khi l\u00e0m vi\u1ec7c v\u1edbi bi\u1ebfn<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/itviec.com\/blog\/bien-trong-java\/#Tong_ket\" >T\u1ed5ng k\u1ebft\u00a0<\/a><\/li><\/ul><\/nav><\/div>\n<p><em><strong>Bi\u1ebfn l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m c\u1ed1t l\u00f5i trong l\u1eadp tr\u00ecnh Java, \u0111\u00f3ng vai tr\u00f2 l\u01b0u tr\u1eef v\u00e0 qu\u1ea3n l\u00fd d\u1eef li\u1ec7u trong su\u1ed1t qu\u00e1 tr\u00ecnh th\u1ef1c thi ch\u01b0\u01a1ng tr\u00ecnh. Vi\u1ec7c hi\u1ec3u r\u00f5 b\u1ea3n ch\u1ea5t c\u1ee7a bi\u1ebfn gi\u00fap l\u1eadp tr\u00ecnh vi\u00ean khai th\u00e1c t\u1ed1i \u0111a kh\u1ea3 n\u0103ng c\u1ee7a Java \u0111\u1ec3 x\u00e2y d\u1ef1ng c\u00e1c \u1ee9ng d\u1ee5ng linh ho\u1ea1t v\u00e0 hi\u1ec7u qu\u1ea3. Trong b\u00e0i vi\u1ebft n\u00e0y, ch\u00fang ta s\u1ebd c\u00f9ng kh\u00e1m ph\u00e1 kh\u00e1i ni\u1ec7m c\u01a1 b\u1ea3n v\u1ec1 bi\u1ebfn trong Java, vai tr\u00f2 quan tr\u1ecdng c\u1ee7a ch\u00fang trong l\u1eadp tr\u00ecnh Java, v\u00e0 l\u00fd do t\u1ea1i sao vi\u1ec7c s\u1eed d\u1ee5ng bi\u1ebfn \u0111\u00fang c\u00e1ch l\u00e0 y\u1ebfu t\u1ed1 then ch\u1ed1t \u0111\u1ec3 vi\u1ebft m\u00e3 ngu\u1ed3n r\u00f5 r\u00e0ng, t\u1ed1i \u01b0u v\u00e0 d\u1ec5 b\u1ea3o tr\u00ec.<\/strong><\/em><\/p>\n<p>\u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u th\u00eam:<\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Kh\u00e1i ni\u1ec7m v\u00e0 ph\u00e2n lo\u1ea1i bi\u1ebfn trong Java<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c ki\u1ec3u d\u1eef li\u1ec7u c\u1ee7a bi\u1ebfn trong Java<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c t\u1eeb kh\u00f3a li\u00ean quan \u0111\u1ebfn bi\u1ebfn trong Java<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ph\u1ea1m vi v\u00e0 th\u1eddi gian s\u1ed1ng c\u1ee7a bi\u1ebfn\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c l\u1ed7i th\u01b0\u1eddng g\u1eb7p khi s\u1eed d\u1ee5ng bi\u1ebfn<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">M\u1eb9o v\u00e0 l\u01b0u \u00fd khi l\u00e0m vi\u1ec7c v\u1edbi bi\u1ebfn<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Bien_trong_Java_la_gi_Cach_khai_bao_va_khoi_tao_bien_trong_Java\"><\/span><strong>Bi\u1ebfn trong Java l\u00e0 g\u00ec? C\u00e1ch khai b\u00e1o v\u00e0 kh\u1edfi t\u1ea1o bi\u1ebfn trong Java<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><strong>Bi\u1ebfn trong Java l\u00e0 g\u00ec?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Bi\u1ebfn l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m c\u01a1 b\u1ea3n trong l\u1eadp tr\u00ecnh, \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 \u0111\u1eb7t t\u00ean cho m\u1ed9t v\u00f9ng nh\u1edb c\u1ee5 th\u1ec3 trong b\u1ed9 nh\u1edb m\u00e1y t\u00ednh nh\u1eb1m l\u01b0u tr\u1eef d\u1eef li\u1ec7u.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Trong Java, bi\u1ebfn ph\u1ea3i \u0111\u01b0\u1ee3c khai b\u00e1o tr\u01b0\u1edbc khi s\u1eed d\u1ee5ng v\u00e0 m\u1ed7i bi\u1ebfn c\u1ea7n \u0111\u01b0\u1ee3c g\u1eafn li\u1ec1n v\u1edbi m\u1ed9t ki\u1ec3u d\u1eef li\u1ec7u c\u1ee5 th\u1ec3. <\/span><span style=\"font-weight: 400;\">\u0110i\u1ec1u n\u00e0y gi\u00fap \u0111\u1ea3m b\u1ea3o t\u00ednh an to\u00e0n v\u00e0 nh\u1ea5t qu\u00e1n khi l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u.<\/span><\/p>\n<blockquote><p><em>\u0110\u1ecdc th\u00eam: <a href=\"https:\/\/itviec.com\/blog\/java-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>Java l\u00e0 g\u00ec? T\u1ea5t c\u1ea3 nh\u1eefng \u0111i\u1ec1u b\u1ea1n c\u1ea7n bi\u1ebft v\u1ec1 ng\u00f4n ng\u1eef Java<\/strong><\/a><\/em><\/p><\/blockquote>\n<h3><b>C\u00e1ch khai b\u00e1o v\u00e0 kh\u1edfi t\u1ea1o bi\u1ebfn trong Java<\/b><\/h3>\n<h4><b>Khai b\u00e1o<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Khi khai b\u00e1o bi\u1ebfn, c\u1ea7n x\u00e1c \u0111\u1ecbnh r\u00f5 ki\u1ec3u d\u1eef li\u1ec7u \u0111\u1ec3 Java c\u00f3 th\u1ec3 t\u1ed1i \u01b0u h\u00f3a qu\u1ea3n l\u00fd b\u1ed9 nh\u1edb. Ki\u1ec3u d\u1eef li\u1ec7u c\u00f3 th\u1ec3 l\u00e0 c\u00e1c ki\u1ec3u c\u01a1 b\u1ea3n nh\u01b0 <\/span><span style=\"font-weight: 400;\">int<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">double<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">char<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c ki\u1ec3u \u0111\u1ed1i t\u01b0\u1ee3ng nh\u01b0 <\/span><span style=\"font-weight: 400;\">String<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">int age;<\/span><\/pre>\n<h4><b>Kh\u1edfi t\u1ea1o<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Vi\u1ec7c g\u00e1n gi\u00e1 tr\u1ecb ban \u0111\u1ea7u cho bi\u1ebfn l\u00e0 c\u1ea7n thi\u1ebft \u0111\u1ec3 bi\u1ebfn c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng ngay. \u0110i\u1ec1u n\u00e0y c\u0169ng gi\u00fap tr\u00e1nh c\u00e1c l\u1ed7i li\u00ean quan \u0111\u1ebfn gi\u00e1 tr\u1ecb kh\u00f4ng x\u00e1c \u0111\u1ecbnh.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">age = 25;<\/span><\/pre>\n<h4><b>Khai b\u00e1o v\u00e0 kh\u1edfi t\u1ea1o c\u00f9ng l\u00fac<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">\u0110\u00e2y l\u00e0 c\u00e1ch ph\u1ed5 bi\u1ebfn gi\u00fap m\u00e3 ngu\u1ed3n g\u1ecdn g\u00e0ng h\u01a1n.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">int age = 25;<\/span><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Quy_tac_dat_ten_bien_trong_Java\"><\/span><b>Quy t\u1eafc \u0111\u1eb7t t\u00ean bi\u1ebfn trong Java<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u00ean bi\u1ebfn ph\u1ea3i b\u1eaft \u0111\u1ea7u b\u1eb1ng ch\u1eef c\u00e1i v\u00e0 ch\u1ec9 c\u00f3 th\u1ec3 ch\u1ee9a c\u00e1c k\u00fd t\u1ef1: ch\u1eef c\u00e1i, ch\u1eef s\u1ed1, v\u00e0 d\u1ea5u g\u1ea1ch d\u01b0\u1edbi (<\/span><span style=\"font-weight: 400;\">_<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u00ean bi\u1ebfn c\u1ea7n tu\u00e2n theo phong c\u00e1ch camelCase \u0111\u1ec3 t\u0103ng t\u00ednh \u0111\u1ecdc hi\u1ec3u v\u00e0 nh\u1ea5t qu\u00e1n trong m\u00e3 ngu\u1ed3n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Tr\u00e1nh s\u1eed d\u1ee5ng c\u00e1c t\u1eeb kh\u00f3a Java l\u00e0m t\u00ean bi\u1ebfn, v\u00ec \u0111i\u1ec1u n\u00e0y s\u1ebd g\u00e2y l\u1ed7i c\u00fa ph\u00e1p.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u00ean bi\u1ebfn c\u1ea7n mang \u00fd ngh\u0129a r\u00f5 r\u00e0ng, m\u00f4 t\u1ea3 ch\u00ednh x\u00e1c vai tr\u00f2 c\u1ee7a bi\u1ebfn trong ch\u01b0\u01a1ng tr\u00ecnh.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">int studentAge = 20;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">String studentName = \"John\";<\/span><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Phan_loai_bien_trong_Java\"><\/span><b>Ph\u00e2n lo\u1ea1i bi\u1ebfn trong Java<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Bi\u1ebfn trong Java \u0111\u01b0\u1ee3c chia th\u00e0nh ba lo\u1ea1i ch\u00ednh: bi\u1ebfn c\u1ee5c b\u1ed9, bi\u1ebfn to\u00e0n c\u1ee5c (instance variables), v\u00e0 bi\u1ebfn t\u0129nh (static variables). M\u1ed7i lo\u1ea1i bi\u1ebfn c\u00f3 ph\u1ea1m vi s\u1eed d\u1ee5ng v\u00e0 \u0111\u1eb7c \u0111i\u1ec3m kh\u00e1c nhau.<\/span><\/p>\n<h3><b>Bi\u1ebfn c\u1ee5c b\u1ed9 (Local Variables)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Bi\u1ebfn c\u1ee5c b\u1ed9 l\u00e0 c\u00e1c bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o b\u00ean trong m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c, m\u1ed9t kh\u1ed1i m\u00e3, ho\u1eb7c m\u1ed9t constructor v\u00e0 ch\u1ec9 t\u1ed3n t\u1ea1i trong ph\u1ea1m vi c\u1ee7a ch\u00fang.<\/span><\/p>\n<p>\u0110\u1eb7c \u0111i\u1ec3m c\u1ee7a bi\u1ebfn c\u1ee5c b\u1ed9<span style=\"font-weight: 400;\">:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Bi\u1ebfn c\u1ee5c b\u1ed9 ch\u1ec9 c\u00f3 th\u1ec3 truy c\u1eadp trong ph\u1ea1m vi m\u00e0 ch\u00fang \u0111\u01b0\u1ee3c khai b\u00e1o.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Java kh\u00f4ng g\u00e1n gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh cho bi\u1ebfn c\u1ee5c b\u1ed9, v\u00ec v\u1eady ch\u00fang c\u1ea7n \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o tr\u01b0\u1edbc khi s\u1eed d\u1ee5ng.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Ch\u00fang c\u00f3 th\u1eddi gian s\u1ed1ng ng\u1eafn, ch\u1ec9 t\u1ed3n t\u1ea1i trong khi ph\u01b0\u01a1ng th\u1ee9c ho\u1eb7c kh\u1ed1i m\u00e3 \u0111ang \u0111\u01b0\u1ee3c th\u1ef1c thi.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">public void printAge() {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0int age = 25; \/\/ Bi\u1ebfn c\u1ee5c b\u1ed9<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0System.out.println(\"Age: \" + age);<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h3><b>Bi\u1ebfn to\u00e0n c\u1ee5c (Instance Variables)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Bi\u1ebfn to\u00e0n c\u1ee5c l\u00e0 c\u00e1c bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o b\u00ean trong m\u1ed9t l\u1edbp nh\u01b0ng n\u1eb1m ngo\u00e0i c\u00e1c ph\u01b0\u01a1ng th\u1ee9c, kh\u1ed1i m\u00e3, ho\u1eb7c constructor. C\u00e1c bi\u1ebfn n\u00e0y \u0111\u1ea1i di\u1ec7n cho tr\u1ea1ng th\u00e1i c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/p>\n<p>\u0110\u1eb7c \u0111i\u1ec3m c\u1ee7a bi\u1ebfn to\u00e0n c\u1ee5c<span style=\"font-weight: 400;\">:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Bi\u1ebfn to\u00e0n c\u1ee5c thu\u1ed9c v\u1ec1 t\u1eebng \u0111\u1ed1i t\u01b0\u1ee3ng, ngh\u0129a l\u00e0 m\u1ed7i \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp s\u1ebd c\u00f3 b\u1ea3n sao ri\u00eang bi\u1ec7t c\u1ee7a bi\u1ebfn \u0111\u00f3.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Java t\u1ef1 \u0111\u1ed9ng g\u00e1n gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh cho bi\u1ebfn to\u00e0n c\u1ee5c n\u1ebfu ch\u00fang kh\u00f4ng \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o (v\u00ed d\u1ee5: <\/span><span style=\"font-weight: 400;\">0<\/span><span style=\"font-weight: 400;\"> cho ki\u1ec3u s\u1ed1 nguy\u00ean, <\/span><span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\"> cho ki\u1ec3u tham chi\u1ebfu).<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Bi\u1ebfn to\u00e0n c\u1ee5c gi\u00fap l\u01b0u tr\u1eef v\u00e0 chia s\u1ebb d\u1eef li\u1ec7u gi\u1eefa c\u00e1c ph\u01b0\u01a1ng th\u1ee9c trong c\u00f9ng m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">public class Student {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0String name; \/\/ Bi\u1ebfn to\u00e0n c\u1ee5c<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0int age;<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0public void displayInfo() {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"Name: \" + name);<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"Age: \" + age);<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h3><b>Bi\u1ebfn t\u0129nh (Static Variables)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Bi\u1ebfn t\u0129nh l\u00e0 c\u00e1c bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o v\u1edbi t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">static<\/span><span style=\"font-weight: 400;\">. Ch\u00fang kh\u00f4ng thu\u1ed9c v\u1ec1 \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee5 th\u1ec3 n\u00e0o m\u00e0 thu\u1ed9c v\u1ec1 l\u1edbp.<\/span><\/p>\n<p>\u0110\u1eb7c \u0111i\u1ec3m c\u1ee7a bi\u1ebfn t\u0129nh<span style=\"font-weight: 400;\">:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Ch\u1ec9 c\u00f3 m\u1ed9t b\u1ea3n sao duy nh\u1ea5t c\u1ee7a bi\u1ebfn t\u0129nh trong b\u1ed9 nh\u1edb, gi\u00fap ti\u1ebft ki\u1ec7m t\u00e0i nguy\u00ean.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Bi\u1ebfn t\u0129nh c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp m\u00e0 kh\u00f4ng c\u1ea7n t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng, s\u1eed d\u1ee5ng t\u00ean l\u1edbp \u0111\u1ec3 truy c\u1eadp tr\u1ef1c ti\u1ebfp.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 l\u01b0u tr\u1eef c\u00e1c gi\u00e1 tr\u1ecb ho\u1eb7c h\u1eb1ng s\u1ed1 chung, c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c chia s\u1ebb b\u1edfi t\u1ea5t c\u1ea3 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">public class MathUtils {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0static final double PI = 3.14159; \/\/ Bi\u1ebfn t\u0129nh<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0public static double calculateCircleArea(double radius) {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return PI * radius * radius;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ Truy c\u1eadp<\/span>\r\n<span style=\"font-weight: 400;\">System.out.println(MathUtils.PI);<\/span><\/pre>\n<h3><strong>B\u1ea3ng t\u1ed5ng h\u1ee3p c\u00e1c lo\u1ea1i bi\u1ebfn trong Java<\/strong><\/h3>\n<table>\n<tbody>\n<tr>\n<td><b>Lo\u1ea1i bi\u1ebfn\u00a0<\/b><\/td>\n<td><b>\u0110\u1ecbnh ngh\u0129a\u00a0<\/b><\/td>\n<td><b>Ph\u1ea1m vi s\u1eed d\u1ee5ng\u00a0<\/b><\/td>\n<td><b>\u0110\u1eb7c \u0111i\u1ec3m<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>Bi\u1ebfn to\u00e0n c\u1ee5c\u00a0<\/b><\/td>\n<td><span style=\"font-weight: 400;\">Bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o trong l\u1edbp nh\u01b0ng b\u00ean ngo\u00e0i t\u1ea5t c\u1ea3 c\u00e1c ph\u01b0\u01a1ng th\u1ee9c ho\u1eb7c kh\u1ed1i.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng b\u1edfi t\u1ea5t c\u1ea3 c\u00e1c ph\u01b0\u01a1ng th\u1ee9c trong l\u1edbp. N\u1ebfu \u0111\u01b0\u1ee3c khai b\u00e1o <\/span><span style=\"font-weight: 400;\">public<\/span><span style=\"font-weight: 400;\">, c\u00f3 th\u1ec3 truy c\u1eadp t\u1eeb l\u1edbp kh\u00e1c.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">&#8211; T\u1ed3n t\u1ea1i trong su\u1ed1t th\u1eddi gian c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng (instance).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8211; Gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh l\u00e0 0 (\u0111\u1ed1i v\u1edbi ki\u1ec3u s\u1ed1), <\/span><span style=\"font-weight: 400;\">false<\/span><span style=\"font-weight: 400;\"> (ki\u1ec3u boolean) ho\u1eb7c <\/span><span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\"> (ki\u1ec3u tham chi\u1ebfu).<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>Bi\u1ebfn c\u1ee5c b\u1ed9<\/b><\/td>\n<td><span style=\"font-weight: 400;\">Bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o b\u00ean trong m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c, kh\u1ed1i, ho\u1eb7c constructor.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ch\u1ec9 c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng b\u00ean trong ph\u01b0\u01a1ng th\u1ee9c, kh\u1ed1i, ho\u1eb7c constructor n\u01a1i n\u00f3 \u0111\u01b0\u1ee3c khai b\u00e1o.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">&#8211; Kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh, b\u1eaft bu\u1ed9c ph\u1ea3i kh\u1edfi t\u1ea1o tr\u01b0\u1edbc khi s\u1eed d\u1ee5ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8211; T\u1ed3n t\u1ea1i trong ph\u1ea1m vi ph\u01b0\u01a1ng th\u1ee9c ho\u1eb7c kh\u1ed1i.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>Bi\u1ebfn t\u0129nh (static)<\/b><\/td>\n<td><span style=\"font-weight: 400;\">Bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o v\u1edbi t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">static<\/span><span style=\"font-weight: 400;\"> v\u00e0 thu\u1ed9c v\u1ec1 l\u1edbp ch\u1ee9 kh\u00f4ng ph\u1ea3i \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp b\u1edfi t\u1ea5t c\u1ea3 c\u00e1c ph\u01b0\u01a1ng th\u1ee9c trong l\u1edbp ho\u1eb7c t\u1eeb l\u1edbp kh\u00e1c th\u00f4ng qua t\u00ean l\u1edbp.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">&#8211; Gi\u00e1 tr\u1ecb \u0111\u01b0\u1ee3c chia s\u1ebb gi\u1eefa t\u1ea5t c\u1ea3 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8211; C\u00f3 gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh nh\u01b0 bi\u1ebfn to\u00e0n c\u1ee5c.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">&#8211; \u0110\u01b0\u1ee3c kh\u1edfi t\u1ea1o khi l\u1edbp \u0111\u01b0\u1ee3c t\u1ea3i v\u00e0o b\u1ed9 nh\u1edb.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"Cac_kieu_du_lieu_cua_bien_trong_Java\"><\/span><strong>C\u00e1c ki\u1ec3u d\u1eef li\u1ec7u c\u1ee7a bi\u1ebfn trong Java<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Trong Java, ki\u1ec3u d\u1eef li\u1ec7u c\u1ee7a bi\u1ebfn x\u00e1c \u0111\u1ecbnh lo\u1ea1i gi\u00e1 tr\u1ecb m\u00e0 bi\u1ebfn c\u00f3 th\u1ec3 l\u01b0u tr\u1eef. Ch\u00fang \u0111\u01b0\u1ee3c chia th\u00e0nh hai lo\u1ea1i ch\u00ednh:<\/span><\/p>\n<ul>\n<li>Ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y (Primitive type)<\/li>\n<li>Ki\u1ec3u d\u1eef li\u1ec7u tham chi\u1ebfu (Reference type)<\/li>\n<\/ul>\n<h3><b>Ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y (Primitive type)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y l\u00e0 c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u c\u01a1 b\u1ea3n \u0111\u01b0\u1ee3c Java cung c\u1ea5p s\u1eb5n. Ch\u00fang kh\u00f4ng c\u00f3 ph\u01b0\u01a1ng th\u1ee9c \u0111i k\u00e8m v\u00e0 th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 l\u01b0u tr\u1eef c\u00e1c gi\u00e1 tr\u1ecb \u0111\u01a1n gi\u1ea3n nh\u01b0 s\u1ed1, k\u00fd t\u1ef1, ho\u1eb7c gi\u00e1 tr\u1ecb logic.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00e1c ki\u1ec3u d\u1eef li\u1ec7u n\u00e0y \u0111\u00f3ng vai tr\u00f2 quan tr\u1ecdng trong vi\u1ec7c \u0111\u1ea3m b\u1ea3o hi\u1ec7u su\u1ea5t v\u00e0 \u0111\u01a1n gi\u1ea3n h\u00f3a qu\u00e1 tr\u00ecnh x\u1eed l\u00fd d\u1eef li\u1ec7u trong c\u00e1c ch\u01b0\u01a1ng tr\u00ecnh Java.<\/span><\/p>\n<h4><strong>Danh s\u00e1ch c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y<\/strong><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>int<\/b><span style=\"font-weight: 400;\">: L\u01b0u tr\u1eef s\u1ed1 nguy\u00ean v\u1edbi k\u00edch th\u01b0\u1edbc chu\u1ea9n, th\u01b0\u1eddng d\u00f9ng \u0111\u1ec3 \u0111\u1ea1i di\u1ec7n cho c\u00e1c s\u1ed1 nguy\u00ean trong ph\u1ea1m vi l\u1edbn.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>float<\/b><span style=\"font-weight: 400;\">: L\u01b0u tr\u1eef s\u1ed1 th\u1ef1c d\u1ea5u ph\u1ea9y \u0111\u1ed9ng v\u1edbi \u0111\u1ed9 ch\u00ednh x\u00e1c t\u01b0\u01a1ng \u0111\u1ed1i, th\u00edch h\u1ee3p cho c\u00e1c t\u00ednh to\u00e1n kh\u00f4ng y\u00eau c\u1ea7u \u0111\u1ed9 ch\u00ednh x\u00e1c cao.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>double<\/b><span style=\"font-weight: 400;\">: L\u01b0u tr\u1eef s\u1ed1 th\u1ef1c d\u1ea5u ph\u1ea9y \u0111\u1ed9ng v\u1edbi \u0111\u1ed9 ch\u00ednh x\u00e1c cao h\u01a1n <\/span><span style=\"font-weight: 400;\">float<\/span><span style=\"font-weight: 400;\">, th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng cho c\u00e1c ph\u00e9p t\u00ednh khoa h\u1ecdc.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>char<\/b><span style=\"font-weight: 400;\">: L\u01b0u tr\u1eef m\u1ed9t k\u00fd t\u1ef1 \u0111\u01a1n trong b\u1ea3ng m\u00e3 Unicode, ph\u00f9 h\u1ee3p cho vi\u1ec7c x\u1eed l\u00fd k\u00fd t\u1ef1 v\u00e0 bi\u1ec3u t\u01b0\u1ee3ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>boolean<\/b><span style=\"font-weight: 400;\">: L\u01b0u tr\u1eef gi\u00e1 tr\u1ecb logic, \u0111\u1ea1i di\u1ec7n cho hai tr\u1ea1ng th\u00e1i c\u01a1 b\u1ea3n l\u00e0 <\/span><span style=\"font-weight: 400;\">true<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">false<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>long<\/b><span style=\"font-weight: 400;\">: L\u01b0u tr\u1eef s\u1ed1 nguy\u00ean v\u1edbi k\u00edch th\u01b0\u1edbc l\u1edbn h\u01a1n <\/span><span style=\"font-weight: 400;\">int<\/span><span style=\"font-weight: 400;\">, h\u1eefu \u00edch khi c\u1ea7n l\u00e0m vi\u1ec7c v\u1edbi s\u1ed1 li\u1ec7u l\u1edbn.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>short<\/b><span style=\"font-weight: 400;\">: L\u01b0u tr\u1eef s\u1ed1 nguy\u00ean nh\u1ecf h\u01a1n <\/span><span style=\"font-weight: 400;\">int<\/span><span style=\"font-weight: 400;\">, th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 ti\u1ebft ki\u1ec7m b\u1ed9 nh\u1edb.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>byte<\/b><span style=\"font-weight: 400;\">: L\u01b0u tr\u1eef s\u1ed1 nguy\u00ean r\u1ea5t nh\u1ecf, ch\u1ee7 y\u1ebfu d\u00f9ng \u0111\u1ec3 t\u1ed1i \u01b0u h\u00f3a b\u1ed9 nh\u1edb trong c\u00e1c \u1ee9ng d\u1ee5ng y\u00eau c\u1ea7u hi\u1ec7u qu\u1ea3 cao.<\/span><\/li>\n<\/ul>\n<h4><strong>B\u1ea3ng k\u00edch th\u01b0\u1edbc v\u00e0 ph\u1ea1m vi gi\u00e1 tr\u1ecb c\u1ee7a t\u1eebng ki\u1ec3u d\u1eef li\u1ec7u<\/strong><\/h4>\n<table>\n<tbody>\n<tr>\n<td><b>Ki\u1ec3u d\u1eef li\u1ec7u\u00a0<\/b><\/td>\n<td><b>K\u00edch th\u01b0\u1edbc (bits)<\/b><\/td>\n<td><b>Ph\u1ea1m vi gi\u00e1 tr\u1ecb\u00a0<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>byte\u00a0<\/b><\/td>\n<td><span style=\"font-weight: 400;\">8<\/span><\/td>\n<td><span style=\"font-weight: 400;\">-128 \u0111\u1ebfn 127<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>short<\/b><\/td>\n<td><span style=\"font-weight: 400;\">13<\/span><\/td>\n<td><span style=\"font-weight: 400;\">-32,768 \u0111\u1ebfn 32,767<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>int<\/b><\/td>\n<td><span style=\"font-weight: 400;\">32<\/span><\/td>\n<td><span style=\"font-weight: 400;\">-2,147,483,648 \u0111\u1ebfn 2,147,483,647<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>long<\/b><\/td>\n<td><span style=\"font-weight: 400;\">64<\/span><\/td>\n<td><span style=\"font-weight: 400;\">-9,223,372,036,854,775,808 \u0111\u1ebfn 9,223,372,036,854,775,807<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>float<\/b><\/td>\n<td><span style=\"font-weight: 400;\">32<\/span><\/td>\n<td><span style=\"font-weight: 400;\">~1.4E-45 \u0111\u1ebfn ~3.4E+38 (6-7 ch\u1eef s\u1ed1 ch\u00ednh x\u00e1c)<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>double<\/b><\/td>\n<td><span style=\"font-weight: 400;\">64<\/span><\/td>\n<td><span style=\"font-weight: 400;\">~4.9E-324 \u0111\u1ebfn ~1.8E+308 (15 ch\u1eef s\u1ed1 ch\u00ednh x\u00e1c)<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>char<\/b><\/td>\n<td><span style=\"font-weight: 400;\">16<\/span><\/td>\n<td><span style=\"font-weight: 400;\">K\u00fd t\u1ef1 Unicode (0 \u0111\u1ebfn 65,535)<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>boolean<\/b><\/td>\n<td><span style=\"font-weight: 400;\">1 (kh\u00f4ng r\u00f5 k\u00edch th\u01b0\u1edbc c\u1ee5 th\u1ec3)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">true ho\u1eb7c false<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">int age = 25;\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">float price = 19.99f;\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">char grade = 'A';\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">boolean isActive = true;\u00a0\u00a0<\/span><\/pre>\n<h3><b>Ki\u1ec3u d\u1eef li\u1ec7u tham chi\u1ebfu (Reference type)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Ki\u1ec3u d\u1eef li\u1ec7u tham chi\u1ebfu d\u00f9ng \u0111\u1ec3 l\u01b0u tr\u1eef tham chi\u1ebfu \u0111\u1ebfn c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng ho\u1eb7c c\u1ea5u tr\u00fac ph\u1ee9c t\u1ea1p.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Kh\u00e1c v\u1edbi ki\u1ec3u nguy\u00ean th\u1ee7, c\u00e1c ki\u1ec3u tham chi\u1ebfu c\u00f3 c\u00e1c ph\u01b0\u01a1ng th\u1ee9c v\u00e0 thu\u1ed9c t\u00ednh \u0111i k\u00e8m, gi\u00fap ch\u00fang linh ho\u1ea1t h\u01a1n khi l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u ph\u1ee9c t\u1ea1p.<\/span><\/p>\n<h4><strong>T\u00ednh n\u0103ng \u0111\u1eb7c bi\u1ec7t c\u1ee7a ki\u1ec3u tham chi\u1ebfu<\/strong><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 ch\u1ee9a gi\u00e1 tr\u1ecb <\/span><span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\">, bi\u1ec3u th\u1ecb bi\u1ebfn kh\u00f4ng tham chi\u1ebfu \u0111\u1ebfn b\u1ea5t k\u1ef3 \u0111\u1ed1i t\u01b0\u1ee3ng n\u00e0o.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Cung c\u1ea5p nhi\u1ec1u ph\u01b0\u01a1ng th\u1ee9c \u0111\u1ec3 x\u1eed l\u00fd d\u1eef li\u1ec7u linh ho\u1ea1t, gi\u00fap l\u1eadp tr\u00ecnh vi\u00ean thao t\u00e1c d\u1eef li\u1ec7u hi\u1ec7u qu\u1ea3 h\u01a1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Th\u00edch h\u1ee3p \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c c\u1ea5u tr\u00fac d\u1eef li\u1ec7u ph\u1ee9c t\u1ea1p v\u00e0 c\u00e1c t\u00ecnh hu\u1ed1ng c\u1ea7n t\u00ednh linh ho\u1ea1t cao.<\/span><\/li>\n<\/ul>\n<h4><strong>C\u00e1c ki\u1ec3u tham chi\u1ebfu ph\u1ed5 bi\u1ebfn<\/strong><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>String<\/b><span style=\"font-weight: 400;\">: L\u01b0u tr\u1eef chu\u1ed7i k\u00fd t\u1ef1. \u0110\u00e2y l\u00e0 m\u1ed9t trong nh\u1eefng ki\u1ec3u tham chi\u1ebfu \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng ph\u1ed5 bi\u1ebfn nh\u1ea5t trong Java.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Array<\/b><span style=\"font-weight: 400;\">: L\u01b0u tr\u1eef danh s\u00e1ch c\u00e1c gi\u00e1 tr\u1ecb c\u00f3 c\u00f9ng ki\u1ec3u. C\u00e1c m\u1ea3ng cho ph\u00e9p l\u01b0u tr\u1eef v\u00e0 truy c\u1eadp d\u1eef li\u1ec7u m\u1ed9t c\u00e1ch c\u00f3 t\u1ed5 ch\u1ee9c.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Object<\/b><span style=\"font-weight: 400;\">: \u0110\u1ea1i di\u1ec7n cho c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng trong Java, th\u01b0\u1eddng \u0111\u01b0\u1ee3c t\u1ea1o t\u1eeb c\u00e1c l\u1edbp (class). \u0110\u1ed1i t\u01b0\u1ee3ng cho ph\u00e9p l\u01b0u tr\u1eef v\u00e0 x\u1eed l\u00fd d\u1eef li\u1ec7u c\u0169ng nh\u01b0 logic ph\u1ee9c t\u1ea1p.<\/span><\/li>\n<\/ul>\n<h4><strong>C\u00e1ch s\u1eed d\u1ee5ng v\u00e0 v\u00ed d\u1ee5 minh h\u1ecda<\/strong><\/h4>\n<h5><b>String<\/b><\/h5>\n<pre><span style=\"font-weight: 400;\">String name = \"John\";\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">System.out.println(\"Hello, \" + name);\u00a0\u00a0<\/span><\/pre>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><span style=\"font-weight: 400;\"> Bi\u1ebfn <\/span><span style=\"font-weight: 400;\">name<\/span><span style=\"font-weight: 400;\"> l\u01b0u tr\u1eef tham chi\u1ebfu \u0111\u1ebfn chu\u1ed7i k\u00fd t\u1ef1 &#8220;John&#8221;. Chu\u1ed7i n\u00e0y c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c x\u1eed l\u00fd b\u1eb1ng c\u00e1c ph\u01b0\u01a1ng th\u1ee9c nh\u01b0 n\u1ed1i chu\u1ed7i, c\u1eaft chu\u1ed7i, ho\u1eb7c t\u00ecm ki\u1ebfm.<\/span><\/p>\n<h5><b>Array<\/b><b><\/b><\/h5>\n<pre><span style=\"font-weight: 400;\">int[] numbers = {1, 2, 3, 4, 5};\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">System.out.println(\"Element at index 2: \" + numbers[2]);\u00a0<\/span><\/pre>\n<p><span style=\"font-weight: 400;\"><strong>Gi\u1ea3i th\u00edch:<\/strong> Bi\u1ebfn <\/span><span style=\"font-weight: 400;\">name<\/span><span style=\"font-weight: 400;\"> l\u01b0u tr\u1eef tham chi\u1ebfu \u0111\u1ebfn chu\u1ed7i k\u00fd t\u1ef1 &#8220;John&#8221;. Chu\u1ed7i n\u00e0y c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c x\u1eed l\u00fd b\u1eb1ng c\u00e1c ph\u01b0\u01a1ng th\u1ee9c nh\u01b0 n\u1ed1i chu\u1ed7i, c\u1eaft chu\u1ed7i, ho\u1eb7c t\u00ecm ki\u1ebfm.<\/span><b><\/b><\/p>\n<h5><b>Object<\/b><b><\/b><\/h5>\n<pre><span style=\"font-weight: 400;\">class Person {\u00a0 <\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0String name;\u00a0\u00a0<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0int age;\u00a0\u00a0<\/span>\r\n<span style=\"font-weight: 400;\">}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">Person person = new Person();\u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">person.name = \"Alice\";\u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">person.age = 30;\u00a0 <\/span>\r\n<span style=\"font-weight: 400;\">System.out.println(\"Name: \" + person.name + \", Age: \" + person.age);<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/pre>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><span style=\"font-weight: 400;\"> Bi\u1ebfn <\/span><span style=\"font-weight: 400;\">person<\/span><span style=\"font-weight: 400;\"> l\u01b0u tr\u1eef tham chi\u1ebfu \u0111\u1ebfn m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp <\/span><span style=\"font-weight: 400;\">Person<\/span><span style=\"font-weight: 400;\">. Th\u00f4ng qua \u0111\u1ed1i t\u01b0\u1ee3ng n\u00e0y, ch\u00fang ta c\u00f3 th\u1ec3 l\u01b0u tr\u1eef v\u00e0 x\u1eed l\u00fd c\u00e1c thu\u1ed9c t\u00ednh li\u00ean quan.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Cac_tu_khoa_lien_quan_den_bien_trong_Java\"><\/span><strong>C\u00e1c t\u1eeb kh\u00f3a li\u00ean quan \u0111\u1ebfn bi\u1ebfn trong Java<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Java cung c\u1ea5p nhi\u1ec1u t\u1eeb kh\u00f3a \u0111\u1eb7c bi\u1ec7t \u0111\u1ec3 m\u1edf r\u1ed9ng c\u00f4ng d\u1ee5ng c\u1ee7a bi\u1ebfn. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 danh s\u00e1ch c\u00e1c t\u1eeb kh\u00f3a quan tr\u1ecdng nh\u1ea5t v\u00e0 vai tr\u00f2 c\u1ee7a ch\u00fang trong Java:<\/span><\/p>\n<h3><strong>Final<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">T\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 khai b\u00e1o bi\u1ebfn h\u1eb1ng, \u0111\u00e1nh d\u1ea5u bi\u1ebfn l\u00e0 kh\u00f4ng thay \u0111\u1ed5i \u0111\u01b0\u1ee3c sau khi \u0111\u00e3 \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o gi\u00e1 tr\u1ecb.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1eb7c \u0111i\u1ec3m:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o v\u1edbi <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\">, n\u00f3 ph\u1ea3i \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o gi\u00e1 tr\u1ecb ngay l\u1eadp t\u1ee9c ho\u1eb7c trong constructor (n\u1ebfu l\u00e0 bi\u1ebfn instance).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Bi\u1ebfn \u0111\u00e1nh d\u1ea5u b\u1eb1ng <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\"> kh\u00f4ng th\u1ec3 b\u1ecb thay \u0111\u1ed5i gi\u00e1 tr\u1ecb sau \u0111\u00f3.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre>final int MAX_VALUE = 100;\r\nSystem.out.println(\"Gi\u00e1 tr\u1ecb c\u1ee7a MAX_VALUE:\r\n+\r\nMAX_VALUE);<\/pre>\n<p><b>Gi\u1ea3i th\u00edch<\/b><span style=\"font-weight: 400;\">: Bi\u1ebfn <\/span><span style=\"font-weight: 400;\">MAX_VALUE<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c khai b\u00e1o v\u1edbi <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\"> n\u00ean n\u00f3 l\u00e0 h\u1eb1ng s\u1ed1 v\u00e0 kh\u00f4ng th\u1ec3 b\u1ecb thay \u0111\u1ed5i gi\u00e1 tr\u1ecb.<\/span><\/p>\n<h3><strong>Static<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">T\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">static<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 khai b\u00e1o bi\u1ebfn t\u0129nh, t\u1ed3n t\u1ea1i \u1edf c\u1ea5p \u0111\u1ed9 l\u1edbp thay v\u00ec instance.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1eb7c \u0111i\u1ec3m:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Bi\u1ebfn t\u0129nh \u0111\u01b0\u1ee3c chia s\u1ebb ch\u00fang gi\u1eefa t\u1ea5t c\u1ea3 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">N\u00f3 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp m\u00e0 kh\u00f4ng c\u1ea7n t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng (instance).<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre>class Counter {\r\n  static int count = 0;\r\n\r\n  Counter() {\r\n    count++;\r\n    System.out.println(\"Counter created. Current count: \" + count);\r\n  }\r\n\r\n  static void resetCount() {\r\n    count = 0;\r\n    System.out.println(\"Counter reset. Current count: \" + count);\r\n  }\r\n\r\n  static int getCount() {\r\n    return count;\r\n  }\r\n\r\n  static void displayCount() {\r\n    System.out.println(\"Total count: \"+count);\r\n} int count = 0;\r\n\r\n  Counter() {\r\n    count++;\r\n  }\r\n}\r\n\r\npublic class Main {\r\n  public static void main(String[] args) {\r\n    Counter c1 = new Counter();\r\n    Counter c2 = new Counter();\r\n    System.out.println(\"Count: \" + Counter.count);\r\n  }\r\n}<\/pre>\n<p><b>Gi\u1ea3i th\u00edch<\/b><span style=\"font-weight: 400;\">: Bi\u1ebfn <\/span><span style=\"font-weight: 400;\">count<\/span><span style=\"font-weight: 400;\"> l\u00e0 bi\u1ebfn t\u0129nh, \u0111\u01b0\u1ee3c chia s\u1ebb cho t\u1ea5t c\u1ea3 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp <\/span><span style=\"font-weight: 400;\">Counter<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<h3><strong>Transient<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">T\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">transient<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 b\u1ecf qua bi\u1ebfn trong qu\u00e1 tr\u00ecnh tu\u1ea7n t\u1ef1 h\u00f3a (serialization).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1eb7c \u0111i\u1ec3m:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Bi\u1ebfn khai b\u00e1o v\u1edbi <\/span><span style=\"font-weight: 400;\">transient<\/span><span style=\"font-weight: 400;\"> s\u1ebd kh\u00f4ng \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef trong lu\u1ed3ng tu\u1ea7n t\u1ef1 h\u00f3a.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">H\u1eefu \u00edch trong vi\u1ec7c b\u1ea3o m\u1eadt d\u1eef li\u1ec7u nh\u1ea1y c\u1ea3m nh\u01b0 m\u1eadt kh\u1ea9u.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">import java.io.*;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">class User implements Serializable {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0String username;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0transient String password;<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0User(String username, String password) {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.username = username;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.password = password;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">public class Main {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0public static void main(String[] args) throws Exception {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0User user = new User(\"admin\", \"12345\");<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Serialization<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(\"user.ser\"));<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0out.writeObject(user);<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0out.close();<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Deserialization<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ObjectInputStream in = new ObjectInputStream(new FileInputStream(\"user.ser\"));<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0User deserializedUser = (User) in.readObject();<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0in.close();<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"Username: \" + deserializedUser.username);<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"Password: \" + deserializedUser.password); \/\/ Null do transient<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><b>Gi\u1ea3i th\u00edch<\/b><span style=\"font-weight: 400;\">: Bi\u1ebfn <\/span><span style=\"font-weight: 400;\">password<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c khai b\u00e1o v\u1edbi <\/span><span style=\"font-weight: 400;\">transient<\/span><span style=\"font-weight: 400;\"> s\u1ebd kh\u00f4ng \u0111\u01b0\u1ee3c tu\u1ea7n t\u1ef1 h\u00f3a.<\/span><\/p>\n<h3><strong>Volatile<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">T\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">volatile<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o bi\u1ebfn lu\u00f4n \u0111\u01b0\u1ee3c \u0111\u1ed3ng b\u1ed9 trong m\u00f4<\/span><\/p>\n<p><span style=\"font-weight: 400;\">M\u1ed9t v\u00ed d\u1ee5 v\u1ec1 vi\u1ec7c s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a volatile:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">class SharedData {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\/\/ S\u1eed d\u1ee5ng volatile \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o t\u00ednh nh\u1ea5t qu\u00e1n khi truy c\u1eadp bi\u1ebfn n\u00e0y t\u1eeb nhi\u1ec1u thread<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private volatile boolean running = true;<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0public boolean isRunning() {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return running;<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0public void stopRunning() {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0running = false;<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">public class VolatileExample {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0public static void main(String[] args) {<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0SharedData sharedData = new SharedData();<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Thread 1: Th\u1ef1c hi\u1ec7n m\u1ed9t c\u00f4ng vi\u1ec7c trong khi running = true<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Thread worker = new Thread(() -&gt; {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"Worker thread started.\");<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0while (sharedData.isRunning()) {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Simulate some work<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"Worker thread stopped.\");<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0});\r\n<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0worker.start();<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Thread 2: D\u1eebng thread sau m\u1ed9t th\u1eddi gian<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Thread stopper = new Thread(() -&gt; {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0try {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Thread.sleep(2000); \/\/ \u0110\u1ee3i 2 gi\u00e2y<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} catch (InterruptedException e) {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0e.printStackTrace();<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"Requesting worker thread to stop...\");<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sharedData.stopRunning();<\/span>\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0});<\/span>\r\n\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0stopper.start();<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><b>Gi\u1ea3i th\u00edch:<\/b><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Bi\u1ebfn running \u0111\u01b0\u1ee3c khai b\u00e1o v\u1edbi t\u1eeb kh\u00f3a volatile: \u0110i\u1ec1u n\u00e0y \u0111\u1ea3m b\u1ea3o r\u1eb1ng khi stopRunning() \u0111\u01b0\u1ee3c g\u1ecdi b\u1edfi Thread 2, gi\u00e1 tr\u1ecb thay \u0111\u1ed5i c\u1ee7a running s\u1ebd \u0111\u01b0\u1ee3c c\u1eadp nh\u1eadt ngay l\u1eadp t\u1ee9c trong b\u1ed9 nh\u1edb ch\u00ednh v\u00e0 nh\u00ecn th\u1ea5y b\u1edfi Thread 1.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">N\u1ebfu kh\u00f4ng s\u1eed d\u1ee5ng volatile, Thread 1 c\u00f3 th\u1ec3 kh\u00f4ng nh\u1eadn th\u1ea5y s\u1ef1 thay \u0111\u1ed5i c\u1ee7a running do n\u00f3 c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng m\u1ed9t b\u1ea3n sao trong b\u1ed9 nh\u1edb cache c\u1ee7a CPU.<\/span><\/li>\n<\/ol>\n<p><b>K\u1ebft qu\u1ea3 khi ch\u1ea1y ch\u01b0\u01a1ng tr\u00ecnh:<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Thread 1 (worker) s\u1ebd li\u00ean t\u1ee5c ch\u1ea1y v\u00f2ng l\u1eb7p while cho \u0111\u1ebfn khi Thread 2 (stopper) thay \u0111\u1ed5i gi\u00e1 tr\u1ecb c\u1ee7a running th\u00e0nh false.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Sau 2 gi\u00e2y, Thread 2 y\u00eau c\u1ea7u d\u1eebng, v\u00e0 Thread 1 k\u1ebft th\u00fac v\u00f2ng l\u1eb7p.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Pham_vi_va_thoi_gian_song_cua_bien_trong_Java\"><\/span><strong>Ph\u1ea1m vi v\u00e0 th\u1eddi gian s\u1ed1ng c\u1ee7a bi\u1ebfn trong Java<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Trong Java, m\u1ed7i bi\u1ebfn \u0111\u1ec1u c\u00f3 ph\u1ea1m vi v\u00e0 th\u1eddi gian s\u1ed1ng ri\u00eang bi\u1ec7t. Hi\u1ec3u r\u00f5 hai kh\u00e1i ni\u1ec7m n\u00e0y kh\u00f4ng ch\u1ec9 gi\u00fap ch\u01b0\u01a1ng tr\u00ecnh ho\u1ea1t \u0111\u1ed9ng hi\u1ec7u qu\u1ea3 m\u00e0 c\u00f2n tr\u00e1nh \u0111\u01b0\u1ee3c c\u00e1c l\u1ed7i li\u00ean quan \u0111\u1ebfn b\u1ed9 nh\u1edb v\u00e0 t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t \u1ee9ng d\u1ee5ng.<\/span><\/p>\n<h3><b>Ph\u1ea1m vi c\u1ee7a bi\u1ebfn<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Ph\u1ea1m vi (scope) c\u1ee7a bi\u1ebfn x\u00e1c \u0111\u1ecbnh khu v\u1ef1c trong ch\u01b0\u01a1ng tr\u00ecnh m\u00e0 bi\u1ebfn c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp v\u00e0 s\u1eed d\u1ee5ng. Java cung c\u1ea5p ba lo\u1ea1i ph\u1ea1m vi ch\u00ednh:<\/span><\/p>\n<h4><b> Block scope<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o trong m\u1ed9t kh\u1ed1i l\u1ec7nh (block) s\u1ebd c\u00f3 ph\u1ea1m vi gi\u1edbi h\u1ea1n trong kh\u1ed1i l\u1ec7nh \u0111\u00f3.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Block \u0111\u01b0\u1ee3c x\u00e1c \u0111\u1ecbnh b\u1edfi c\u00e1c c\u1eb7p d\u1ea5u ngo\u1eb7c nh\u1ecdn <\/span><span style=\"font-weight: 400;\">{}<\/span><span style=\"font-weight: 400;\">. Khi k\u1ebft th\u00fac kh\u1ed1i, bi\u1ebfn s\u1ebd kh\u00f4ng c\u00f2n t\u1ed3n t\u1ea1i.<\/span><\/li>\n<\/ul>\n<p>V\u00ed d\u1ee5:<\/p>\n<pre>public class Main {\r\n  public static void main(String[] args) {\r\n    {\r\n      int x = 10; \/\/ Bi\u1ebfn x ch\u1ec9 t\u1ed3n t\u1ea1i trong block n\u00e0y\r\n      System.out.println(x); \/\/ H\u1ee3p l\u1ec7\r\n    }\r\n    \/\/ System.out.println(x); \/\/ L\u1ed7i: x ngo\u00e0i ph\u1ea1m vi\r\n  }\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Block scope th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 gi\u1edbi h\u1ea1n ph\u1ea1m vi ho\u1ea1t \u0111\u1ed9ng c\u1ee7a bi\u1ebfn, gi\u00fap gi\u1ea3m nguy c\u01a1 xung \u0111\u1ed9t t\u00ean bi\u1ebfn.<\/span><\/p>\n<h4><b> Method scope<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o trong m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c ch\u1ec9 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp b\u00ean trong ph\u01b0\u01a1ng th\u1ee9c \u0111\u00f3.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi ph\u01b0\u01a1ng th\u1ee9c k\u1ebft th\u00fac, bi\u1ebfn s\u1ebd b\u1ecb gi\u1ea3i ph\u00f3ng kh\u1ecfi b\u1ed9 nh\u1edb.<\/span><\/li>\n<\/ul>\n<p>V\u00ed d\u1ee5:<\/p>\n<pre>public class Main {\r\n  public static void main(String[] args) {\r\n    int y = 20; \/\/ Bi\u1ebfn y thu\u1ed9c ph\u1ea1m vi c\u1ee7a ph\u01b0\u01a1ng th\u1ee9c main\r\n    System.out.println(y); \/\/ H\u1ee3p l\u1ec7\r\n  }\r\n  \/\/ System.out.println(y); \/\/ L\u1ed7i: y ngo\u00e0i ph\u1ea1m vi\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Method scope th\u01b0\u1eddng d\u00f9ng cho c\u00e1c bi\u1ebfn t\u1ea1m th\u1eddi ph\u1ee5c v\u1ee5 logic x\u1eed l\u00fd c\u1ee7a ph\u01b0\u01a1ng th\u1ee9c.<\/span><\/p>\n<h4><b> Class scope<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o trong l\u1edbp, ngo\u00e0i c\u00e1c ph\u01b0\u01a1ng th\u1ee9c ho\u1eb7c block, s\u1ebd c\u00f3 ph\u1ea1m vi to\u00e0n b\u1ed9 l\u1edbp.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c bi\u1ebfn n\u00e0y c\u00f2n \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 instance variables ho\u1eb7c class variables, t\u00f9y thu\u1ed9c v\u00e0o vi\u1ec7c ch\u00fang c\u00f3 s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">static<\/span><span style=\"font-weight: 400;\"> hay kh\u00f4ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp t\u1eeb b\u1ea5t k\u1ef3 ph\u01b0\u01a1ng th\u1ee9c n\u00e0o trong l\u1edbp, tu\u1ef3 thu\u1ed9c v\u00e0o modifier (public, private, protected).<\/span><\/li>\n<\/ul>\n<p>V\u00ed d\u1ee5:<\/p>\n<pre>public class Main {\r\n  int z = 30; \/\/ Bi\u1ebfn z thu\u1ed9c ph\u1ea1m vi class\r\n\r\n  public void display() {\r\n    System.out.println(z); \/\/ H\u1ee3p l\u1ec7\r\n  }\r\n\r\n  public static void main(String[] args) {\r\n    Main obj = new Main();\r\n    obj.display(); \/\/ Truy c\u1eadp z qua ph\u01b0\u01a1ng th\u1ee9c\r\n  }\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Class scope \u0111\u1eb7c bi\u1ec7t h\u1eefu \u00edch khi b\u1ea1n mu\u1ed1n l\u01b0u tr\u1eef tr\u1ea1ng th\u00e1i ho\u1eb7c th\u00f4ng tin d\u00f9ng chung trong su\u1ed1t v\u00f2ng \u0111\u1eddi c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/p>\n<h4><strong>B\u1ea3ng so s\u00e1nh c\u00e1c ph\u1ea1m vi c\u1ee7a bi\u1ebfn trong Java<\/strong><\/h4>\n<table>\n<tbody>\n<tr>\n<td><b>Ti\u00eau ch\u00ed\u00a0<\/b><\/td>\n<td><b>Block Scope<\/b><\/td>\n<td><b>Method Scope<\/b><\/td>\n<td><b>Class Scope<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>\u0110\u1ecbnh ngh\u0129a\u00a0<\/b><\/td>\n<td><span style=\"font-weight: 400;\">Ph\u1ea1m vi c\u1ee7a bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o b\u00ean trong m\u1ed9t kh\u1ed1i {} (nh\u01b0 v\u00f2ng l\u1eb7p, c\u00e2u l\u1ec7nh if,..)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ph\u1ea1m vi c\u1ee7a bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o b\u00ean trong m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ph\u1ea1m vi c\u1ee7a bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o tr\u1ef1c ti\u1ebfp trong l\u1edbp, b\u00ean ngo\u00e0i c\u00e1c ph\u01b0\u01a1ng th\u1ee9c<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>C\u00e1ch khai b\u00e1o<\/b><\/td>\n<td><span style=\"font-weight: 400;\">Bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o b\u00ean trong c\u1eb7p {}<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o b\u00ean trong m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o b\u00ean ngo\u00e0i t\u1ea5t c\u1ea3 c\u00e1c ph\u01b0\u01a1ng th\u1ee9c, th\u01b0\u1eddng c\u00f3 t\u1eeb kh\u00f3a static ho\u1eb7c kh\u00f4ng\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>Ph\u1ea1m vi ho\u1ea1t \u0111\u1ed9ng<\/b><\/td>\n<td><span style=\"font-weight: 400;\">Ch\u1ec9 trong kh\u1ed1i {} n\u01a1i m\u00e0 n\u00f3 \u0111\u01b0\u1ee3c khai b\u00e1o<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ch\u1ec9 trong ph\u01b0\u01a1ng th\u1ee9c n\u01a1i m\u00e0 n\u00f3 \u0111\u01b0\u1ee3c khai b\u00e1o<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp b\u1edfi t\u1ea5t c\u1ea3 c\u00e1c ph\u01b0\u01a1ng th\u1ee9c trong l\u1edbp (ho\u1eb7c t\u1eeb c\u00e1c l\u1edbp kh\u00e1c n\u1ebfu l\u00e0 public)<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>Th\u1eddi gian t\u1ed3n t\u1ea1i\u00a0<\/b><\/td>\n<td><span style=\"font-weight: 400;\">T\u1ed3n t\u1ea1i t\u1eeb khi kh\u1ed1i b\u1eaft \u0111\u1ea7u cho \u0111\u1ebfn khi k\u1ebft th\u00fac\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">T\u1ed3n t\u1ea1i t\u1eeb khi ph\u01b0\u01a1ng th\u1ee9c b\u1eaft \u0111\u1ea7u cho \u0111\u1ebfn khi ph\u01b0\u01a1ng th\u1ee9c k\u1ebft th\u00fac\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">T\u1ed3n t\u1ea1i trong su\u1ed1t th\u1eddi gian t\u1ed3n t\u1ea1i c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng (n\u1ebfu l\u00e0 bi\u1ebfn instance) ho\u1eb7c ch\u01b0\u01a1ng tr\u00ecnh (n\u1ebfu l\u00e0 bi\u1ebfn <\/span><span style=\"font-weight: 400;\">static<\/span><span style=\"font-weight: 400;\">)<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>Kh\u1ea3 n\u0103ng truy c\u1eadp<\/b><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng th\u1ec3 truy c\u1eadp t\u1eeb b\u00ean ngo\u00e0i kh\u1ed1i<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng th\u1ec3 truy c\u1eadp t\u1eeb b\u00ean ngo\u00e0i ph\u01b0\u01a1ng th\u1ee9c<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 truy c\u1eadp t\u1eeb b\u00ean ngo\u00e0i l\u1edbp n\u1ebfu \u0111\u01b0\u1ee3c khai b\u00e1o <\/span><span style=\"font-weight: 400;\">public<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>Th\u1eddi gian s\u1ed1ng c\u1ee7a bi\u1ebfn\u00a0<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Th\u1eddi gian s\u1ed1ng c\u1ee7a bi\u1ebfn (lifetime) quy\u1ebft \u0111\u1ecbnh khi n\u00e0o bi\u1ebfn \u0111\u01b0\u1ee3c c\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb v\u00e0 khi n\u00e0o b\u1ed9 nh\u1edb c\u1ee7a bi\u1ebfn b\u1ecb thu h\u1ed3i. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 c\u00e1c lo\u1ea1i bi\u1ebfn v\u00e0 th\u1eddi gian s\u1ed1ng t\u01b0\u01a1ng \u1ee9ng:<\/span><\/p>\n<h4><b> Bi\u1ebfn c\u1ee5c b\u1ed9 (Local variables)<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb khi block ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c ch\u1ee9a bi\u1ebfn \u0111\u01b0\u1ee3c th\u1ef1c thi.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Gi\u1ea3i ph\u00f3ng khi block ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c k\u1ebft th\u00fac.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Bi\u1ebfn c\u1ee5c b\u1ed9 kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh, do \u0111\u00f3 c\u1ea7n \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o tr\u01b0\u1edbc khi s\u1eed d\u1ee5ng.<\/span><\/li>\n<\/ul>\n<p>V\u00ed d\u1ee5:<\/p>\n<pre>public class Main {\r\n  public static void main(String[] args) {\r\n    if (true) {\r\n      int a = 5; \/\/ Bi\u1ebfn c\u1ee5c b\u1ed9\r\n      System.out.println(a); \/\/ H\u1ee3p l\u1ec7\r\n    }\r\n    \/\/ System.out.println(a); \/\/ L\u1ed7i: a kh\u00f4ng c\u00f2n t\u1ed3n t\u1ea1i\r\n  }\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Bi\u1ebfn c\u1ee5c b\u1ed9 th\u01b0\u1eddng d\u00f9ng cho c\u00e1c thao t\u00e1c t\u1ea1m th\u1eddi, \u0111\u1ea3m b\u1ea3o b\u1ed9 nh\u1edb \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng hi\u1ec7u qu\u1ea3.<\/span><\/p>\n<h4><b> Bi\u1ebfn thu\u1ed9c t\u00ednh (Instance variables)<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb khi m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp \u0111\u01b0\u1ee3c t\u1ea1o.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Duy tr\u00ec trong su\u1ed1t v\u00f2ng \u0111\u1eddi c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Gi\u1ea3i ph\u00f3ng khi \u0111\u1ed1i t\u01b0\u1ee3ng kh\u00f4ng c\u00f2n tham chi\u1ebfu n\u00e0o v\u00e0 b\u1ecb Garbage Collector thu h\u1ed3i.<\/span><\/li>\n<\/ul>\n<p>V\u00ed d\u1ee5:<\/p>\n<pre>public class Main {\r\n  int b = 10; \/\/ Bi\u1ebfn thu\u1ed9c t\u00ednh\r\n\r\n  public static void main(String[] args) {\r\n    Main obj = new Main();\r\n    System.out.println(obj.b); \/\/ H\u1ee3p l\u1ec7 khi \u0111\u1ed1i t\u01b0\u1ee3ng t\u1ed3n t\u1ea1i\r\n  }\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Bi\u1ebfn thu\u1ed9c t\u00ednh h\u1eefu \u00edch khi c\u1ea7n l\u01b0u tr\u1eef tr\u1ea1ng th\u00e1i ho\u1eb7c d\u1eef li\u1ec7u li\u00ean quan \u0111\u1ebfn \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee5 th\u1ec3.<\/span><\/p>\n<h4><b> Bi\u1ebfn t\u0129nh (Static variables)<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb khi ch\u01b0\u01a1ng tr\u00ecnh b\u1eaft \u0111\u1ea7u v\u00e0 t\u1ed3n t\u1ea1i \u0111\u1ebfn khi ch\u01b0\u01a1ng tr\u00ecnh k\u1ebft th\u00fac.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u01b0\u1ee3c chia s\u1ebb b\u1edfi t\u1ea5t c\u1ea3 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp.<\/span><\/li>\n<\/ul>\n<p>V\u00ed d\u1ee5:<\/p>\n<pre>public class Main {\r\n  static int c = 50; \/\/ Bi\u1ebfn t\u0129nh\r\n\r\n  public static void main(String[] args) {\r\n    System.out.println(Main.c); \/\/ C\u00f3 th\u1ec3 truy c\u1eadp tr\u1ef1c ti\u1ebfp qua t\u00ean l\u1edbp\r\n  }\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Bi\u1ebfn t\u0129nh th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng cho c\u00e1c gi\u00e1 tr\u1ecb ho\u1eb7c th\u00f4ng tin d\u00f9ng chung trong to\u00e0n b\u1ed9 ch\u01b0\u01a1ng tr\u00ecnh.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Cac_loi_thuong_gap_khi_su_dung_bien_trong_Java\"><\/span><strong>C\u00e1c l\u1ed7i th\u01b0\u1eddng g\u1eb7p khi s\u1eed d\u1ee5ng bi\u1ebfn trong Java<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Khi s\u1eed d\u1ee5ng bi\u1ebfn trong Java, nh\u1eefng l\u1ed7i n\u00e0y th\u01b0\u1eddng x\u1ea3y ra do thi\u1ebfu hi\u1ec3u bi\u1ebft v\u1ec1 c\u00e1ch Java qu\u1ea3n l\u00fd b\u1ed9 nh\u1edb, ph\u1ea1m vi bi\u1ebfn, ho\u1eb7c khi x\u1eed l\u00fd c\u00e1c tr\u01b0\u1eddng h\u1ee3p \u0111a lu\u1ed3ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">D\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t s\u1ed1 l\u1ed7i ph\u1ed5 bi\u1ebfn c\u00f9ng gi\u1ea3i th\u00edch chi ti\u1ebft v\u00e0 c\u00e1ch kh\u1eafc ph\u1ee5c:<\/span><\/p>\n<h3><b>S\u1eed d\u1ee5ng bi\u1ebfn ch\u01b0a \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Java, c\u00e1c bi\u1ebfn c\u1ee5c b\u1ed9 (local variables) kh\u00f4ng \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o m\u1eb7c \u0111\u1ecbnh. N\u1ebfu ch\u01b0\u01a1ng tr\u00ecnh truy c\u1eadp v\u00e0o bi\u1ebfn ch\u01b0a \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o, m\u1ed9t l\u1ed7i bi\u00ean d\u1ecbch s\u1ebd x\u1ea3y ra. \u0110i\u1ec1u n\u00e0y gi\u00fap Java ng\u0103n ng\u1eeba c\u00e1c l\u1ed7i kh\u00f3 \u0111o\u00e1n \u0111\u1ecbnh trong th\u1eddi gian ch\u1ea1y.<\/span><\/p>\n<p>V\u00ed d\u1ee5 l\u1ed7i:<\/p>\n<pre>public class Main {\r\n  public static void main(String[] args) {\r\n    int number;\r\n    System.out.println(number); \/\/ L\u1ed7i: Bi\u1ebfn ch\u01b0a \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o\r\n  }\r\n}<\/pre>\n<p><b>Kh\u1eafc ph\u1ee5c:<\/b><span style=\"font-weight: 400;\"> \u0110\u1ea3m b\u1ea3o lu\u00f4n kh\u1edfi t\u1ea1o gi\u00e1 tr\u1ecb cho bi\u1ebfn tr\u01b0\u1edbc khi s\u1eed d\u1ee5ng. \u0110i\u1ec1u n\u00e0y kh\u00f4ng ch\u1ec9 gi\u00fap tr\u00e1nh l\u1ed7i bi\u00ean d\u1ecbch m\u00e0 c\u00f2n \u0111\u1ea3m b\u1ea3o ch\u01b0\u01a1ng tr\u00ecnh ho\u1ea1t \u0111\u1ed9ng \u0111\u00fang nh\u01b0 mong \u0111\u1ee3i.<\/span><\/p>\n<p>V\u00ed d\u1ee5 \u0111\u00fang:<\/p>\n<pre>public class Main {\r\n  public static void main(String[] args) {\r\n    int number = 0;\r\n    System.out.println(number);\r\n  }\r\n}<\/pre>\n<h3><b>Truy c\u1eadp bi\u1ebfn ngo\u00e0i ph\u1ea1m vi (scope)<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Bi\u1ebfn trong Java c\u00f3 ph\u1ea1m vi ho\u1ea1t \u0111\u1ed9ng nh\u1ea5t \u0111\u1ecbnh (scope). N\u1ebfu ch\u01b0\u01a1ng tr\u00ecnh c\u1ed1 g\u1eafng truy c\u1eadp bi\u1ebfn ngo\u00e0i ph\u1ea1m vi n\u00e0y, m\u1ed9t l\u1ed7i bi\u00ean d\u1ecbch s\u1ebd x\u1ea3y ra. \u0110\u00e2y l\u00e0 l\u1ed7i th\u01b0\u1eddng g\u1eb7p khi l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c kh\u1ed1i m\u00e3 nh\u01b0 v\u00f2ng l\u1eb7p, \u0111i\u1ec1u ki\u1ec7n, ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c.<\/span><\/p>\n<p>V\u00ed d\u1ee5 l\u1ed7i:<\/p>\n<pre>public class Main {\r\n  public static void main(String[] args) {\r\n    if (true) {\r\n      int x = 10;\r\n    }\r\n    System.out.println(x); \/\/ L\u1ed7i: Bi\u1ebfn x ngo\u00e0i ph\u1ea1m vi\r\n  }\r\n}<\/pre>\n<p><b>Kh\u1eafc ph\u1ee5c:<\/b><span style=\"font-weight: 400;\"> \u0110\u1ea3m b\u1ea3o r\u1eb1ng bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o trong ph\u1ea1m vi ho\u1ea1t \u0111\u1ed9ng ph\u00f9 h\u1ee3p. Khi c\u1ea7n s\u1eed d\u1ee5ng bi\u1ebfn trong nhi\u1ec1u ph\u1ea1m vi kh\u00e1c nhau, h\u00e3y khai b\u00e1o n\u00f3 \u1edf c\u1ea5p cao h\u01a1n.<\/span><\/p>\n<p>V\u00ed d\u1ee5 \u0111\u00fang:<\/p>\n<pre>public class Main {\r\n  public static void main(String[] args) {\r\n    int x = 10;\r\n    System.out.println(x);\r\n  }\r\n}<\/pre>\n<h3><b>Nh\u1ea7m l\u1eabn gi\u1eefa ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean thu\u1ef7 v\u00e0 ki\u1ec3u tham chi\u1ebfu<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y (primitive types) l\u01b0u tr\u1eef tr\u1ef1c ti\u1ebfp gi\u00e1 tr\u1ecb, trong khi ki\u1ec3u tham chi\u1ebfu (reference types) l\u01b0u \u0111\u1ecba ch\u1ec9 c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng trong b\u1ed9 nh\u1edb heap. S\u1ef1 nh\u1ea7m l\u1eabn gi\u1eefa hai ki\u1ec3u n\u00e0y c\u00f3 th\u1ec3 d\u1eabn \u0111\u1ebfn c\u00e1c l\u1ed7i logic, \u0111\u1eb7c bi\u1ec7t khi so s\u00e1nh gi\u00e1 tr\u1ecb.<\/span><\/p>\n<p>V\u00ed d\u1ee5 l\u1ed7i:<\/p>\n<pre>Integer a = 100;\r\nInteger b= 100;\r\nSystem.out.println(a == b); \/\/ K\u1ebft qu\u1ea3: true (trong ph\u1ea1m vi -128 \u0111\u1ebfn 127)\r\n\r\nInteger c = 200;\r\nInteger d = 200;\r\nSystem.out.println(c == d); \/\/ K\u1ebft qu\u1ea3: false (ngo\u00e0i ph\u1ea1m vi cache)<\/pre>\n<p><b>Kh\u1eafc ph\u1ee5c:<\/b><span style=\"font-weight: 400;\"> Thay v\u00ec s\u1eed d\u1ee5ng to\u00e1n t\u1eed <\/span><span style=\"font-weight: 400;\">==<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 so s\u00e1nh, h\u00e3y s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">equals()<\/span><span style=\"font-weight: 400;\"> khi l\u00e0m vi\u1ec7c v\u1edbi ki\u1ec3u tham chi\u1ebfu.<\/span><\/p>\n<p>V\u00ed d\u1ee5 \u0111\u00fang:<\/p>\n<pre>Integer c = 200;\r\nInteger d= 200;\r\nSystem.out.println(c.equals(d)); \/\/ K\u1ebft qu\u1ea3: true<\/pre>\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i ra, c\u1ea7n ch\u00fa \u00fd r\u1eb1ng c\u00e1c ki\u1ec3u nguy\u00ean th\u1ee7y c\u00f3 hi\u1ec7u su\u1ea5t cao h\u01a1n v\u00ec kh\u00f4ng li\u00ean quan \u0111\u1ebfn vi\u1ec7c qu\u1ea3n l\u00fd b\u1ed9 nh\u1edb heap, do \u0111\u00f3 n\u00ean \u01b0u ti\u00ean s\u1eed d\u1ee5ng ch\u00fang khi c\u00f3 th\u1ec3.<\/span><\/p>\n<h3><b>Thay \u0111\u1ed5i gi\u00e1 tr\u1ecb bi\u1ebfn trong m\u00f4i tr\u01b0\u1eddng \u0111a lu\u1ed3ng v\u00e0 kh\u00f4ng \u0111\u1ed3ng b\u1ed9<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Trong l\u1eadp tr\u00ecnh \u0111a lu\u1ed3ng, khi nhi\u1ec1u lu\u1ed3ng c\u00f9ng truy c\u1eadp v\u00e0 thay \u0111\u1ed5i gi\u00e1 tr\u1ecb c\u1ee7a m\u1ed9t bi\u1ebfn, l\u1ed7i &#8220;race condition&#8221; c\u00f3 th\u1ec3 x\u1ea3y ra. \u0110i\u1ec1u n\u00e0y l\u00e0m k\u1ebft qu\u1ea3 tr\u1edf n\u00ean kh\u00f4ng \u0111\u1ed3ng nh\u1ea5t ho\u1eb7c kh\u00f4ng th\u1ec3 d\u1ef1 \u0111o\u00e1n.<\/span><\/p>\n<p>V\u00ed d\u1ee5 l\u1ed7i:<\/p>\n<pre>public class Counter {\r\n  private int count = 0;\r\n\r\n  public void increment() {\r\n    count++;\r\n  }\r\n\r\n  public int getCount() {\r\n    return count;\r\n  }\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Trong m\u00f4i tr\u01b0\u1eddng \u0111a lu\u1ed3ng, n\u1ebfu kh\u00f4ng \u0111\u1ed3ng b\u1ed9 h\u00f3a truy c\u1eadp, c\u00e1c lu\u1ed3ng c\u00f3 th\u1ec3 ghi \u0111\u00e8 l\u1eabn nhau, d\u1eabn \u0111\u1ebfn k\u1ebft qu\u1ea3 sai l\u1ec7ch.<\/span><\/p>\n<p><b>Kh\u1eafc ph\u1ee5c:<\/b><span style=\"font-weight: 400;\"> S\u1eed d\u1ee5ng c\u00e1c c\u01a1 ch\u1ebf \u0111\u1ed3ng b\u1ed9 nh\u01b0 <\/span><span style=\"font-weight: 400;\">synchronized<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n d\u1eef li\u1ec7u.<\/span><\/p>\n<p>V\u00ed d\u1ee5 \u0111\u00fang:<\/p>\n<pre>public class Counter {\r\n  private int count = 0;\r\n\r\n  public synchronized void increment() {\r\n    count++;\r\n  }\r\n\r\n  public synchronized int getCount() {\r\n    return count;\r\n  }\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i ra, c\u00e1c l\u1edbp nh\u01b0 <\/span><span style=\"font-weight: 400;\">AtomicInteger<\/span><span style=\"font-weight: 400;\"> trong g\u00f3i <\/span><span style=\"font-weight: 400;\">java.util.concurrent<\/span><span style=\"font-weight: 400;\"> c\u0169ng cung c\u1ea5p c\u00e1ch gi\u1ea3i quy\u1ebft hi\u1ec7u qu\u1ea3 h\u01a1n cho c\u00e1c v\u1ea5n \u0111\u1ec1 li\u00ean quan \u0111\u1ebfn bi\u1ebfn trong m\u00f4i tr\u01b0\u1eddng \u0111a lu\u1ed3ng.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Meo_va_luu_y_khi_lam_viec_voi_bien\"><\/span><strong>M\u1eb9o v\u00e0 l\u01b0u \u00fd khi l\u00e0m vi\u1ec7c v\u1edbi bi\u1ebfn<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>\u0110\u1eb7t t\u00ean bi\u1ebfn r\u00f5 r\u00e0ng v\u00e0 d\u1ec5 hi\u1ec3u<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">T\u00ean bi\u1ebfn l\u00e0 y\u1ebfu t\u1ed1 quan tr\u1ecdng gi\u00fap m\u00e3 ngu\u1ed3n d\u1ec5 \u0111\u1ecdc v\u00e0 b\u1ea3o tr\u00ec. H\u00e3y \u0111\u1eb7t t\u00ean bi\u1ebfn sao cho m\u00f4 t\u1ea3 \u0111\u01b0\u1ee3c m\u1ee5c \u0111\u00edch ho\u1eb7c d\u1eef li\u1ec7u m\u00e0 bi\u1ebfn l\u01b0u tr\u1eef. Tr\u00e1nh s\u1eed d\u1ee5ng c\u00e1c t\u00ean kh\u00f4ng r\u00f5 r\u00e0ng nh\u01b0 <\/span><span style=\"font-weight: 400;\">x<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">y<\/span><span style=\"font-weight: 400;\">, ho\u1eb7c c\u00e1c k\u00fd hi\u1ec7u ng\u1eafn n\u1ebfu kh\u00f4ng c\u1ea7n thi\u1ebft.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 t\u1ed1t:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">int numberOfStudents = 25;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">String customerName = \"John Doe\";<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 ch\u01b0a t\u1ed1t:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">int n = 25;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">String cn = \u201cJohn Doe\u201d;<\/span><\/pre>\n<h3><b>T\u1ed1i \u01b0u ho\u00e1 vi\u1ec7c s\u1eed d\u1ee5ng b\u1ed9 nh\u1edb<\/b><b><\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Ch\u1ecdn ki\u1ec3u d\u1eef li\u1ec7u ph\u00f9 h\u1ee3p v\u1edbi nhu c\u1ea7u th\u1ef1c t\u1ebf \u0111\u1ec3 tr\u00e1nh l\u00e3ng ph\u00ed b\u1ed9 nh\u1edb. V\u00ed d\u1ee5, n\u1ebfu ch\u1ec9 c\u1ea7n l\u01b0u tr\u1eef m\u1ed9t s\u1ed1 nh\u1ecf, h\u00e3y s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">byte<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">short<\/span><span style=\"font-weight: 400;\"> thay v\u00ec <\/span><span style=\"font-weight: 400;\">int<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">long<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p>V\u00ed d\u1ee5:<\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">byte<\/span><span style=\"font-weight: 400;\"> cho c\u00e1c gi\u00e1 tr\u1ecb t\u1eeb -128 \u0111\u1ebfn 127.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">int<\/span><span style=\"font-weight: 400;\"> cho c\u00e1c s\u1ed1 nguy\u00ean l\u1edbn h\u01a1n.<\/span><\/li>\n<\/ul>\n<pre><span style=\"font-weight: 400;\">byte age = 30;\u00a0 \/\/ T\u1ed1i \u01b0u cho gi\u00e1 tr\u1ecb nh\u1ecf<\/span>\r\n\r\n<span style=\"font-weight: 400;\">int population = 1000000;\u00a0 \/\/ D\u00f9ng cho s\u1ed1 l\u1edbn h\u01a1n<\/span><b><\/b><\/pre>\n<h3><b>H\u1ea1n ch\u1ebf s\u1eed d\u1ee5ng bi\u1ebfn t\u0129nh kh\u00f4ng c\u1ea7n thi\u1ebft<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Bi\u1ebfn t\u0129nh (static) t\u1ed3n t\u1ea1i trong to\u00e0n b\u1ed9 v\u00f2ng \u0111\u1eddi c\u1ee7a ch\u01b0\u01a1ng tr\u00ecnh v\u00e0 chi\u1ebfm b\u1ed9 nh\u1edb li\u00ean t\u1ee5c. V\u00ec v\u1eady, ch\u1ec9 s\u1eed d\u1ee5ng bi\u1ebfn t\u0129nh khi c\u1ea7n l\u01b0u tr\u1eef th\u00f4ng tin d\u00f9ng chung v\u00e0 kh\u00f4ng thay \u0111\u1ed5i th\u01b0\u1eddng xuy\u00ean.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 p<\/span><span style=\"font-weight: 400;\">h\u00f9 h\u1ee3p: s\u1eed d\u1ee5ng bi\u1ebfn t\u0129nh cho c\u00e1c h\u1eb1ng s\u1ed1 chung<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">public class Config {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public static final String APP_NAME = \"My Application\";<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 kh\u00f4ng ph\u00f9 h\u1ee3p: s\u1eed d\u1ee5ng bi\u1ebfn t\u0129nh cho d\u1eef li\u1ec7u thay \u0111\u1ed5i li\u00ean t\u1ee5c<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">\/\/ Tr\u00e1nh vi\u1ec7c n\u00e0y<\/span>\r\n\r\n<span style=\"font-weight: 400;\">public static int counter = 0;<\/span><b><\/b><\/pre>\n<h3><b>Qu\u1ea3n l\u00fd v\u00e0 d\u1ecdn d\u1eb9p bi\u1ebfn \u0111\u00fang c\u00e1ch trong ch\u01b0\u01a1ng tr\u00ecnh\u00a0<\/b><\/h3>\n<h4><b>Gi\u1ea3m ph\u1ea1m vi c\u1ee7a bi\u1ebfn<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Ch\u1ec9 khai b\u00e1o bi\u1ebfn trong ph\u1ea1m vi c\u1ea7n thi\u1ebft \u0111\u1ec3 d\u1ec5 qu\u1ea3n l\u00fd v\u00e0 gi\u1ea3m nguy c\u01a1 l\u1ed7i. S\u1eed d\u1ee5ng bi\u1ebfn c\u1ee5c b\u1ed9 (local) thay v\u00ec bi\u1ebfn to\u00e0n c\u1ee5c (global) khi c\u00f3 th\u1ec3.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre>public void calculateSum() {\r\n  int sum = 0; \/\/ Bi\u1ebfn ch\u1ec9 s\u1eed d\u1ee5ng trong ph\u01b0\u01a1ng th\u1ee9c n\u00e0y\r\n  for (int i = 1; i &lt;= 10; i++) {\r\n    sum += i;\r\n  }\r\n  System.out.println(\"Sum: \" + sum);\r\n}<\/pre>\n<h4><b>Gi\u1ea3i ph\u00f3ng tham chi\u1ebfu kh\u00f4ng c\u1ea7n thi\u1ebft<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Khi m\u1ed9t bi\u1ebfn tham chi\u1ebfu kh\u00f4ng c\u00f2n c\u1ea7n thi\u1ebft, h\u00e3y g\u00e1n gi\u00e1 tr\u1ecb <\/span><span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 th\u00f4ng b\u00e1o cho b\u1ed9 thu gom r\u00e1c (Garbage Collector) gi\u1ea3i ph\u00f3ng b\u1ed9 nh\u1edb.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre>String data = \"Temporary data\";\r\ndata = null; \/\/ Gi\u1ea3i ph\u00f3ng b\u1ed9 nh\u1edb<\/pre>\n<h4><b>S\u1eed d\u1ee5ng c\u00f4ng c\u1ee5 ki\u1ec3m tra b\u1ed9 nh\u1edb<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">C\u00e1c c\u00f4ng c\u1ee5 nh\u01b0 VisualVM ho\u1eb7c Eclipse Memory Analyzer c\u00f3 th\u1ec3 gi\u00fap b\u1ea1n ph\u00e1t hi\u1ec7n v\u00e0 kh\u1eafc ph\u1ee5c r\u00f2 r\u1ec9 b\u1ed9 nh\u1edb do qu\u1ea3n l\u00fd bi\u1ebfn kh\u00f4ng hi\u1ec7u qu\u1ea3.<\/span><\/p>\n<blockquote><p><em>\u0110\u1ecdc th\u00eam: <a href=\"https:\/\/itviec.com\/blog\/java-ide-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>Top 6+ ph\u1ea7n m\u1ec1m code Java t\u1ed1t nh\u1ea5t<\/strong><\/a><\/em><\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Tong_ket\"><\/span><span style=\"font-weight: 400;\"><strong>T\u1ed5ng k\u1ebft<\/strong>\u00a0<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Bi\u1ebfn trong Java l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m n\u1ec1n t\u1ea3ng, \u0111\u00f3ng vai tr\u00f2 quan tr\u1ecdng trong vi\u1ec7c l\u01b0u tr\u1eef v\u00e0 x\u1eed l\u00fd d\u1eef li\u1ec7u. Th\u00f4ng qua b\u00e0i vi\u1ebft n\u00e0y, ch\u00fang ta \u0111\u00e3 t\u00ecm hi\u1ec3u v\u1ec1 c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u c\u1ee7a bi\u1ebfn, t\u1eeb ki\u1ec3u nguy\u00ean th\u1ee7y nh\u01b0 <\/span><span style=\"font-weight: 400;\">int<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">float<\/span><span style=\"font-weight: 400;\">, \u0111\u1ebfn ki\u1ec3u tham chi\u1ebfu nh\u01b0 <\/span><span style=\"font-weight: 400;\">String<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">array<\/span><span style=\"font-weight: 400;\">, v\u00e0 \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Vi\u1ec7c s\u1eed d\u1ee5ng bi\u1ebfn \u0111\u00fang c\u00e1ch gi\u00fap t\u1ed1i \u01b0u h\u00f3a b\u1ed9 nh\u1edb, t\u0103ng t\u00ednh hi\u1ec7u qu\u1ea3 cho ch\u01b0\u01a1ng tr\u00ecnh, \u0111\u1ed3ng th\u1eddi gi\u00fap m\u00e3 ngu\u1ed3n tr\u1edf n\u00ean d\u1ec5 \u0111\u1ecdc v\u00e0 b\u1ea3o tr\u00ec h\u01a1n. \u0110\u1ec3 tr\u1edf th\u00e0nh m\u1ed9t l\u1eadp tr\u00ecnh vi\u00ean Java gi\u1ecfi, vi\u1ec7c hi\u1ec3u s\u00e2u kh\u00e1i ni\u1ec7m bi\u1ebfn v\u00e0 c\u00e1ch v\u1eadn d\u1ee5ng ch\u00fang l\u00e0 b\u01b0\u1edbc \u0111i \u0111\u1ea7u ti\u00ean nh\u01b0ng kh\u00f4ng k\u00e9m ph\u1ea7n quan tr\u1ecdng.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bi\u1ebfn l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m c\u1ed1t l\u00f5i trong l\u1eadp tr\u00ecnh Java, \u0111\u00f3ng vai tr\u00f2 l\u01b0u tr\u1eef v\u00e0 qu\u1ea3n l\u00fd d\u1eef li\u1ec7u trong su\u1ed1t qu\u00e1 tr\u00ecnh th\u1ef1c thi ch\u01b0\u01a1ng tr\u00ecnh. Vi\u1ec7c hi\u1ec3u r\u00f5 b\u1ea3n ch\u1ea5t c\u1ee7a bi\u1ebfn gi\u00fap l\u1eadp tr\u00ecnh vi\u00ean khai th\u00e1c t\u1ed1i \u0111a kh\u1ea3 n\u0103ng c\u1ee7a Java \u0111\u1ec3 x\u00e2y d\u1ef1ng c\u00e1c \u1ee9ng d\u1ee5ng linh [&hellip;]<\/p>\n","protected":false},"author":203,"featured_media":83658,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109],"tags":[],"class_list":["post-83642","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it"],"blocksy_meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.7) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Bi\u1ebfn trong Java: Chi ti\u1ebft h\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng \u0111\u00fang c\u00e1ch - ITviec Blog<\/title>\n<meta name=\"description\" content=\"Kh\u00e1m ph\u00e1 v\u1ec1 bi\u1ebfn trong Java, vai tr\u00f2 quan tr\u1ecdng c\u1ee7a ch\u00fang, v\u00e0 c\u00e1ch s\u1eed d\u1ee5ng bi\u1ebfn \u0111\u00fang c\u00e1ch \u0111\u1ec3 vi\u1ebft m\u00e3 ngu\u1ed3n r\u00f5 r\u00e0ng, t\u1ed1i \u01b0u v\u00e0 d\u1ec5 b\u1ea3o tr\u00ec.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itviec.com\/blog\/bien-trong-java\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bi\u1ebfn trong Java: Chi ti\u1ebft h\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng \u0111\u00fang c\u00e1ch\" \/>\n<meta property=\"og:description\" content=\"Bi\u1ebfn l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m c\u1ed1t l\u00f5i trong l\u1eadp tr\u00ecnh Java, \u0111\u00f3ng vai tr\u00f2 l\u01b0u tr\u1eef v\u00e0 qu\u1ea3n l\u00fd d\u1eef li\u1ec7u trong su\u1ed1t qu\u00e1 tr\u00ecnh th\u1ef1c thi ch\u01b0\u01a1ng tr\u00ecnh. Vi\u1ec7c hi\u1ec3u r\u00f5 b\u1ea3n\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/bien-trong-java\/\" \/>\n<meta property=\"og:site_name\" content=\"ITviec Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ITviec\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-31T14:06:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/12\/Bien-trong-Java-vippro.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1500\" \/>\n\t<meta property=\"og:image:height\" content=\"790\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Tien Tran\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ITviec\" \/>\n<meta name=\"twitter:site\" content=\"@ITviec\" \/>\n<meta name=\"twitter:label1\" content=\"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tien Tran\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"22 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Bi\u1ebfn trong Java: Chi ti\u1ebft h\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng \u0111\u00fang c\u00e1ch - ITviec Blog","description":"Kh\u00e1m ph\u00e1 v\u1ec1 bi\u1ebfn trong Java, vai tr\u00f2 quan tr\u1ecdng c\u1ee7a ch\u00fang, v\u00e0 c\u00e1ch s\u1eed d\u1ee5ng bi\u1ebfn \u0111\u00fang c\u00e1ch \u0111\u1ec3 vi\u1ebft m\u00e3 ngu\u1ed3n r\u00f5 r\u00e0ng, t\u1ed1i \u01b0u v\u00e0 d\u1ec5 b\u1ea3o tr\u00ec.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/itviec.com\/blog\/bien-trong-java\/","og_locale":"vi_VN","og_type":"article","og_title":"Bi\u1ebfn trong Java: Chi ti\u1ebft h\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng \u0111\u00fang c\u00e1ch","og_description":"Bi\u1ebfn l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m c\u1ed1t l\u00f5i trong l\u1eadp tr\u00ecnh Java, \u0111\u00f3ng vai tr\u00f2 l\u01b0u tr\u1eef v\u00e0 qu\u1ea3n l\u00fd d\u1eef li\u1ec7u trong su\u1ed1t qu\u00e1 tr\u00ecnh th\u1ef1c thi ch\u01b0\u01a1ng tr\u00ecnh. Vi\u1ec7c hi\u1ec3u r\u00f5 b\u1ea3n","og_url":"https:\/\/itviec.com\/blog\/bien-trong-java\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2024-12-31T14:06:42+00:00","og_image":[{"width":1500,"height":790,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/12\/Bien-trong-Java-vippro.jpg","type":"image\/jpeg"}],"author":"Tien Tran","twitter_card":"summary_large_image","twitter_creator":"@ITviec","twitter_site":"@ITviec","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"Tien Tran","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"22 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/bien-trong-java\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/bien-trong-java\/"},"author":{"name":"Tien Tran","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/1595d671c49cfa2a48cd3c0a047a1298"},"headline":"Bi\u1ebfn trong Java: Chi ti\u1ebft h\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng \u0111\u00fang c\u00e1ch","datePublished":"2024-12-31T14:06:42+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/bien-trong-java\/"},"wordCount":6016,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/bien-trong-java\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/12\/Bien-trong-Java-vippro.jpg","articleSection":["Chuy\u00ean m\u00f4n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/bien-trong-java\/","url":"https:\/\/itviec.com\/blog\/bien-trong-java\/","name":"Bi\u1ebfn trong Java: Chi ti\u1ebft h\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng \u0111\u00fang c\u00e1ch - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/bien-trong-java\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/bien-trong-java\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/12\/Bien-trong-Java-vippro.jpg","datePublished":"2024-12-31T14:06:42+00:00","description":"Kh\u00e1m ph\u00e1 v\u1ec1 bi\u1ebfn trong Java, vai tr\u00f2 quan tr\u1ecdng c\u1ee7a ch\u00fang, v\u00e0 c\u00e1ch s\u1eed d\u1ee5ng bi\u1ebfn \u0111\u00fang c\u00e1ch \u0111\u1ec3 vi\u1ebft m\u00e3 ngu\u1ed3n r\u00f5 r\u00e0ng, t\u1ed1i \u01b0u v\u00e0 d\u1ec5 b\u1ea3o tr\u00ec.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/bien-trong-java\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/bien-trong-java\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/bien-trong-java\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/12\/Bien-trong-Java-vippro.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/12\/Bien-trong-Java-vippro.jpg","width":1500,"height":790,"caption":"Bie\u0302\u0301n trong Java - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/bien-trong-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Chuy\u00ean m\u00f4n IT","item":"https:\/\/itviec.com\/blog\/chuyen-mon-it\/"},{"@type":"ListItem","position":2,"name":"Bi\u1ebfn trong Java: Chi ti\u1ebft h\u01b0\u1edbng d\u1eabn s\u1eed d\u1ee5ng \u0111\u00fang c\u00e1ch"}]},{"@type":"WebSite","@id":"https:\/\/itviec.com\/blog\/#website","url":"https:\/\/itviec.com\/blog\/","name":"ITviec Blog","description":"IT Jobs &amp; People in Vietnam","publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itviec.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"vi"},{"@type":"Organization","@id":"https:\/\/itviec.com\/blog\/#organization","name":"ITviec","url":"https:\/\/itviec.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","width":1800,"height":1800,"caption":"ITviec"},"image":{"@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ITviec","https:\/\/x.com\/ITviec","https:\/\/www.linkedin.com\/company\/itviec","https:\/\/www.youtube.com\/channel\/UCYthAQ3bcGr57M_ag5gHDvQ"]},{"@type":"Person","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/1595d671c49cfa2a48cd3c0a047a1298","name":"Tien Tran","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/05\/tien-tran-author-e1715658627643-100x100.jpg","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/05\/tien-tran-author-e1715658627643-100x100.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/05\/tien-tran-author-e1715658627643-100x100.jpg","caption":"Tien Tran"},"url":"https:\/\/itviec.com\/blog\/author\/tien-tran\/"}]}},"_links":{"self":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/83642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/users\/203"}],"replies":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/comments?post=83642"}],"version-history":[{"count":0,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/83642\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/83658"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=83642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=83642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=83642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}