{"id":82081,"date":"2024-11-29T11:48:58","date_gmt":"2024-11-29T04:48:58","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=82081"},"modified":"2025-09-27T23:23:38","modified_gmt":"2025-09-27T16:23:38","slug":"cau-hoi-phong-van-java","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-java\/","title":{"rendered":"Top 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java nh\u1ea5t \u0111\u1ecbnh c\u00f3 trong bu\u1ed5i ph\u1ecfng v\u1ea5n"},"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\/cau-hoi-phong-van-java\/#Java_Developer_la_ai\" >Java Developer l\u00e0 ai?<\/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\/cau-hoi-phong-van-java\/#Cac_cau_hoi_phong_van_Java_lien_quan_den_Java_co_ban\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java li\u00ean quan \u0111\u1ebfn Java c\u01a1 b\u1ea3n<\/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\/cau-hoi-phong-van-java\/#Cac_cau_hoi_phong_van_Java_lien_quan_den_OOP\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java li\u00ean quan \u0111\u1ebfn OOP<\/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\/cau-hoi-phong-van-java\/#Cac_cau_hoi_phong_van_Java_lien_quan_den_Java_Collections_Framework\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java li\u00ean quan \u0111\u1ebfn Java Collections Framework<\/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\/cau-hoi-phong-van-java\/#Cac_cau_hoi_phong_van_Java_lien_quan_den_Exception_Handling_Xu_ly_ngoai_le\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java li\u00ean quan \u0111\u1ebfn Exception Handling (X\u1eed l\u00fd ngo\u1ea1i l\u1ec7)<\/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\/cau-hoi-phong-van-java\/#Cac_cau_hoi_phong_van_Java_lien_quan_den_Java_Multithreading_Da_luong_trong_Java\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java li\u00ean quan \u0111\u1ebfn Java Multithreading (\u0110a lu\u1ed3ng 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\/cau-hoi-phong-van-java\/#Cac_cau_hoi_phong_van_Java_lien_quan_den_Java_Stream_API\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java li\u00ean quan \u0111\u1ebfn Java Stream API<\/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\/cau-hoi-phong-van-java\/#Cac_cau_hoi_phong_van_Java_ve_tinh_nang_noi_bat_cua_Java_8_moi_nhat\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java v\u1ec1 t\u00ednh n\u0103ng n\u1ed5i b\u1eadt c\u1ee7a Java 8 m\u1edbi nh\u1ea5t<\/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\/cau-hoi-phong-van-java\/#Tong_ket_cau_hoi_phong_van_Java\" >T\u1ed5ng k\u1ebft c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java<\/a><\/li><\/ul><\/nav><\/div>\n\n<p><em><strong>T\u1ed5ng h\u1ee3p 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java ph\u1ed5 bi\u1ebfn d\u00e0nh cho c\u00e1c l\u1eadp tr\u00ecnh vi\u00ean Java, t\u1eeb nh\u1eefng c\u00e2u h\u1ecfi c\u01a1 b\u1ea3n v\u1ec1 c\u00fa ph\u00e1p v\u00e0 t\u00ednh n\u0103ng ng\u00f4n ng\u1eef \u0111\u1ebfn nh\u1eefng v\u1ea5n \u0111\u1ec1 n\u00e2ng cao nh\u01b0 x\u1eed l\u00fd \u0111\u1ed3ng th\u1eddi, t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t v\u00e0 hi\u1ec3u bi\u1ebft v\u1ec1 c\u00e1c framework ph\u1ed5 bi\u1ebfn.<\/strong><\/em><\/p>\n\n\n\n<p>\u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u th\u00eam v\u1ec1:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java v\u1ec1 Java Core Basics (C\u01a1 b\u1ea3n v\u1ec1 Java)<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java li\u00ean quan \u0111\u1ebfn l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng trong Java<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java li\u00ean quan \u0111\u1ebfn c\u00e1c th\u01b0 vi\u1ec7n \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng ph\u1ed5 bi\u1ebfn trong Java<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java v\u1ec1 x\u1eed l\u00fd ngo\u1ea1i l\u1ec7(Exception Handling)<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java v\u1ec1 x\u1eed l\u00fd \u0111a lu\u1ed3ng<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java li\u00ean quan \u0111\u1ebfn Java Stream API v\u00e0 c\u00e1c t\u00ednh n\u0103ng n\u1ed5i b\u1eadt c\u1ee7a Java 8<\/span><\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Tr\u01b0\u1edbc khi th\u1ef1c h\u00e0nh c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java, b\u1ea1n \u0111\u00e3 \u1ee9ng tuy\u1ec3n <strong><a href=\"https:\/\/itviec.com\/it-jobs\/java?utm_medium=anchor_text_high&amp;utm_source=blog&amp;utm_campaign=viec_lam_java&amp;utm_content=cau_hoi_phong_van_java\" target=\"_blank\" rel=\"noreferrer noopener\">vi\u1ec7c l\u00e0m Java<\/a><\/strong> m\u1edbi nh\u1ea5t tr\u00ean ITviec ch\u01b0a?<\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-java-developer-la-ai\"><span class=\"ez-toc-section\" id=\"Java_Developer_la_ai\"><\/span><strong>Java Developer l\u00e0 ai?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">Java Developer \u0111\u00f3ng vai tr\u00f2 ch\u1ee7 ch\u1ed1t trong vi\u1ec7c thi\u1ebft k\u1ebf, ph\u00e1t tri\u1ec3n, ki\u1ec3m th\u1eed v\u00e0 b\u1ea3o tr\u00ec c\u00e1c \u1ee9ng d\u1ee5ng web, h\u1ec7 th\u1ed1ng doanh nghi\u1ec7p v\u00e0 n\u1ec1n t\u1ea3ng di \u0111\u1ed9ng, \u0111\u1ea3m b\u1ea3o \u1ee9ng d\u1ee5ng \u0111\u00e1p \u1ee9ng c\u00e1c y\u00eau c\u1ea7u k\u1ef9 thu\u1eadt m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Java Developer c\u1ea7n th\u00e0nh th\u1ea1o ng\u00f4n ng\u1eef Java, l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng, v\u00e0 c\u00e1c c\u00f4ng ngh\u1ec7 nh\u01b0 Spring, Hibernate, c\u00f9ng ki\u1ebfn th\u1ee9c v\u1ec1 c\u01a1 s\u1edf d\u1eef li\u1ec7u SQL. K\u1ef9 n\u0103ng gi\u1ea3i quy\u1ebft v\u1ea5n \u0111\u1ec1, l\u00e0m vi\u1ec7c nh\u00f3m, v\u00e0 kh\u1ea3 n\u0103ng t\u1ef1 h\u1ecdc \u0111\u1ec3 c\u1eadp nh\u1eadt c\u00f4ng ngh\u1ec7 m\u1edbi l\u00e0 r\u1ea5t c\u1ea7n thi\u1ebft, gi\u00fap Java Developer t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t \u1ee9ng d\u1ee5ng v\u00e0 \u0111\u1ea3m b\u1ea3o ch\u1ea5t l\u01b0\u1ee3ng s\u1ea3n ph\u1ea9m ph\u1ea7n m\u1ec1m.<\/span><\/p>\n\n\n\n<p>\u0110\u1ecdc th\u00eam c\u00e1c b\u00e0i vi\u1ebft sau \u0111\u1ec3 n\u1eafm v\u1eefng t\u1ed5ng quan v\u1ec1 Java v\u00e0 Java Developer:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/itviec.com\/blog\/java-la-gi\/\" target=\"_blank\" rel=\"noopener\">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<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/itviec.com\/blog\/java-developer\/\" target=\"_blank\" rel=\"noopener\">Java Developer: Nhi\u1ec7m v\u1ee5, k\u1ef9 n\u0103ng v\u00e0 l\u1ed9 tr\u00ecnh s\u1ef1 nghi\u1ec7p<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/itviec.com\/blog\/hoc-java-de-lam-gi\/\" target=\"_blank\" rel=\"noopener\">H\u1ecdc Java \u0111\u1ec3 l\u00e0m g\u00ec? C\u00e1c v\u1ecb tr\u00ed c\u00f3 th\u1ec3 l\u00e0m sau khi h\u1ecdc Java<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/itviec.com\/blog\/java-backend-la-gi\/\" target=\"_blank\" rel=\"noopener\">Java Backend: L\u1ed9 tr\u00ecnh h\u1ecdc Java Backend chi ti\u1ebft A-Z<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cac-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-java-lien-quan-d\u1ebfn-java-c\u01a1-b\u1ea3n\"><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Java_lien_quan_den_Java_co_ban\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java li\u00ean quan \u0111\u1ebfn Java c\u01a1 b\u1ea3n<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-li\u1ec7t-ke-cac-ki\u1ec3u-d\u1eef-li\u1ec7u-trong-java\"><strong>Li\u1ec7t k\u00ea c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u trong Java<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong Java, c\u00e1c lo\u1ea1i d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c chia th\u00e0nh hai nh\u00f3m ch\u00ednh: <\/span><b>Ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y (Primitive Data Type)<\/b><span style=\"font-weight: 400;\"> v\u00e0 <\/span><b>Ki\u1ec3u d\u1eef li\u1ec7u tham chi\u1ebfu (Reference Data Type)<\/b><span style=\"font-weight: 400;\">.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-ki\u1ec3u-d\u1eef-li\u1ec7u-nguyen-th\u1ee7y-primitive-data-type\"><b>Ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y (Primitive Data Type)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00e1c ki\u1ec3u d\u1eef li\u1ec7u n\u00e0y l\u01b0u tr\u1eef gi\u00e1 tr\u1ecb tr\u1ef1c ti\u1ebfp v\u00e0 \u0111\u01b0\u1ee3c chia th\u00e0nh 8 lo\u1ea1i c\u01a1 b\u1ea3n:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Lo\u1ea1i&nbsp;<\/b><\/td><td><b>Mi\u00eau t\u1ea3&nbsp;<\/b><\/td><td><b>K\u00edch th\u01b0\u1edbc&nbsp;<\/b><\/td><td><b>Gi\u00e1 tr\u1ecb m\u1eb7c <\/b><b>\u0111\u1ecbnh<\/b><\/td><\/tr><tr><td><b>Byte<\/b><\/td><td><span style=\"font-weight: 400;\">S\u1ed1 nguy\u00ean, t\u1eeb -128 \u0111\u1ebfn 127<\/span><\/td><td><span style=\"font-weight: 400;\">1 byte<\/span><\/td><td><span style=\"font-weight: 400;\">0<\/span><\/td><\/tr><tr><td><b>Short<\/b><\/td><td><span style=\"font-weight: 400;\">S\u1ed1 nguy\u00ean, t\u1eeb -32,768 \u0111\u1ebfn 32,767<\/span><\/td><td><span style=\"font-weight: 400;\">2 byte<\/span><\/td><td><span style=\"font-weight: 400;\">0<\/span><\/td><\/tr><tr><td><b>Int<\/b><\/td><td><span style=\"font-weight: 400;\">\u1ed1 nguy\u00ean, t\u1eeb -2^31 \u0111\u1ebfn 2^31-1<\/span><\/td><td><span style=\"font-weight: 400;\">4 byte<\/span><\/td><td><span style=\"font-weight: 400;\">0<\/span><\/td><\/tr><tr><td><b>Long&nbsp;<\/b><\/td><td><span style=\"font-weight: 400;\">S\u1ed1 nguy\u00ean, t\u1eeb -2^63 \u0111\u1ebfn 2^63-1<\/span><\/td><td><span style=\"font-weight: 400;\">8 byte<\/span><\/td><td><span style=\"font-weight: 400;\">0L<\/span><\/td><\/tr><tr><td><b>Float<\/b><\/td><td><span style=\"font-weight: 400;\">S\u1ed1 th\u1ef1c, \u0111\u1ed9 ch\u00ednh x\u00e1c \u0111\u01a1n<\/span><\/td><td><span style=\"font-weight: 400;\">4 byte&nbsp;<\/span><\/td><td><span style=\"font-weight: 400;\">0.0f<\/span><\/td><\/tr><tr><td><b>Double&nbsp;<\/b><\/td><td><span style=\"font-weight: 400;\">S\u1ed1 th\u1ef1c, \u0111\u1ed9 ch\u00ednh x\u00e1c k\u00e9p<\/span><\/td><td><span style=\"font-weight: 400;\">8 byte<\/span><\/td><td><span style=\"font-weight: 400;\">0.0d<\/span><\/td><\/tr><tr><td><b>Char<\/b><\/td><td><span style=\"font-weight: 400;\">K\u00fd t\u1ef1 Unicode<\/span><\/td><td><span style=\"font-weight: 400;\">2 byte<\/span><\/td><td><span style=\"font-weight: 400;\">&#8216;\\u0000&#8217;<\/span><\/td><\/tr><tr><td><b>Boolean<\/b><\/td><td><span style=\"font-weight: 400;\">Gi\u00e1 tr\u1ecb \u0111\u00fang ho\u1eb7c sai<\/span><\/td><td><span style=\"font-weight: 400;\">1 bit<\/span><\/td><td><span style=\"font-weight: 400;\">false<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-ki\u1ec3u-d\u1eef-li\u1ec7u-tham-chi\u1ebfu-reference-data-type\"><b>Ki\u1ec3u d\u1eef li\u1ec7u tham chi\u1ebfu (Reference Data Type)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00e1c ki\u1ec3u d\u1eef li\u1ec7u n\u00e0y l\u01b0u tr\u1eef \u0111\u1ecba ch\u1ec9 (tham chi\u1ebfu) c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng trong b\u1ed9 nh\u1edb. Ch\u00fang bao g\u1ed3m:<\/span><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Lo\u1ea1i&nbsp;<\/b><\/td><td><b>Mi\u00eau t\u1ea3<\/b><\/td><\/tr><tr><td><b>String<\/b><\/td><td><span style=\"font-weight: 400;\">Chu\u1ed7i k\u00fd t\u1ef1<\/span><\/td><\/tr><tr><td><b>Array<\/b><\/td><td><span style=\"font-weight: 400;\">M\u1ea3ng c\u00e1c ph\u1ea7n t\u1eed c\u00f9ng ki\u1ec3u<\/span><\/td><\/tr><tr><td><b>List<\/b><\/td><td><span style=\"font-weight: 400;\">\u0110\u1ecbnh ngh\u0129a c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng<\/span><\/td><\/tr><tr><td><b>Interface<\/b><\/td><td><span style=\"font-weight: 400;\">\u0110\u1ecbnh ngh\u0129a h\u00e0nh vi cho c\u00e1c l\u1edbp<\/span><\/td><\/tr><tr><td><b>Enum&nbsp;<\/b><\/td><td><span style=\"font-weight: 400;\">Ki\u1ec3u li\u1ec7t k\u00ea, \u0111\u1ea1i di\u1ec7n cho t\u1eadp c\u00e1c h\u1eb1ng s\u1ed1<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">Ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y l\u00e0 c\u00e1c lo\u1ea1i d\u1eef li\u1ec7u c\u01a1 b\u1ea3n v\u00e0 nhanh, trong khi ki\u1ec3u d\u1eef li\u1ec7u tham chi\u1ebfu \u0111\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng ph\u1ee9c t\u1ea1p.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-tinh-nang-n\u1ed5i-b\u1eadt-c\u1ee7a-java-so-v\u1edbi-cac-ngon-ng\u1eef-khac\"><strong>T\u00ednh n\u0103ng n\u1ed5i b\u1eadt c\u1ee7a Java so v\u1edbi c\u00e1c ng\u00f4n ng\u1eef kh\u00e1c?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Java n\u1ed5i b\u1eadt v\u1edbi nhi\u1ec1u t\u00ednh n\u0103ng khi\u1ebfn n\u00f3 tr\u1edf th\u00e0nh m\u1ed9t trong nh\u1eefng ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh ph\u1ed5 bi\u1ebfn nh\u1ea5t, \u0111\u1eb7c bi\u1ec7t l\u00e0 trong c\u00e1c \u1ee9ng d\u1ee5ng doanh nghi\u1ec7p v\u00e0 h\u1ec7 th\u1ed1ng l\u1edbn. M\u1ed9t s\u1ed1 t\u00ednh n\u0103ng ch\u00ednh bao g\u1ed3m:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>N\u1ec1n t\u1ea3ng \u0111\u1ed9c l\u1eadp<\/b><span style=\"font-weight: 400;\">: Java ch\u1ea1y tr\u00ean n\u1ec1n t\u1ea3ng <\/span><i><span style=\"font-weight: 400;\">Java Virtual Machine (JVM)<\/span><\/i><span style=\"font-weight: 400;\">, cho ph\u00e9p m\u00e3 Java c\u00f3 th\u1ec3 ch\u1ea1y tr\u00ean nhi\u1ec1u h\u1ec7 \u0111i\u1ec1u h\u00e0nh kh\u00e1c nhau m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i thay \u0111\u1ed5i. \u0110i\u1ec1u n\u00e0y gi\u00fap Java c\u00f3 kh\u1ea3 n\u0103ng &#8220;vi\u1ebft m\u1ed9t l\u1ea7n, ch\u1ea1y m\u1ecdi n\u01a1i&#8221; (<\/span><i><span style=\"font-weight: 400;\">Write Once, Run Anywhere<\/span><\/i><span style=\"font-weight: 400;\">).<\/span><\/li>\n\n\n\n<li><b>Qu\u1ea3n l\u00fd b\u1ed9 nh\u1edb t\u1ef1 \u0111\u1ed9ng<\/b><span style=\"font-weight: 400;\">: Java t\u00edch h\u1ee3p c\u01a1 ch\u1ebf <\/span><i><span style=\"font-weight: 400;\">garbage collection<\/span><\/i><span style=\"font-weight: 400;\">, gi\u00fap gi\u1ea3i ph\u00f3ng b\u1ed9 nh\u1edb t\u1ef1 \u0111\u1ed9ng khi c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng kh\u00f4ng c\u00f2n \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng. \u0110i\u1ec1u n\u00e0y gi\u1ea3m thi\u1ec3u nguy c\u01a1 r\u00f2 r\u1ec9 b\u1ed9 nh\u1edb v\u00e0 gi\u00fap t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t \u1ee9ng d\u1ee5ng.<\/span><\/li>\n\n\n\n<li><b>B\u1ea3o m\u1eadt cao<\/b><span style=\"font-weight: 400;\">: Java c\u00f3 c\u00e1c t\u00ednh n\u0103ng b\u1ea3o m\u1eadt nh\u01b0 ki\u1ec3m so\u00e1t truy c\u1eadp, m\u00e3 h\u00f3a v\u00e0 <\/span><i><span style=\"font-weight: 400;\">sandboxing<\/span><\/i><span style=\"font-weight: 400;\"> gi\u00fap b\u1ea3o v\u1ec7 d\u1eef li\u1ec7u v\u00e0 h\u1ec7 th\u1ed1ng, \u0111\u1eb7c bi\u1ec7t quan tr\u1ecdng khi ch\u1ea1y c\u00e1c \u1ee9ng d\u1ee5ng trong m\u00f4i tr\u01b0\u1eddng m\u1ea1ng.<\/span><\/li>\n\n\n\n<li><b>H\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng<\/b><span style=\"font-weight: 400;\">: Java l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef ho\u00e0n to\u00e0n h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng, gi\u00fap m\u00e3 ngu\u1ed3n d\u1ec5 b\u1ea3o tr\u00ec, m\u1edf r\u1ed9ng v\u00e0 t\u00e1i s\u1eed d\u1ee5ng. \u0110i\u1ec1u n\u00e0y r\u1ea5t c\u00f3 l\u1ee3i cho vi\u1ec7c ph\u00e1t tri\u1ec3n c\u00e1c d\u1ef1 \u00e1n l\u1edbn.<\/span><\/li>\n\n\n\n<li><b>\u0110a lu\u1ed3ng (Multithreading)<\/b><span style=\"font-weight: 400;\">: Java h\u1ed7 tr\u1ee3 l\u1eadp tr\u00ecnh \u0111a lu\u1ed3ng, cho ph\u00e9p th\u1ef1c thi nhi\u1ec1u t\u00e1c v\u1ee5 \u0111\u1ed3ng th\u1eddi, gi\u00fap c\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t v\u00e0 kh\u1ea3 n\u0103ng \u0111\u00e1p \u1ee9ng c\u1ee7a \u1ee9ng d\u1ee5ng.<\/span><\/li>\n\n\n\n<li><b>Th\u01b0 vi\u1ec7n phong ph\u00fa v\u00e0 c\u1ed9ng \u0111\u1ed3ng m\u1ea1nh m\u1ebd<\/b><span style=\"font-weight: 400;\">: Java c\u00f3 th\u01b0 vi\u1ec7n chu\u1ea9n phong ph\u00fa v\u00e0 c\u1ed9ng \u0111\u1ed3ng l\u1eadp tr\u00ecnh vi\u00ean r\u1ed9ng l\u1edbn, gi\u00fap vi\u1ec7c ph\u00e1t tri\u1ec3n v\u00e0 x\u1eed l\u00fd c\u00e1c v\u1ea5n \u0111\u1ec1 tr\u1edf n\u00ean thu\u1eadn ti\u1ec7n h\u01a1n.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">Nh\u1eefng t\u00ednh n\u0103ng n\u00e0y gi\u00fap Java \u0111\u00e1p \u1ee9ng t\u1ed1t c\u00e1c y\u00eau c\u1ea7u t\u1eeb c\u00e1c h\u1ec7 th\u1ed1ng ph\u1ee9c t\u1ea1p, \u0111\u00f2i h\u1ecfi hi\u1ec7u n\u0103ng, b\u1ea3o m\u1eadt v\u00e0 kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng cao.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-cac-cong-c\u1ee5-phat-tri\u1ec3n-nh\u01b0-jvm-jre-va-jdk-khac-nhau-nh\u01b0-th\u1ebf-nao\"><strong>C\u00e1c c\u00f4ng c\u1ee5 ph\u00e1t tri\u1ec3n nh\u01b0 JVM, JRE, v\u00e0 JDK kh\u00e1c nhau nh\u01b0 th\u1ebf n\u00e0o?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Th\u00e0nh ph\u1ea7n&nbsp;<\/b><\/td><td><b>M\u00f4 t\u1ea3<\/b><\/td><td><b>M\u1ee5c \u0111\u00edch&nbsp;<\/b><\/td><\/tr><tr><td><b>JVM (Java Virtual Machine)<\/b><\/td><td><span style=\"font-weight: 400;\">Java Virtual Machine l\u00e0 m\u00e1y \u1ea3o ch\u1ea1y m\u00e3 bytecode c\u1ee7a Java. N\u00f3 th\u1ef1c hi\u1ec7n qu\u00e1 tr\u00ecnh bi\u00ean d\u1ecbch v\u00e0 t\u1ed1i \u01b0u h\u00f3a m\u00e3 th\u00e0nh m\u00e3 m\u00e1y \u0111\u1ec3 ch\u01b0\u01a1ng tr\u00ecnh Java c\u00f3 th\u1ec3 ch\u1ea1y \u0111\u01b0\u1ee3c.<\/span><\/td><td><span style=\"font-weight: 400;\">Gi\u00fap \u1ee9ng d\u1ee5ng Java ch\u1ea1y tr\u00ean m\u1ecdi n\u1ec1n t\u1ea3ng h\u1ed7 tr\u1ee3 JVM m\u00e0 kh\u00f4ng c\u1ea7n thay \u0111\u1ed5i m\u00e3 ngu\u1ed3n (Write Once, Run Anywhere).<\/span><\/td><\/tr><tr><td><b>JRE (Java Runtime Environment)<\/b><\/td><td><span style=\"font-weight: 400;\">Java Runtime Environment l\u00e0 m\u00f4i tr\u01b0\u1eddng \u0111\u1ec3 ch\u1ea1y c\u00e1c \u1ee9ng d\u1ee5ng Java. N\u00f3 bao g\u1ed3m JVM v\u00e0 c\u00e1c th\u01b0 vi\u1ec7n l\u1edbp Java c\u1ea7n thi\u1ebft.<\/span><\/td><td><span style=\"font-weight: 400;\">Cho ph\u00e9p ng\u01b0\u1eddi d\u00f9ng ch\u1ea1y c\u00e1c \u1ee9ng d\u1ee5ng Java \u0111\u00e3 \u0111\u01b0\u1ee3c bi\u00ean d\u1ecbch m\u00e0 kh\u00f4ng c\u1ea7n JDK.<\/span><\/td><\/tr><tr><td><b>JDK (Java Development Kit)<\/b><\/td><td><span style=\"font-weight: 400;\">Java Development Kit l\u00e0 b\u1ed9 c\u00f4ng c\u1ee5 ph\u00e1t tri\u1ec3n ph\u1ea7n m\u1ec1m Java. N\u00f3 bao g\u1ed3m JRE, JVM, c\u00f9ng c\u00e1c c\u00f4ng c\u1ee5 ph\u00e1t tri\u1ec3n nh\u01b0 tr\u00ecnh bi\u00ean d\u1ecbch <\/span><span style=\"font-weight: 400;\">javac<\/span><span style=\"font-weight: 400;\">, tr\u00ecnh g\u1ee1 l\u1ed7i.<\/span><\/td><td><span style=\"font-weight: 400;\">Cung c\u1ea5p m\u00f4i tr\u01b0\u1eddng \u0111\u1ec3 l\u1eadp tr\u00ecnh vi\u00ean ph\u00e1t tri\u1ec3n, bi\u00ean d\u1ecbch v\u00e0 ki\u1ec3m th\u1eed c\u00e1c \u1ee9ng d\u1ee5ng Java.<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">JDK l\u00e0 c\u1ea7n thi\u1ebft cho vi\u1ec7c ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng, trong khi JRE v\u00e0 JVM ch\u1ee7 y\u1ebfu \u0111\u1ec3 ch\u1ea1y \u1ee9ng d\u1ee5ng \u0111\u00e3 bi\u00ean d\u1ecbch.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-cac-lo\u1ea1i-bi\u1ebfn-trong-java-la-gi\"><strong>C\u00e1c lo\u1ea1i bi\u1ebfn trong Java l\u00e0 g\u00ec?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong Java, c\u00f3 b\u1ed1n lo\u1ea1i bi\u1ebfn ch\u00ednh:<\/span><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><b>Bi\u1ebfn c\u1ee5c b\u1ed9 (Local Variables):<\/b><span style=\"font-weight: 400;\"> Khai b\u00e1o trong m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c, constructor, ho\u1eb7c kh\u1ed1i m\u00e3, v\u00e0 ch\u1ec9 c\u00f3 th\u1ec3 truy c\u1eadp trong ph\u1ea1m vi c\u1ee7a n\u00f3. Bi\u1ebfn c\u1ee5c b\u1ed9 kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh v\u00e0 ph\u1ea3i \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o tr\u01b0\u1edbc khi s\u1eed d\u1ee5ng.<\/span><\/li>\n\n\n\n<li><b>Bi\u1ebfn th\u00e0nh vi\u00ean (Instance Variables):<\/b><span style=\"font-weight: 400;\"> Khai b\u00e1o trong m\u1ed9t l\u1edbp nh\u01b0ng ngo\u00e0i b\u1ea5t k\u1ef3 ph\u01b0\u01a1ng th\u1ee9c n\u00e0o. M\u1ed7i \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp s\u1ebd c\u00f3 b\u1ea3n sao ri\u00eang c\u1ee7a bi\u1ebfn n\u00e0y.<\/span><\/li>\n\n\n\n<li><b>Bi\u1ebfn t\u0129nh (Static Variables):<\/b><span style=\"font-weight: 400;\"> Khai b\u00e1o v\u1edbi t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">static<\/span><span style=\"font-weight: 400;\"> trong m\u1ed9t l\u1edbp. \u0110\u00e2y l\u00e0 bi\u1ebfn d\u00f9ng chung cho t\u1ea5t c\u1ea3 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp v\u00e0 \u0111\u01b0\u1ee3c chia s\u1ebb tr\u00ean l\u1edbp thay v\u00ec \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/li>\n\n\n\n<li><b>Bi\u1ebfn h\u1eb1ng (Constants):<\/b><span style=\"font-weight: 400;\"> S\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 khai b\u00e1o, l\u00e0m cho gi\u00e1 tr\u1ecb c\u1ee7a bi\u1ebfn kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i sau khi kh\u1edfi t\u1ea1o.<\/span><\/li>\n<\/ol>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong Java, c\u00f3 m\u1ed9t s\u1ed1 t\u1eeb kh\u00f3a quan tr\u1ecdng th\u01b0\u1eddng g\u1eb7p:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>final<\/b><b>:<\/b><span style=\"font-weight: 400;\"> D\u00f9ng \u0111\u1ec3 khai b\u00e1o h\u1eb1ng s\u1ed1 ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c kh\u00f4ng th\u1ec3 ghi \u0111\u00e8, v\u00e0 l\u1edbp kh\u00f4ng th\u1ec3 k\u1ebf th\u1eeba.<\/span><\/li>\n\n\n\n<li><b>static<\/b><b>:<\/b><span style=\"font-weight: 400;\"> Bi\u1ebfn ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c \u0111\u01b0\u1ee3c khai b\u00e1o l\u00e0 <\/span><span style=\"font-weight: 400;\">static<\/span><span style=\"font-weight: 400;\"> s\u1ebd t\u1ed3n t\u1ea1i \u1edf c\u1ea5p l\u1edbp, ngh\u0129a l\u00e0 ch\u00fang kh\u00f4ng ph\u1ee5 thu\u1ed9c v\u00e0o \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp tr\u1ef1c ti\u1ebfp t\u1eeb l\u1edbp.<\/span><\/li>\n\n\n\n<li><b>volatile<\/b><b>:<\/b><span style=\"font-weight: 400;\"> \u0110\u1ea3m b\u1ea3o r\u1eb1ng gi\u00e1 tr\u1ecb c\u1ee7a bi\u1ebfn s\u1ebd \u0111\u01b0\u1ee3c \u0111\u1ecdc t\u1eeb b\u1ed9 nh\u1edb ch\u00ednh m\u1ed7i khi c\u00f3 truy c\u1eadp, ph\u00f9 h\u1ee3p cho c\u00e1c bi\u1ebfn \u0111\u01b0\u1ee3c truy c\u1eadp b\u1edfi nhi\u1ec1u lu\u1ed3ng trong l\u1eadp tr\u00ecnh \u0111a lu\u1ed3ng.<\/span><\/li>\n\n\n\n<li><b>synchronized<\/b><b>:<\/b><span style=\"font-weight: 400;\"> \u0110\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 \u0111\u1ed3ng b\u1ed9 h\u00f3a c\u00e1c ph\u01b0\u01a1ng th\u1ee9c ho\u1eb7c kh\u1ed1i m\u00e3, gi\u00fap ng\u0103n ch\u1eb7n vi\u1ec7c truy c\u1eadp c\u00f9ng l\u00fac v\u00e0o t\u00e0i nguy\u00ean d\u00f9ng chung t\u1eeb nhi\u1ec1u lu\u1ed3ng.<\/span><\/li>\n\n\n\n<li><b>abstract<\/b><b>:<\/b><span style=\"font-weight: 400;\"> \u0110\u01b0\u1ee3c d\u00f9ng cho l\u1edbp ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c tr\u1eebu t\u01b0\u1ee3ng. M\u1ed9t l\u1edbp tr\u1eebu t\u01b0\u1ee3ng kh\u00f4ng th\u1ec3 \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o v\u00e0 th\u01b0\u1eddng \u0111\u01b0\u1ee3c k\u1ebf th\u1eeba.<\/span><\/li>\n\n\n\n<li><b>transient<\/b><b>:<\/b><span style=\"font-weight: 400;\"> D\u00f9ng \u0111\u1ec3 b\u1ecf qua kh\u00f4ng tu\u1ea7n t\u1ef1 h\u00f3a m\u1ed9t bi\u1ebfn khi l\u1edbp \u0111\u01b0\u1ee3c tu\u1ea7n t\u1ef1 h\u00f3a.<\/span><\/li>\n\n\n\n<li><b>this<\/b><b>:<\/b><span style=\"font-weight: 400;\"> \u0110\u1ea1i di\u1ec7n cho \u0111\u1ed1i t\u01b0\u1ee3ng hi\u1ec7n t\u1ea1i trong ph\u01b0\u01a1ng th\u1ee9c ho\u1eb7c constructor.<\/span><\/li>\n\n\n\n<li><b>super<\/b><b>:<\/b><span style=\"font-weight: 400;\"> D\u00f9ng \u0111\u1ec3 tham chi\u1ebfu \u0111\u1ebfn l\u1edbp cha ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c c\u1ee7a l\u1edbp cha trong l\u1edbp con.<\/span><\/li>\n\n\n\n<li><b>enum<\/b><b>:<\/b><span style=\"font-weight: 400;\"> \u0110\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 \u0111\u1ecbnh ngh\u0129a c\u00e1c h\u1eb1ng s\u1ed1 d\u1ea1ng li\u1ec7t k\u00ea.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-phan-bi\u1ec7t-gi\u1eefa-ki\u1ec3u-d\u1eef-li\u1ec7u-nguyen-th\u1ee7y-primitive-va-ki\u1ec3u-d\u1ed1i-t\u01b0\u1ee3ng-object-reference-trong-java\"><strong>Ph\u00e2n bi\u1ec7t gi\u1eefa ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y (primitive) v\u00e0 ki\u1ec3u \u0111\u1ed1i t\u01b0\u1ee3ng (object\/reference) trong Java?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Lo\u1ea1i&nbsp;<\/b><\/td><td><b>Ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean thu\u1ef7<\/b><\/td><td><b>Ki\u1ec3u \u0111\u1ed1i t\u01b0\u1ee3ng<\/b><\/td><\/tr><tr><td><b>\u0110\u1ecbnh ngh\u0129a&nbsp;<\/b><\/td><td><span style=\"font-weight: 400;\">C\u00e1c ki\u1ec3u d\u1eef li\u1ec7u \u0111\u01a1n gi\u1ea3n c\u00f3 s\u1eb5n trong Java (<\/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;\">, <\/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;\">, <\/span><span style=\"font-weight: 400;\">boolean<\/span><span style=\"font-weight: 400;\">, v.v.)<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00e1c ki\u1ec3u d\u1eef li\u1ec7u do ng\u01b0\u1eddi d\u00f9ng \u0111\u1ecbnh ngh\u0129a (<\/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;\">, c\u00e1c class t\u00f9y ch\u1ec9nh)<\/span><\/td><\/tr><tr><td><b>B\u1ed9 nh\u1edb l\u01b0u tr\u1eef<\/b><\/td><td><span style=\"font-weight: 400;\">L\u01b0u tr\u1eef tr\u1ef1c ti\u1ebfp trong b\u1ed9 nh\u1edb <\/span><b>stack<\/b><\/td><td><span style=\"font-weight: 400;\">L\u01b0u tr\u1eef trong b\u1ed9 nh\u1edb <\/span><b>heap<\/b><span style=\"font-weight: 400;\">, bi\u1ebfn tham chi\u1ebfu ch\u1ec9 l\u01b0u \u0111\u1ecba ch\u1ec9 \u0111\u1ed1i t\u01b0\u1ee3ng<\/span><\/td><\/tr><tr><td><b>T\u00ednh b\u1ea5t bi\u1ebfn<\/b><\/td><td><span style=\"font-weight: 400;\">C\u00e1c gi\u00e1 tr\u1ecb l\u00e0 <\/span><b>b\u1ea5t bi\u1ebfn (immutable)<\/b><span style=\"font-weight: 400;\">, kh\u00f4ng th\u1ec3 ch\u1ee9a ph\u01b0\u01a1ng th\u1ee9c<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u00f3 th\u1ec3 <\/span><b>thay \u0111\u1ed5i (mutable)<\/b><span style=\"font-weight: 400;\"> v\u00e0 ch\u1ee9a c\u00e1c ph\u01b0\u01a1ng th\u1ee9c, bi\u1ebfn<\/span><\/td><\/tr><tr><td><b>Truy c\u1eadp v\u00e0 hi\u1ec7u su\u1ea5t&nbsp;<\/b><\/td><td><span style=\"font-weight: 400;\">Truy c\u1eadp nhanh h\u01a1n, kh\u00f4ng c\u1ea7n c\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb \u0111\u1eb7c bi\u1ec7t<\/span><\/td><td><span style=\"font-weight: 400;\">Truy c\u1eadp ch\u1eadm h\u01a1n v\u00ec tham chi\u1ebfu \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 c\u1ea7n c\u1ea5p ph\u00e1t b\u1ed9 nh\u1edb tr\u00ean heap<\/span><\/td><\/tr><tr><td><b>Ch\u1ee9a ph\u01b0\u01a1ng th\u1ee9c<\/b><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng th\u1ec3 ch\u1ee9a ph\u01b0\u01a1ng th\u1ee9c<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 ch\u1ee9a c\u00e1c ph\u01b0\u01a1ng th\u1ee9c v\u00e0 bi\u1ebfn kh\u00e1c nhau<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-khai-ni\u1ec7m-overloading-va-overriding-la-gi-neu-vi-d\u1ee5-minh-h\u1ecda\"><strong>Kh\u00e1i ni\u1ec7m Overloading v\u00e0 Overriding l\u00e0 g\u00ec? N\u00eau v\u00ed d\u1ee5 minh h\u1ecda.<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong Java, <\/span><b>Overloading<\/b><span style=\"font-weight: 400;\"> v\u00e0 <\/span><b>Overriding<\/b><span style=\"font-weight: 400;\"> l\u00e0 hai c\u01a1 ch\u1ebf quan tr\u1ecdng c\u1ee7a t\u00ednh \u0111a h\u00ecnh (polymorphism), nh\u01b0ng ch\u00fang c\u00f3 nh\u1eefng \u0111i\u1ec3m kh\u00e1c nhau r\u00f5 r\u1ec7t v\u1ec1 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng v\u00e0 m\u1ee5c \u0111\u00edch s\u1eed d\u1ee5ng.<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Overloading (N\u1ea1p ch\u1ed3ng ph\u01b0\u01a1ng th\u1ee9c)<\/b><span style=\"font-weight: 400;\">: \u0110\u00e2y l\u00e0 khi ch\u00fang ta \u0111\u1ecbnh ngh\u0129a nhi\u1ec1u ph\u01b0\u01a1ng th\u1ee9c c\u00f9ng t\u00ean trong c\u00f9ng m\u1ed9t l\u1edbp nh\u01b0ng c\u00f3 tham s\u1ed1 kh\u00e1c nhau (v\u1ec1 s\u1ed1 l\u01b0\u1ee3ng ho\u1eb7c ki\u1ec3u d\u1eef li\u1ec7u tham s\u1ed1). M\u1ee5c ti\u00eau c\u1ee7a overloading l\u00e0 cung c\u1ea5p c\u00e1c phi\u00ean b\u1ea3n kh\u00e1c nhau c\u1ee7a c\u00f9ng m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c \u0111\u1ec3 ph\u00f9 h\u1ee3p v\u1edbi c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u ho\u1eb7c s\u1ed1 l\u01b0\u1ee3ng tham s\u1ed1 kh\u00e1c nhau. \u0110i\u1ec1u n\u00e0y x\u1ea3y ra \u1edf <\/span><b>compile-time<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MathOperation {\n  public int add(int a, int b) {\n    return a + b;\n  }\n\n  public double add(double a, double b) {\n    return a + b;\n  }\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong v\u00ed d\u1ee5 n\u00e0y, ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">add<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c n\u1ea1p ch\u1ed3ng (overloaded) \u0111\u1ec3 x\u1eed l\u00fd c\u1ea3 ki\u1ec3u <\/span><span style=\"font-weight: 400;\">int<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">double<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Overriding (Ghi \u0111\u00e8 ph\u01b0\u01a1ng th\u1ee9c)<\/b><span style=\"font-weight: 400;\">: Overriding x\u1ea3y ra khi m\u1ed9t l\u1edbp con \u0111\u1ecbnh ngh\u0129a l\u1ea1i ph\u01b0\u01a1ng th\u1ee9c \u0111\u00e3 \u0111\u01b0\u1ee3c khai b\u00e1o trong l\u1edbp cha. Ph\u01b0\u01a1ng th\u1ee9c \u0111\u01b0\u1ee3c ghi \u0111\u00e8 ph\u1ea3i c\u00f3 c\u00f9ng t\u00ean, ki\u1ec3u tr\u1ea3 v\u1ec1 v\u00e0 danh s\u00e1ch tham s\u1ed1 nh\u01b0 ph\u01b0\u01a1ng th\u1ee9c trong l\u1edbp cha. M\u1ee5c ti\u00eau c\u1ee7a overriding l\u00e0 \u0111\u1ec3 l\u1edbp con c\u00f3 th\u1ec3 cung c\u1ea5p phi\u00ean b\u1ea3n ph\u01b0\u01a1ng th\u1ee9c ph\u00f9 h\u1ee3p v\u1edbi \u0111\u1eb7c \u0111i\u1ec3m c\u1ee7a l\u1edbp \u0111\u00f3. \u0110i\u1ec1u n\u00e0y x\u1ea3y ra \u1edf <\/span><b>runtime<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n  public void sound() {\n    System.out.println(\"Some sound\");\n  }\n}\n\nclass Dog extends Animal {\n  @Override\n  public void sound() {\n    System.out.println(\"Bark\");\n  }\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong v\u00ed d\u1ee5 n\u00e0y, l\u1edbp <\/span><span style=\"font-weight: 400;\">Dog<\/span><span style=\"font-weight: 400;\"> ghi \u0111\u00e8 (override) ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">sound<\/span><span style=\"font-weight: 400;\"> t\u1eeb l\u1edbp <\/span><span style=\"font-weight: 400;\">Animal<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 cung c\u1ea5p h\u00e0nh vi ri\u00eang (ti\u1ebfng s\u1ee7a) c\u1ee7a n\u00f3.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-constructor-la-gi-khac-gi-v\u1edbi-ph\u01b0\u01a1ng-th\u1ee9c-method-thong-th\u01b0\u1eddng\"><strong>Constructor l\u00e0 g\u00ec? Kh\u00e1c g\u00ec v\u1edbi ph\u01b0\u01a1ng th\u1ee9c (method) th\u00f4ng th\u01b0\u1eddng?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Constructor<\/span><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c \u0111\u1eb7c bi\u1ec7t trong Java \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 kh\u1edfi t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp. \u0110i\u1ec3m quan tr\u1ecdng l\u00e0 constructor c\u00f3 c\u00f9ng t\u00ean v\u1edbi l\u1edbp v\u00e0 kh\u00f4ng c\u00f3 ki\u1ec3u tr\u1ea3 v\u1ec1 (kh\u00f4ng c\u00f3 <\/span><span style=\"font-weight: 400;\">void<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c ki\u1ec3u d\u1eef li\u1ec7u). N\u00f3 \u0111\u01b0\u1ee3c g\u1ecdi t\u1ef1 \u0111\u1ed9ng khi m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng \u0111\u01b0\u1ee3c t\u1ea1o ra t\u1eeb l\u1edbp.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Kh\u00e1c v\u1edbi constructor, <\/span><span style=\"font-weight: 400;\">ph\u01b0\u01a1ng th\u1ee9c (method) trong Java <\/span><span style=\"font-weight: 400;\">l\u00e0 c\u00e1c h\u00e0m c\u00f3 th\u1ec3 tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb (ho\u1eb7c kh\u00f4ng) v\u00e0 c\u00f3 th\u1ec3 c\u00f3 t\u00ean kh\u00e1c v\u1edbi t\u00ean c\u1ee7a l\u1edbp. M\u1ed9t ph\u01b0\u01a1ng th\u1ee9c c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c g\u1ecdi b\u1ea5t k\u1ef3 l\u00fac n\u00e0o khi \u0111\u1ed1i t\u01b0\u1ee3ng \u0111\u00e3 \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o.<\/span><\/p>\n\n\n\n<p>V\u00ed d\u1ee5 v\u1ec1 constructor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car {\n  String model;\n  int year;\n\n  \/\/ Constructor\n  public Car(String model, int year) {\n    this.model = model;\n    this. year = year;\n  }\n}<\/code><\/pre>\n\n\n\n<p>V\u00ed d\u1ee5 v\u1ec1 method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Car {\n  String model;\n  int year;\n\n  \/\/ Method\n  public void startEngine() {\n    System.out.println(\"Engine started\");\n  }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-static-keyword-co-vai-tro-gi-static-block-va-static-method-khac-nhau-nh\u01b0-th\u1ebf-nao\"><strong>Static keyword c\u00f3 vai tr\u00f2 g\u00ec? Static block v\u00e0 static method kh\u00e1c nhau nh\u01b0 th\u1ebf n\u00e0o?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong Java, 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 c\u00e1c th\u00e0nh ph\u1ea7n c\u1ee7a l\u1edbp m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp \u0111\u00f3. N\u00f3 c\u00f3 th\u1ec3 \u00e1p d\u1ee5ng cho c\u00e1c bi\u1ebfn, ph\u01b0\u01a1ng th\u1ee9c, kh\u1ed1i m\u00e3 (block) v\u00e0 l\u1edbp n\u1ed9i b\u1ed9 (nested class).<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Static variable<\/b><span style=\"font-weight: 400;\"> (bi\u1ebfn static): L\u00e0 bi\u1ebfn \u0111\u01b0\u1ee3c chia s\u1ebb gi\u1eefa t\u1ea5t c\u1ea3 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp. N\u00f3 kh\u00f4ng b\u1ecb thay \u0111\u1ed5i khi m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng m\u1edbi \u0111\u01b0\u1ee3c t\u1ea1o, m\u00e0 thay v\u00e0o \u0111\u00f3, t\u1ea5t c\u1ea3 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng chia s\u1ebb gi\u00e1 tr\u1ecb c\u1ee7a n\u00f3.<\/span><\/li>\n\n\n\n<li><b>Static method<\/b><span style=\"font-weight: 400;\"> (ph\u01b0\u01a1ng th\u1ee9c static): L\u00e0 ph\u01b0\u01a1ng th\u1ee9c c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c g\u1ecdi m\u00e0 kh\u00f4ng c\u1ea7n t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp. N\u00f3 c\u00f3 th\u1ec3 truy c\u1eadp v\u00e0 thay \u0111\u1ed5i c\u00e1c bi\u1ebfn static, nh\u01b0ng kh\u00f4ng th\u1ec3 truy c\u1eadp c\u00e1c bi\u1ebfn ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c non-static c\u1ee7a l\u1edbp.<\/span><\/li>\n\n\n\n<li><b>Static block<\/b><span style=\"font-weight: 400;\"> (kh\u1ed1i m\u00e3 static): L\u00e0 m\u1ed9t kh\u1ed1i m\u00e3 \u0111\u1eb7c bi\u1ec7t \u0111\u01b0\u1ee3c th\u1ef1c thi m\u1ed9t l\u1ea7n duy nh\u1ea5t khi l\u1edbp \u0111\u01b0\u1ee3c n\u1ea1p v\u00e0o b\u1ed9 nh\u1edb, tr\u01b0\u1edbc khi b\u1ea5t k\u1ef3 ph\u01b0\u01a1ng th\u1ee9c ho\u1eb7c \u0111\u1ed1i t\u01b0\u1ee3ng n\u00e0o c\u1ee7a l\u1edbp \u0111\u00f3 \u0111\u01b0\u1ee3c t\u1ea1o ra. N\u00f3 th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 kh\u1edfi t\u1ea1o c\u00e1c bi\u1ebfn static ho\u1eb7c th\u1ef1c hi\u1ec7n m\u1ed9t s\u1ed1 c\u00f4ng vi\u1ec7c kh\u1edfi t\u1ea1o m\u00e0 ch\u1ec9 c\u1ea7n l\u00e0m m\u1ed9t l\u1ea7n.<\/span><\/li>\n<\/ul>\n\n\n\n<p><b>S\u1ef1 kh\u00e1c nhau gi\u1eefa Static block v\u00e0 Static method:<\/b><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Static block<\/b><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 th\u1ef1c hi\u1ec7n kh\u1edfi t\u1ea1o ho\u1eb7c c\u00e1c thao t\u00e1c c\u1ea5u h\u00ecnh t\u0129nh khi l\u1edbp \u0111\u01b0\u1ee3c n\u1ea1p v\u00e0o b\u1ed9 nh\u1edb. Kh\u1ed1i m\u00e3 n\u00e0y ch\u1ec9 ch\u1ea1y m\u1ed9t l\u1ea7n duy nh\u1ea5t, ngay khi l\u1edbp \u0111\u01b0\u1ee3c n\u1ea1p.<\/span><\/li>\n\n\n\n<li><b>Static method<\/b><span style=\"font-weight: 400;\"> l\u00e0 ph\u01b0\u01a1ng th\u1ee9c c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c g\u1ecdi m\u00e0 kh\u00f4ng c\u1ea7n t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp v\u00e0 th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 chung kh\u00f4ng ph\u1ee5 thu\u1ed9c v\u00e0o tr\u1ea1ng th\u00e1i c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng. Ph\u01b0\u01a1ng th\u1ee9c n\u00e0y c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c g\u1ecdi t\u1eeb b\u1ea5t k\u1ef3 \u0111\u00e2u trong ch\u01b0\u01a1ng tr\u00ecnh m\u00e0 kh\u00f4ng c\u1ea7n s\u1ef1 hi\u1ec7n di\u1ec7n c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Ti\u00eau ch\u00ed&nbsp;<\/b><\/td><td><b>Static Block&nbsp;<\/b><\/td><td><b>Static Method<\/b><\/td><\/tr><tr><td><b>Khi n\u00e0o \u0111\u01b0\u1ee3c th\u1ef1c thi<\/b><\/td><td><span style=\"font-weight: 400;\">\u0110\u01b0\u1ee3c th\u1ef1c thi ngay khi l\u1edbp \u0111\u01b0\u1ee3c n\u1ea1p v\u00e0o b\u1ed9 nh\u1edb<\/span><\/td><td><span style=\"font-weight: 400;\">Ch\u1ec9 \u0111\u01b0\u1ee3c th\u1ef1c thi khi \u0111\u01b0\u1ee3c g\u1ecdi tr\u1ef1c <\/span><span style=\"font-weight: 400;\">ti\u1ebfp<\/span><\/td><\/tr><tr><td><b>M\u1ee5c \u0111\u00edch ch\u00ednh<\/b><\/td><td><span style=\"font-weight: 400;\">Kh\u1edfi t\u1ea1o c\u00e1c bi\u1ebfn t\u0129nh ho\u1eb7c th\u1ef1c hi\u1ec7n c\u00e1c thi\u1ebft l\u1eadp tr\u01b0\u1edbc khi ch\u01b0\u01a1ng tr\u00ecnh b\u1eaft \u0111\u1ea7u<\/span><\/td><td><span style=\"font-weight: 400;\">Th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c g\u1ecdi l\u1ea1i nhi\u1ec1u l\u1ea7n trong su\u1ed1t ch\u01b0\u01a1ng tr\u00ecnh<\/span><\/td><\/tr><tr><td><b>C\u00e1ch khai b\u00e1o<\/b><\/td><td><span style=\"font-weight: 400;\">D\u00f9ng kh\u1ed1i static { &#8230; }<\/span><\/td><td><span style=\"font-weight: 400;\">\u0110\u1ecbnh ngh\u0129a b\u1eb1ng t\u1eeb kh\u00f3a static trong khai b\u00e1o h\u00e0m<\/span><\/td><\/tr><tr><td><b>Tham s\u1ed1&nbsp;<\/b><\/td><td><span style=\"font-weight: 400;\">kh\u00f4ng<\/span><span style=\"font-weight: 400;\"> c\u00f3 tham s\u1ed1<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 c\u00f3 tham s\u1ed1 \u0111\u1ea7u v\u00e0o<\/span><\/td><\/tr><tr><td><b>Kh\u1ea3 n\u0103ng s\u1eed d\u1ee5ng l\u1ea1i<\/b><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng th\u1ec3 s\u1eed d\u1ee5ng l\u1ea1i (v\u00ec ch\u1ec9 ch\u1ea1y m\u1ed9t l\u1ea7n)<\/span><\/td><td><span style=\"font-weight: 400;\">Co th\u1ec3 t\u00e1i d\u1ee5ng v\u00e0 g\u1ecdi h\u00e0m l\u1ea1i b\u1ea5t k\u1ef3 l\u00fac n\u00e0o<\/span><\/td><\/tr><tr><td><b>Truy c\u1eadp t\u1edbi th\u00e0nh ph\u1ea7n<\/b><\/td><td><span style=\"font-weight: 400;\">Ch\u1ec9 c\u00f3 th\u1ec3 truy c\u1eadp \u0111\u1ebfn c\u00e1c <\/span><b>th\u00e0nh ph\u1ea7n t\u0129nh<\/b><span style=\"font-weight: 400;\"> c\u1ee7a l\u1edbp<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 truy c\u1eadp \u0111\u1ebfn <\/span><b>c\u00e1c th\u00e0nh ph\u1ea7n t\u0129nh<\/b><span style=\"font-weight: 400;\"> c\u1ee7a l\u1edbp<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><b>V\u00ed d\u1ee5 minh h\u1ecda:<\/b><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyClass {\n  static int count = 0; \/\/ Static variable\n\n  static {\n    \/\/ Static block: ch\u1ea1y khi l\u1edbp \u0111\u01b0\u1ee3c n\u1ea1p v\u00e0o b\u1ed9 nh\u1edb\n    count = 10;\n    System.out.println(\"Static block executed!\");\n  }\n\n  static void displayCount() { \/\/ Static method\n    System.out.println(\"Count: \" + count);\n  }\n\n  public static void main(String&#91;] args) {\n    MyClass.displayCount(); \/\/ G\u1ecdi ph\u01b0\u01a1ng th\u1ee9c static\n  }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cac-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-java-lien-quan-d\u1ebfn-oop\"><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Java_lien_quan_den_OOP\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java li\u00ean quan \u0111\u1ebfn OOP<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u0110\u1ecdc th\u00eam:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-oop-phan-1\/\" target=\"_blank\" rel=\"noopener\">Top 50+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n OOP v\u00e0 tr\u1ea3 l\u1eddi m\u1edbi nh\u1ea5t (Ph\u1ea7n 1)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-oop-phan-2\/\" target=\"_blank\" rel=\"noopener\">Top 50+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n OOP v\u00e0 tr\u1ea3 l\u1eddi m\u1edbi nh\u1ea5t (Ph\u1ea7n 2)<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-gi\u1ea3i-thich-4-nguyen-ly-chinh-c\u1ee7a-l\u1eadp-trinh-h\u01b0\u1edbng-d\u1ed1i-t\u01b0\u1ee3ng-encapsulation-inheritance-polymorphism-abstraction-trong-java\"><strong>Gi\u1ea3i th\u00edch 4 nguy\u00ean l\u00fd ch\u00ednh c\u1ee7a l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng (Encapsulation, Inheritance, Polymorphism, Abstraction) trong Java<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">L\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng (OOP) l\u00e0 m\u1ed9t m\u00f4 h\u00ecnh l\u1eadp tr\u00ecnh d\u1ef1a tr\u00ean vi\u1ec7c t\u1ed5 ch\u1ee9c m\u00e3 ngu\u1ed3n th\u00e0nh c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 l\u1edbp. B\u1ed1n nguy\u00ean l\u00fd ch\u00ednh c\u1ee7a OOP trong Java l\u00e0 <\/span><b>Encapsulation (\u0110\u00f3ng g\u00f3i)<\/b><span style=\"font-weight: 400;\">, <\/span><b>Inheritance (K\u1ebf th\u1eeba)<\/b><span style=\"font-weight: 400;\">, <\/span><b>Polymorphism (\u0110a h\u00ecnh)<\/b><span style=\"font-weight: 400;\"> v\u00e0 <\/span><b>Abstraction (Tr\u1eebu t\u01b0\u1ee3ng)<\/b><span style=\"font-weight: 400;\">. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 gi\u1ea3i th\u00edch chi ti\u1ebft v\u1ec1 t\u1eebng nguy\u00ean l\u00fd:<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-encapsulation-dong-goi\"><b>Encapsulation (\u0110\u00f3ng g\u00f3i)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Encapsulation l\u00e0 qu\u00e1 tr\u00ecnh nh\u00f3m c\u00e1c thu\u1ed9c t\u00ednh (bi\u1ebfn) v\u00e0 ph\u01b0\u01a1ng th\u1ee9c (h\u00e0m) l\u1ea1i th\u00e0nh m\u1ed9t \u0111\u01a1n v\u1ecb duy nh\u1ea5t, g\u1ecdi l\u00e0 l\u1edbp. \u0110i\u1ec1u n\u00e0y gi\u00fap b\u1ea3o v\u1ec7 d\u1eef li\u1ec7u b\u00ean trong l\u1edbp kh\u1ecfi b\u1ecb thay \u0111\u1ed5i tr\u1ef1c ti\u1ebfp t\u1eeb b\u00ean ngo\u00e0i. C\u00e1c thu\u1ed9c t\u00ednh th\u01b0\u1eddng \u0111\u01b0\u1ee3c khai b\u00e1o l\u00e0 private v\u00e0 ch\u1ec9 c\u00f3 th\u1ec3 truy c\u1eadp th\u00f4ng qua c\u00e1c ph\u01b0\u01a1ng th\u1ee9c getter v\u00e0 setter c\u00f4ng khai.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Person {\n  private String name; \/\/ private thu\u1ed9c t\u00ednh\n\n  public String getName() { \/\/ ph\u01b0\u01a1ng th\u1ee9c getter\n    return name;\n  }\n\n  public void setName(String name) { \/\/ ph\u01b0\u01a1ng th\u1ee9c setter\n    this.name = name;\n  }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-inheritance-k\u1ebf-th\u1eeba\"><b>Inheritance (K\u1ebf th\u1eeba)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Inheritance cho ph\u00e9p m\u1ed9t l\u1edbp (subclass) k\u1ebf th\u1eeba c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c c\u1ee7a m\u1ed9t l\u1edbp kh\u00e1c (superclass). \u0110i\u1ec1u n\u00e0y gi\u00fap t\u00e1i s\u1eed d\u1ee5ng m\u00e3 ngu\u1ed3n v\u00e0 t\u1ea1o ra c\u00e1c l\u1edbp con c\u00f3 th\u1ec3 m\u1edf r\u1ed9ng c\u00e1c t\u00ednh n\u0103ng c\u1ee7a l\u1edbp cha m\u00e0 kh\u00f4ng c\u1ea7n vi\u1ebft l\u1ea1i m\u00e3.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n  public void makeSound() {\n    System.out.println(\"Animal makes a sound\");\n  }\n}\n\nclass Dog extends Animal {\n  @Override\n  public void makeSound() {\n    System.out.println(\"Dog barks\");\n  }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-polymorphism-da-hinh\"><b>Polymorphism (\u0110a h\u00ecnh)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Polymorphism cho ph\u00e9p c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a c\u00e1c l\u1edbp kh\u00e1c nhau c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00e1c ph\u01b0\u01a1ng th\u1ee9c gi\u1ed1ng nhau, nh\u01b0ng c\u00e1ch th\u1ee9c th\u1ef1c hi\u1ec7n l\u1ea1i kh\u00e1c nhau. \u0110i\u1ec1u n\u00e0y gi\u00fap code linh ho\u1ea1t v\u00e0 d\u1ec5 b\u1ea3o tr\u00ec h\u01a1n. C\u00f3 hai lo\u1ea1i polymorphism trong Java: <\/span><b>Method Overloading<\/b><span style=\"font-weight: 400;\"> (n\u1ea1p ch\u1ed3ng ph\u01b0\u01a1ng th\u1ee9c) v\u00e0 <\/span><b>Method Overriding<\/b><span style=\"font-weight: 400;\"> (ghi \u0111\u00e8 ph\u01b0\u01a1ng th\u1ee9c).<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Method Overloading:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Calculator {\n  public int add(int a, int b) {\n    return a + b;\n  }\n  public double add(double a, double b) {\n    return a + b;\n  }\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Method Overriding:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n  public void makeSound() {\n    System.out.println(\"Animal makes a sound\");\n  }\n}\n\nclass Dog extends Animal {\n  @Override\n  public void makeSound() {\n    System.out.println(\"Dog barks\");\n  }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-abstraction-tr\u1eebu-t\u01b0\u1ee3ng\"><b>Abstraction (Tr\u1eebu t\u01b0\u1ee3ng)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Abstraction l\u00e0 qu\u00e1 tr\u00ecnh \u1ea9n \u0111i c\u00e1c chi ti\u1ebft tri\u1ec3n khai v\u00e0 ch\u1ec9 cung c\u1ea5p nh\u1eefng g\u00ec c\u1ea7n thi\u1ebft cho ng\u01b0\u1eddi s\u1eed d\u1ee5ng. Trong Java, abstraction c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n th\u00f4ng qua <\/span><b>abstract classes<\/b><span style=\"font-weight: 400;\"> v\u00e0 <\/span><b>interfaces<\/b><span style=\"font-weight: 400;\">. L\u1edbp abstract kh\u00f4ng th\u1ec3 kh\u1edfi t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng tr\u1ef1c ti\u1ebfp v\u00e0 th\u01b0\u1eddng ch\u1ee9a c\u00e1c ph\u01b0\u01a1ng th\u1ee9c abstract m\u00e0 c\u00e1c l\u1edbp con ph\u1ea3i c\u00e0i \u0111\u1eb7t.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>abstract class Animal {\n  abstract void makeSound(); \/\/ ph\u01b0\u01a1ng th\u1ee9c abstract\n}\n\nclass Dog extends Animal {\n  @Override\n  void makeSound() {\n    System.out.println(\"Dog barks\");\n  }\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><span style=\"font-weight: 400;\">L\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng \u0111\u01b0\u1ee3c xem l\u00e0 ki\u1ebfn th\u1ee9c c\u1ef1c k\u1ef3 quan tr\u1ecdng cho c\u00e1c l\u1eadp tr\u00ecnh vi\u00ean n\u00f3i chung v\u00e0 l\u1eadp tr\u00ecnh Java n\u00f3i ri\u00eang, b\u1ea1n c\u00f3 t\u00ecm hi\u1ec3u th\u00eam chi ti\u1ebft t\u1ea1i: <a href=\"https:\/\/itviec.com\/blog\/oop-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>OOP l\u00e0 g\u00ec? 4 \u0111\u1eb7c t\u00ednh c\u01a1 b\u1ea3n c\u1ee7a OOP<\/strong><\/a><\/span><\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-interface-la-gi-so-sanh-interface-va-abstract-class\"><span style=\"font-weight: 400;\"><strong>Interface l\u00e0 g\u00ec? So s\u00e1nh Interface v\u00e0 Abstract Class<\/strong><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Interface<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t ki\u1ec3u d\u1eef li\u1ec7u \u0111\u1eb7c bi\u1ec7t trong Java, n\u00f3 \u0111\u1ecbnh ngh\u0129a c\u00e1c ph\u01b0\u01a1ng th\u1ee9c m\u00e0 c\u00e1c l\u1edbp th\u1ef1c thi ph\u1ea3i c\u00e0i \u0111\u1eb7t, nh\u01b0ng kh\u00f4ng ch\u1ee9a b\u1ea5t k\u1ef3 hi\u1ec7n th\u1ef1c n\u00e0o c\u1ee7a ph\u01b0\u01a1ng th\u1ee9c. C\u00e1c ph\u01b0\u01a1ng th\u1ee9c trong interface m\u1eb7c \u0111\u1ecbnh l\u00e0 <\/span><span style=\"font-weight: 400;\">public abstract<\/span><span style=\"font-weight: 400;\">. Interface ch\u1ec9 c\u00f3 th\u1ec3 ch\u1ee9a khai b\u00e1o ph\u01b0\u01a1ng th\u1ee9c v\u00e0 c\u00e1c h\u1eb1ng s\u1ed1 (constants).<\/span><\/li>\n\n\n\n<li><b>Abstract Class<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t l\u1edbp kh\u00f4ng th\u1ec3 \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o, nh\u01b0ng c\u00f3 th\u1ec3 ch\u1ee9a c\u1ea3 ph\u01b0\u01a1ng th\u1ee9c \u0111\u00e3 tri\u1ec3n khai (concrete methods) v\u00e0 ph\u01b0\u01a1ng th\u1ee9c ch\u01b0a tri\u1ec3n khai (abstract methods). Abstract class d\u00f9ng \u0111\u1ec3 \u0111\u1ecbnh ngh\u0129a m\u1ed9t m\u1eabu cho c\u00e1c l\u1edbp con.<\/span><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Ti\u00eau ch\u00ed<\/b><\/td><td><b>Interface<\/b><\/td><td><b>Abstract Class<\/b><\/td><\/tr><tr><td><b>Ph\u01b0\u01a1ng th\u1ee9c<\/b><\/td><td><span style=\"font-weight: 400;\">Ch\u1ec9 ch\u1ee9a ph\u01b0\u01a1ng th\u1ee9c abstract (kh\u00f4ng c\u00f3 ph\u1ea7n th\u00e2n)<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 ch\u1ee9a c\u1ea3 ph\u01b0\u01a1ng th\u1ee9c abstract v\u00e0 ph\u01b0\u01a1ng th\u1ee9c c\u1ee5 th\u1ec3 (concrete methods)<\/span><\/td><\/tr><tr><td><b>K\u1ebf th\u1eeba<\/b><\/td><td><span style=\"font-weight: 400;\">L\u1edbp c\u00f3 th\u1ec3 implement nhi\u1ec1u interface<\/span><\/td><td><span style=\"font-weight: 400;\">L\u1edbp ch\u1ec9 c\u00f3 th\u1ec3 k\u1ebf th\u1eeba 1 abstract class<\/span><\/td><\/tr><tr><td><b>Tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng<\/b><\/td><td><span style=\"font-weight: 400;\">D\u00f9ng khi c\u00e1c l\u1edbp kh\u00f4ng c\u00f3 m\u1ed1i quan h\u1ec7 cha con r\u00f5 r\u00e0ng, nh\u01b0ng c\u1ea7n chia s\u1ebb c\u00e1c ph\u01b0\u01a1ng th\u1ee9c chung<\/span><\/td><td><span style=\"font-weight: 400;\">D\u00f9ng khi c\u00e1c l\u1edbp c\u00f3 m\u1ed1i quan h\u1ec7 cha con v\u00e0 c\u1ea7n k\u1ebf th\u1eeba t\u00ednh n\u0103ng chung<\/span><\/td><\/tr><tr><td><b>Kh\u1edfi t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng<\/b><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng th\u1ec3 kh\u1edfi t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng t\u1eeb interface<\/span><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng th\u1ec3 kh\u1edfi t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng t\u1eeb abstract class, nh\u01b0ng c\u00f3 th\u1ec3 ch\u1ee9a constructor<\/span><\/td><\/tr><tr><td><b>C\u00e1c th\u00e0nh ph\u1ea7n<\/b><\/td><td><span style=\"font-weight: 400;\">Ch\u1ec9 c\u00f3 th\u1ec3 ch\u1ee9a h\u1eb1ng s\u1ed1 (constant), kh\u00f4ng c\u00f3 bi\u1ebfn instance<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 ch\u1ee9a bi\u1ebfn instance v\u00e0 h\u1eb1ng s\u1ed1<\/span><\/td><\/tr><tr><td><b>T\u00ednh k\u1ebf th\u1eeba<\/b><\/td><td><span style=\"font-weight: 400;\">T\u1ea5t c\u1ea3 ph\u01b0\u01a1ng th\u1ee9c m\u1eb7c \u0111\u1ecbnh l\u00e0 <\/span><span style=\"font-weight: 400;\">public<\/span><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 c\u00f3 ph\u01b0\u01a1ng th\u1ee9c v\u1edbi c\u00e1c m\u1ee9c \u0111\u1ed9 truy c\u1eadp kh\u00e1c nhau (public, private, protected)<\/span><\/td><\/tr><tr><td><b>M\u1ee5c \u0111\u00edch<\/b><\/td><td><span style=\"font-weight: 400;\">T\u1ea1o ra c\u00e1c l\u1edbp kh\u00f4ng c\u00f3 quan h\u1ec7 gi\u1eefa ch\u00fang, nh\u01b0ng c\u00f3 chung c\u00e1c ph\u01b0\u01a1ng th\u1ee9c<\/span><\/td><td><span style=\"font-weight: 400;\">\u0110\u1ecbnh ngh\u0129a m\u1ed9t l\u1edbp c\u01a1 s\u1edf chung cho c\u00e1c l\u1edbp con v\u1edbi m\u1ed9t s\u1ed1 ph\u1ea7n tri\u1ec3n khai s\u1eb5n<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Interface\npublic interface Animal {\n  void sound(); \/\/ Ph\u01b0\u01a1ng th\u1ee9c abstract\n}\n\n\/\/ Abstract Class\npublic abstract class Animal {\n  abstract void sound(); \/\/ Ph\u01b0\u01a1ng th\u1ee9c abstract\n\n  void eat() { \/\/ Ph\u01b0\u01a1ng th\u1ee9c c\u1ee5 th\u1ec3\n    System.out.println(\"Eating...\");\n  }\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Nh\u01b0 v\u1eady, <\/span><b>Interface<\/b><span style=\"font-weight: 400;\"> th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng khi c\u1ea7n \u0111\u1ecbnh ngh\u0129a m\u1ed9t nh\u00f3m c\u00e1c ph\u01b0\u01a1ng th\u1ee9c m\u00e0 nhi\u1ec1u l\u1edbp kh\u00f4ng li\u00ean quan c\u00f3 th\u1ec3 th\u1ef1c thi, c\u00f2n <\/span><b>Abstract Class<\/b><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng khi c\u1ea7n k\u1ebf th\u1eeba c\u00e1c t\u00ednh n\u0103ng chung t\u1eeb m\u1ed9t l\u1edbp c\u01a1 s\u1edf trong h\u1ec7 th\u1ed1ng k\u1ebf th\u1eeba.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-tinh-dong-goi-encapsulation-va-vai-tro-c\u1ee7a-t\u1eeb-khoa-private-trong-java\"><strong>T\u00ednh \u0111\u00f3ng g\u00f3i (Encapsulation) v\u00e0 vai tr\u00f2 c\u1ee7a t\u1eeb kh\u00f3a private trong Java?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">T\u00ednh \u0111\u00f3ng g\u00f3i (Encapsulation) l\u00e0 m\u1ed9t trong nh\u1eefng nguy\u00ean t\u1eafc quan tr\u1ecdng c\u1ee7a l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng (OOP), gi\u00fap b\u1ea3o v\u1ec7 d\u1eef li\u1ec7u kh\u1ecfi b\u1ecb thay \u0111\u1ed5i tr\u1ef1c ti\u1ebfp t\u1eeb b\u00ean ngo\u00e0i l\u1edbp. Thay v\u00e0o \u0111\u00f3, c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c \u0111\u01b0\u1ee3c \u1ea9n gi\u1ea5u v\u00e0 ch\u1ec9 c\u00f3 th\u1ec3 truy c\u1eadp th\u00f4ng qua c\u00e1c ph\u01b0\u01a1ng th\u1ee9c c\u00f4ng khai (getter, setter) n\u1ebfu c\u1ea7n thi\u1ebft. \u0110i\u1ec1u n\u00e0y gi\u00fap \u0111\u1ea3m b\u1ea3o t\u00ednh to\u00e0n v\u1eb9n c\u1ee7a d\u1eef li\u1ec7u v\u00e0 cung c\u1ea5p c\u00e1c c\u01a1 ch\u1ebf ki\u1ec3m tra v\u00e0 x\u1eed l\u00fd khi d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c thay \u0111\u1ed5i.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">T\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">private<\/span><span style=\"font-weight: 400;\"> trong Java \u0111\u00f3ng vai tr\u00f2 r\u1ea5t quan tr\u1ecdng trong vi\u1ec7c th\u1ef1c hi\u1ec7n t\u00ednh \u0111\u00f3ng g\u00f3i. Khi khai b\u00e1o m\u1ed9t thu\u1ed9c t\u00ednh ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c l\u00e0 <\/span><span style=\"font-weight: 400;\">private<\/span><span style=\"font-weight: 400;\">, \u0111i\u1ec1u \u0111\u00f3 c\u00f3 ngh\u0129a l\u00e0 ch\u00fang ch\u1ec9 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp v\u00e0 s\u1eed d\u1ee5ng trong ch\u00ednh l\u1edbp \u0111\u00f3, kh\u00f4ng th\u1ec3 truy c\u1eadp tr\u1ef1c ti\u1ebfp t\u1eeb b\u00ean ngo\u00e0i. \u0110i\u1ec1u n\u00e0y gi\u00fap ng\u0103n ch\u1eb7n vi\u1ec7c truy c\u1eadp tr\u00e1i ph\u00e9p v\u00e0 b\u1ea3o v\u1ec7 d\u1eef li\u1ec7u kh\u1ecfi nh\u1eefng thay \u0111\u1ed5i kh\u00f4ng mong mu\u1ed1n.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Person {\n  private String name; \/\/ thu\u1ed9c t\u00ednh private\n  private int age;\n\n  \/\/ Constructor\n  public Person (String name, int age) {\n    this.name = name;\n    this.age = age;\n  }\n\n  \/\/ Getter v\u00e0 Setter \u0111\u1ec3 truy c\u1eadp thu\u1ed9c t\u00ednh\n  public String getName() {\n    return name;\n  }\n\n  public void setName(String name) {\n    this.name = name;\n  }\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong v\u00ed d\u1ee5 tr\u00ean, thu\u1ed9c t\u00ednh <\/span><span style=\"font-weight: 400;\">name<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">age<\/span><span style=\"font-weight: 400;\"> l\u00e0 <\/span><span style=\"font-weight: 400;\">private<\/span><span style=\"font-weight: 400;\">, v\u00ec v\u1eady ch\u00fang kh\u00f4ng th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp tr\u1ef1c ti\u1ebfp t\u1eeb b\u00ean ngo\u00e0i l\u1edbp <\/span><span style=\"font-weight: 400;\">Person<\/span><span style=\"font-weight: 400;\">. Thay v\u00e0o \u0111\u00f3, ch\u00fang ch\u1ec9 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp th\u00f4ng qua c\u00e1c ph\u01b0\u01a1ng th\u1ee9c getter v\u00e0 setter. \u0110i\u1ec1u n\u00e0y gi\u00fap \u0111\u1ea3m b\u1ea3o r\u1eb1ng c\u00e1c gi\u00e1 tr\u1ecb c\u1ee7a <\/span><span style=\"font-weight: 400;\">name<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">age<\/span><span style=\"font-weight: 400;\"> lu\u00f4n h\u1ee3p l\u1ec7 v\u00e0 c\u00f3 th\u1ec3 ki\u1ec3m tra trong qu\u00e1 tr\u00ecnh thay \u0111\u1ed5i.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-th\u1ebf-nao-d\u1ec3-ngan-ch\u1eb7n-vi\u1ec7c-k\u1ebf-th\u1eeba-trong-java-neu-vi-d\u1ee5-s\u1eed-d\u1ee5ng-t\u1eeb-khoa-final\"><strong>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 ng\u0103n ch\u1eb7n vi\u1ec7c k\u1ebf th\u1eeba trong Java? N\u00eau v\u00ed d\u1ee5 s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a final<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 ng\u0103n ch\u1eb7n vi\u1ec7c k\u1ebf th\u1eeba trong Java, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\"> v\u1edbi l\u1edbp ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c. Khi m\u1ed9t l\u1edbp \u0111\u01b0\u1ee3c khai b\u00e1o l\u00e0 <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\">, n\u00f3 kh\u00f4ng th\u1ec3 b\u1ecb k\u1ebf th\u1eeba. T\u01b0\u01a1ng t\u1ef1, khi m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c \u0111\u01b0\u1ee3c khai b\u00e1o l\u00e0 <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\">, ph\u01b0\u01a1ng th\u1ee9c \u0111\u00f3 kh\u00f4ng th\u1ec3 b\u1ecb ghi \u0111\u00e8 (override) trong l\u1edbp con.<\/span><\/p>\n\n\n\n<p>V\u00ed d\u1ee5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">\/\/ L\u1edbp final kh\u00f4ng th\u1ec3 b\u1ecb k\u1ebf th\u1eeba<\/span>\n<span style=\"font-weight: 400;\">public final class MyClass {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;public void display() {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Hello from MyClass\");<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">\/\/ L\u1edbp con kh\u00f4ng th\u1ec3 k\u1ebf th\u1eeba t\u1eeb MyClass v\u00ec MyClass l\u00e0 final<\/span>\n<span style=\"font-weight: 400;\">\/\/ class SubClass extends MyClass { \/\/ L\u1ed7i bi\u00ean d\u1ecbch: Cannot inherit from final 'MyClass' }<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong v\u00ed d\u1ee5 tr\u00ean, l\u1edbp <\/span><span style=\"font-weight: 400;\">MyClass<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c khai b\u00e1o l\u00e0 <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\">, do \u0111\u00f3 kh\u00f4ng th\u1ec3 b\u1ecb k\u1ebf th\u1eeba trong l\u1edbp <\/span><span style=\"font-weight: 400;\">SubClass<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n\n\n\n<p>V\u00ed d\u1ee5 v\u1ec1 ph\u01b0\u01a1ng th\u1ee9c final:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">class Parent {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;public final void show() {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"This is a final method\");<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">class Child extends Parent {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;\/\/ Kh\u00f4ng th\u1ec3 ghi \u0111\u00e8 ph\u01b0\u01a1ng th\u1ee9c show() v\u00ec n\u00f3 l\u00e0 final<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;\/\/ public void show() { } \/\/ L\u1ed7i bi\u00ean d\u1ecbch: Cannot override the final method from Parent<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">\u1ede \u0111\u00e2y, ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">show()<\/span><span style=\"font-weight: 400;\"> trong l\u1edbp <\/span><span style=\"font-weight: 400;\">Parent<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c khai b\u00e1o l\u00e0 <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\">, do \u0111\u00f3 l\u1edbp con <\/span><span style=\"font-weight: 400;\">Child<\/span><span style=\"font-weight: 400;\"> kh\u00f4ng th\u1ec3 ghi \u0111\u00e8 (override) ph\u01b0\u01a1ng th\u1ee9c n\u00e0y.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lam-sao-d\u1ec3-t\u1ea1o-m\u1ed9t-l\u1edbp-b\u1ea5t-bi\u1ebfn-immutable-class-trong-java\"><strong>L\u00e0m sao \u0111\u1ec3 t\u1ea1o m\u1ed9t l\u1edbp b\u1ea5t bi\u1ebfn (immutable class) trong Java?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 t\u1ea1o m\u1ed9t l\u1edbp b\u1ea5t bi\u1ebfn (immutable class) trong Java, b\u1ea1n c\u1ea7n tu\u00e2n th\u1ee7 c\u00e1c b\u01b0\u1edbc sau:<\/span><\/p>\n\n\n\n<p><b>B\u01b0\u1edbc 1. \u0110\u00e1nh d\u1ea5u l\u1edbp l\u00e0 <\/b><b>final<\/b><b>:<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">L\u1edbp ph\u1ea3i \u0111\u01b0\u1ee3c khai b\u00e1o l\u00e0 <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 kh\u00f4ng th\u1ec3 b\u1ecb k\u1ebf th\u1eeba v\u00e0 thay \u0111\u1ed5i.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">public final class Person {<\/span><\/code><\/pre>\n\n\n\n<p><b>B\u01b0\u1edbc 2. \u0110\u00e1nh d\u1ea5u c\u00e1c tr\u01b0\u1eddng (fields) l\u00e0 <\/b><b>private<\/b><b> v\u00e0 <\/b><b>final<\/b><b>:<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ea3m b\u1ea3o r\u1eb1ng c\u00e1c tr\u01b0\u1eddng c\u1ee7a l\u1edbp kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i sau khi \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">private final String name;<\/span>\n\n<span style=\"font-weight: 400;\">private final int age;<\/span><\/code><\/pre>\n\n\n\n<p><b>B\u01b0\u1edbc 3. Kh\u1edfi t\u1ea1o gi\u00e1 tr\u1ecb c\u1ee7a c\u00e1c tr\u01b0\u1eddng qua constructor:<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">T\u1ea1o m\u1ed9t constructor \u0111\u1ec3 kh\u1edfi t\u1ea1o c\u00e1c tr\u01b0\u1eddng khi \u0111\u1ed1i t\u01b0\u1ee3ng \u0111\u01b0\u1ee3c t\u1ea1o ra. Constructor kh\u00f4ng n\u00ean cho ph\u00e9p thay \u0111\u1ed5i gi\u00e1 tr\u1ecb c\u1ee7a c\u00e1c tr\u01b0\u1eddng sau khi \u0111\u1ed1i t\u01b0\u1ee3ng \u0111\u00e3 \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public Person (String name, int age) {\n  this.name = name;\n  this.age = age;\n}<\/code><\/pre>\n\n\n\n<p><b>B\u01b0\u1edbc 4. Kh\u00f4ng cung c\u1ea5p setter methods:<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 \u0111\u1ea3m b\u1ea3o t\u00ednh b\u1ea5t bi\u1ebfn, kh\u00f4ng \u0111\u01b0\u1ee3c cung c\u1ea5p ph\u01b0\u01a1ng th\u1ee9c setter, v\u00ec ch\u00fang s\u1ebd thay \u0111\u1ed5i gi\u00e1 tr\u1ecb c\u1ee7a c\u00e1c tr\u01b0\u1eddng.<\/span><\/p>\n\n\n\n<p><b>B\u01b0\u1edbc 5. N\u1ebfu c\u00f3 c\u00e1c tr\u01b0\u1eddng tham chi\u1ebfu \u0111\u1ed1i t\u01b0\u1ee3ng (reference types), h\u00e3y t\u1ea1o b\u1ea3n sao c\u1ee7a ch\u00fang trong constructor v\u00e0 getter:<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">N\u1ebfu tr\u01b0\u1eddng l\u00e0 m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng (v\u00ed d\u1ee5 <\/span><span style=\"font-weight: 400;\">List<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">Date<\/span><span style=\"font-weight: 400;\">), b\u1ea1n c\u1ea7n \u0111\u1ea3m b\u1ea3o r\u1eb1ng kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i tr\u1ea1ng th\u00e1i c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng th\u00f4ng qua l\u1edbp b\u1ea5t bi\u1ebfn. H\u00e3y t\u1ea1o b\u1ea3n sao c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng trong c\u1ea3 constructor v\u00e0 getter.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private final Date birthDate;\n\npublic Person (String name, int age, Date birthDate) {\n  this.name = name;\n  this.age = age;\n  this.birthDate = new Date(birthDate.getTime()); \/\/ T\u1ea1o b\u1ea3n sao\n}\n\npublic Date getBirthDate() {\n  return new Date(birthDate.getTime()); \/\/ Tr\u1ea3 v\u1ec1 b\u1ea3n sao\n}<\/code><\/pre>\n\n\n\n<p><b>T\u00f3m l\u1ea1i:<\/b><span style=\"font-weight: 400;\"> \u0110\u1ec3 t\u1ea1o m\u1ed9t l\u1edbp b\u1ea5t bi\u1ebfn trong Java, b\u1ea1n c\u1ea7n \u0111\u1ea3m b\u1ea3o r\u1eb1ng l\u1edbp l\u00e0 <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\">, c\u00e1c tr\u01b0\u1eddng l\u00e0 <\/span><span style=\"font-weight: 400;\">private<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\">, ch\u1ec9 c\u00f3 getter v\u00e0 kh\u00f4ng c\u00f3 setter, v\u00e0 x\u1eed l\u00fd \u0111\u00fang c\u00e1c tr\u01b0\u1eddng tham chi\u1ebfu \u0111\u1ec3 b\u1ea3o v\u1ec7 t\u00ednh b\u1ea5t bi\u1ebfn c\u1ee7a ch\u00fang.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-phan-bi\u1ec7t-this-va-super-keyword-trong-java\"><strong>Ph\u00e2n bi\u1ec7t this v\u00e0 super keyword trong Java<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong Java, c\u1ea3 <\/span><span style=\"font-weight: 400;\">this<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">super<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec1u l\u00e0 c\u00e1c t\u1eeb kh\u00f3a tham chi\u1ebfu \u0111\u1ebfn \u0111\u1ed1i t\u01b0\u1ee3ng, nh\u01b0ng ch\u00fang c\u00f3 m\u1ee5c \u0111\u00edch v\u00e0 ph\u1ea1m vi s\u1eed d\u1ee5ng kh\u00e1c nhau:<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-this\"><b>this<\/b><\/h4>\n\n\n\n<p>Tham chi\u1ebfu \u0111\u1ebfn \u0111\u1ed1i t\u01b0\u1ee3ng hi\u1ec7n t\u1ea1i, t\u1ee9c l\u00e0 \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp hi\u1ec7n t\u1ea1i (l\u1edbp m\u00e0 ph\u01b0\u01a1ng th\u1ee9c ho\u1eb7c bi\u1ebfn n\u00e0y \u0111\u01b0\u1ee3c g\u1ecdi). N\u00f3 th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c t\u00ecnh hu\u1ed1ng:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">\u0110\u1ec3 ph\u00e2n bi\u1ec7t gi\u1eefa bi\u1ebfn instance v\u00e0 tham s\u1ed1 c\u1ee7a ph\u01b0\u01a1ng th\u1ee9c ho\u1eb7c constructor khi ch\u00fang c\u00f3 t\u00ean gi\u1ed1ng nhau.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">\u0110\u1ec3 g\u1ecdi constructor kh\u00e1c c\u1ee7a c\u00f9ng m\u1ed9t l\u1edbp t\u1eeb m\u1ed9t constructor kh\u00e1c.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyClass {\n  int x;\n\n  MyClass(int x) {\n    this.x = x; \/\/ 'this' ph\u00e2n bi\u1ec7t bi\u1ebfn instance 'x' v\u00e0 tham s\u1ed1 'x'\n  }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-super\"><b>super<\/b><\/h4>\n\n\n\n<p>Tham chi\u1ebfu \u0111\u1ebfn l\u1edbp cha (superclass) c\u1ee7a l\u1edbp hi\u1ec7n t\u1ea1i. N\u00f3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c t\u00ecnh hu\u1ed1ng:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">\u0110\u1ec3 g\u1ecdi constructor c\u1ee7a l\u1edbp cha.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">\u0110\u1ec3 truy c\u1eadp \u0111\u1ebfn c\u00e1c ph\u01b0\u01a1ng th\u1ee9c ho\u1eb7c bi\u1ebfn c\u1ee7a l\u1edbp cha b\u1ecb che khu\u1ea5t b\u1edfi l\u1edbp con.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Parent {\n  void display() {\n    System.out.println(\"Display of Parent\");\n  }\n}\n\nclass Child extends Parent {\n  void display() {\n    super.display(); \/\/ G\u1ecdi ph\u01b0\u01a1ng th\u1ee9c display() c\u1ee7a l\u1edbp cha\n    System.out.println(\"Display of Child\");\n  }\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u1ea3 <\/span><strong>this<\/strong><span style=\"font-weight: 400;\"> v\u00e0 <\/span><strong>super<\/strong><span style=\"font-weight: 400;\"> \u0111\u1ec1u \u0111\u00f3ng vai tr\u00f2 quan tr\u1ecdng trong vi\u1ec7c qu\u1ea3n l\u00fd \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 k\u1ebf th\u1eeba trong Java, gi\u00fap b\u1ea1n vi\u1ebft m\u00e3 r\u00f5 r\u00e0ng v\u00e0 hi\u1ec7u qu\u1ea3 h\u01a1n trong c\u00e1c m\u1ed1i quan h\u1ec7 gi\u1eefa c\u00e1c l\u1edbp.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-anonymous-class-la-gi-khi-nao-nen-s\u1eed-d\u1ee5ng\"><strong>Anonymous class l\u00e0 g\u00ec? Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Anonymous class trong Java l\u00e0 m\u1ed9t l\u1edbp kh\u00f4ng c\u00f3 t\u00ean v\u00e0 \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a ngay t\u1ea1i ch\u1ed7, th\u01b0\u1eddng l\u00e0 khi b\u1ea1n c\u1ea7n m\u1ed9t l\u1edbp \u0111\u1ec3 th\u1ef1c thi m\u1ed9t interface ho\u1eb7c k\u1ebf th\u1eeba m\u1ed9t l\u1edbp. N\u00f3 gi\u00fap ti\u1ebft ki\u1ec7m kh\u00f4ng gian m\u00e3 v\u00e0 th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong nh\u1eefng t\u00ecnh hu\u1ed1ng c\u1ea7n m\u1ed9t tri\u1ec3n khai t\u1ea1m th\u1eddi m\u00e0 kh\u00f4ng mu\u1ed1n t\u1ea1o ra m\u1ed9t l\u1edbp con ri\u00eang bi\u1ec7t.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Ch\u00fang ta th\u01b0\u1eddng s\u1eed d\u1ee5ng anonymous class khi:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">C\u1ea7n m\u1ed9t l\u1edbp ch\u1ec9 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng m\u1ed9t l\u1ea7n trong ph\u1ea1m vi c\u1ee7a m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c ho\u1eb7c block m\u00e3.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Mu\u1ed1n t\u1ea1o c\u00e1c instance c\u1ee7a l\u1edbp m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i khai b\u00e1o l\u1edbp \u0111\u00f3 tr\u01b0\u1edbc.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 \u0111i\u1ec3n h\u00ecnh l\u00e0 s\u1eed d\u1ee5ng trong callback, event handling, ho\u1eb7c trong c\u00e1c API nh\u01b0 Java Swing hay c\u00e1c th\u01b0 vi\u1ec7n x\u1eed l\u00fd s\u1ef1 ki\u1ec7n.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">Tuy nhi\u00ean, c\u1ea7n tr\u00e1nh s\u1eed d\u1ee5ng anonymous class khi logic c\u1ee7a l\u1edbp qu\u00e1 ph\u1ee9c t\u1ea1p ho\u1eb7c khi c\u1ea7n t\u00e1i s\u1eed d\u1ee5ng l\u1edbp nhi\u1ec1u l\u1ea7n, v\u00ec \u0111i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 g\u00e2y kh\u00f3 kh\u0103n trong vi\u1ec7c duy tr\u00ec m\u00e3 ngu\u1ed3n.<\/span><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Tham kh\u1ea3o y\u00eau c\u1ea7u c\u00f4ng vi\u1ec7c t\u1eeb <strong><a href=\"https:\/\/itviec.com\/it-jobs\/java?utm_medium=anchor_text_mid&amp;utm_source=blog&amp;utm_campaign=viec_lam_java&amp;utm_content=cau_hoi_phong_van_java\" target=\"_blank\" rel=\"noreferrer noopener\">vi\u1ec7c l\u00e0m Java<\/a><\/strong> to\u00e0n qu\u1ed1c m\u1edbi nh\u1ea5t \u0111\u1ec3 chu\u1ea9n b\u1ecb \u0111\u1ea7y \u0111\u1ee7 cho bu\u1ed5i ph\u1ecfng v\u1ea5n Java ti\u1ebfp theo.<\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cac-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-java-lien-quan-d\u1ebfn-java-collections-framework\"><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Java_lien_quan_den_Java_Collections_Framework\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java li\u00ean quan \u0111\u1ebfn Java Collections Framework<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-collections-framework-la-gi-k\u1ec3-ten-cac-interface-chinh-c\u1ee7a-java-collections-framework\"><strong>Collections Framework l\u00e0 g\u00ec? K\u1ec3 t\u00ean c\u00e1c interface ch\u00ednh c\u1ee7a Java Collections Framework<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/technotes\/guides\/collections\/overview.html\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">Collections Framework trong Java<\/span><\/a><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c l\u1edbp v\u00e0 interface gi\u00fap qu\u1ea3n l\u00fd nh\u00f3m \u0111\u1ed1i t\u01b0\u1ee3ng, bao g\u1ed3m c\u00e1c thao t\u00e1c nh\u01b0 l\u01b0u tr\u1eef, truy xu\u1ea5t v\u00e0 s\u1eafp x\u1ebfp d\u1eef li\u1ec7u. N\u00f3 cung c\u1ea5p c\u00e1c c\u1ea5u tr\u00fac d\u1eef li\u1ec7u nh\u01b0 <\/span><span style=\"font-weight: 400;\">List<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">Set<\/span><span style=\"font-weight: 400;\">, v\u00e0 <\/span><span style=\"font-weight: 400;\">Map<\/span><span style=\"font-weight: 400;\">, gi\u00fap l\u1eadp tr\u00ecnh vi\u00ean l\u1ef1a ch\u1ecdn v\u00e0 s\u1eed d\u1ee5ng c\u1ea5u tr\u00fac ph\u00f9 h\u1ee3p v\u1edbi y\u00eau c\u1ea7u c\u1ee7a \u1ee9ng d\u1ee5ng.<\/span><\/p>\n\n\n\n<p><b>Ba interface ch\u00ednh trong Collections Framework l\u00e0:<\/b><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><b>List<\/b><span style=\"font-weight: 400;\">: L\u00e0 m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c ph\u1ea7n t\u1eed c\u00f3 th\u1ee9 t\u1ef1 v\u00e0 c\u00f3 th\u1ec3 ch\u1ee9a c\u00e1c ph\u1ea7n t\u1eed tr\u00f9ng l\u1eb7p. C\u00e1c l\u1edbp tri\u1ec3n khai ph\u1ed5 bi\u1ebfn l\u00e0 <\/span><span style=\"font-weight: 400;\">ArrayList<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">LinkedList<\/span><span style=\"font-weight: 400;\">. <\/span><span style=\"font-weight: 400;\">List<\/span><span style=\"font-weight: 400;\"> cho ph\u00e9p truy c\u1eadp ph\u1ea7n t\u1eed theo ch\u1ec9 s\u1ed1 v\u00e0 duy tr\u00ec th\u1ee9 t\u1ef1 th\u00eam v\u00e0o.<\/span><\/li>\n\n\n\n<li><b>Set<\/b><span style=\"font-weight: 400;\">: L\u00e0 m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c ph\u1ea7n t\u1eed kh\u00f4ng c\u00f3 th\u1ee9 t\u1ef1 v\u00e0 kh\u00f4ng ch\u1ee9a ph\u1ea7n t\u1eed tr\u00f9ng l\u1eb7p. C\u00e1c l\u1edbp tri\u1ec3n khai ph\u1ed5 bi\u1ebfn l\u00e0 <\/span><span style=\"font-weight: 400;\">HashSet<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">LinkedHashSet<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">TreeSet<\/span><span style=\"font-weight: 400;\">. <\/span><span style=\"font-weight: 400;\">Set<\/span><span style=\"font-weight: 400;\"> kh\u00f4ng cho ph\u00e9p c\u00e1c ph\u1ea7n t\u1eed tr\u00f9ng l\u1eb7p, v\u00e0 th\u1ee9 t\u1ef1 c\u1ee7a c\u00e1c ph\u1ea7n t\u1eed kh\u00f4ng \u0111\u01b0\u1ee3c \u0111\u1ea3m b\u1ea3o (tr\u1eeb khi s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">LinkedHashSet<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">TreeSet<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n\n\n\n<li><b>Map<\/b><span style=\"font-weight: 400;\">: L\u00e0 m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c c\u1eb7p kh\u00f3a-gi\u00e1 tr\u1ecb, trong \u0111\u00f3 m\u1ed7i kh\u00f3a l\u00e0 duy nh\u1ea5t v\u00e0 li\u00ean k\u1ebft v\u1edbi m\u1ed9t gi\u00e1 tr\u1ecb. C\u00e1c l\u1edbp tri\u1ec3n khai ph\u1ed5 bi\u1ebfn l\u00e0 <\/span><span style=\"font-weight: 400;\">HashMap<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">LinkedHashMap<\/span><span style=\"font-weight: 400;\">, v\u00e0 <\/span><span style=\"font-weight: 400;\">TreeMap<\/span><span style=\"font-weight: 400;\">. <\/span><span style=\"font-weight: 400;\">Map<\/span><span style=\"font-weight: 400;\"> cho ph\u00e9p truy xu\u1ea5t gi\u00e1 tr\u1ecb th\u00f4ng qua kh\u00f3a.<\/span><\/li>\n<\/ol>\n\n\n\n<p><b>V\u00ed d\u1ee5 trong \u1ee9ng d\u1ee5ng:<\/b><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">N\u1ebfu c\u1ea7n duy tr\u00ec m\u1ed9t danh s\u00e1ch c\u00e1c ph\u1ea7n t\u1eed theo th\u1ee9 t\u1ef1 v\u00e0 c\u00f3 th\u1ec3 c\u00f3 tr\u00f9ng l\u1eb7p, ch\u00fang ta s\u1ebd s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">List<\/span><span style=\"font-weight: 400;\"> (v\u00ed d\u1ee5: <\/span><span style=\"font-weight: 400;\">ArrayList<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">N\u1ebfu kh\u00f4ng c\u1ea7n tr\u00f9ng l\u1eb7p v\u00e0 kh\u00f4ng quan t\u00e2m \u0111\u1ebfn th\u1ee9 t\u1ef1, <\/span><span style=\"font-weight: 400;\">Set<\/span><span style=\"font-weight: 400;\"> s\u1ebd l\u00e0 l\u1ef1a ch\u1ecdn ph\u00f9 h\u1ee3p (v\u00ed d\u1ee5: <\/span><span style=\"font-weight: 400;\">HashSet<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Khi c\u1ea7n l\u01b0u tr\u1eef d\u1eef li\u1ec7u d\u01b0\u1edbi d\u1ea1ng c\u00e1c c\u1eb7p kh\u00f3a-gi\u00e1 tr\u1ecb, <\/span><span style=\"font-weight: 400;\">Map<\/span><span style=\"font-weight: 400;\"> s\u1ebd l\u00e0 gi\u1ea3i ph\u00e1p t\u1ed1t nh\u1ea5t (v\u00ed d\u1ee5: <\/span><span style=\"font-weight: 400;\">HashMap<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-so-sanh-array-va-arraylist-trong-java\"><span style=\"font-weight: 400;\"><strong>So s\u00e1nh Array v\u00e0 ArrayList trong Java<\/strong><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Array<\/b><span style=\"font-weight: 400;\"> l\u00e0 c\u1ea5u tr\u00fac d\u1eef li\u1ec7u c\u01a1 b\u1ea3n, ph\u00f9 h\u1ee3p khi b\u1ea1n c\u1ea7n m\u1ed9t m\u1ea3ng v\u1edbi k\u00edch th\u01b0\u1edbc c\u1ed1 \u0111\u1ecbnh v\u00e0 kh\u00f4ng c\u1ea7n thay \u0111\u1ed5i k\u00edch th\u01b0\u1edbc trong qu\u00e1 tr\u00ecnh s\u1eed d\u1ee5ng.<\/span><\/li>\n\n\n\n<li><b>ArrayList<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t l\u1edbp trong Java Collection Framework, cung c\u1ea5p t\u00ednh linh ho\u1ea1t h\u01a1n trong vi\u1ec7c thay \u0111\u1ed5i k\u00edch th\u01b0\u1edbc v\u00e0 thao t\u00e1c v\u1edbi c\u00e1c ph\u1ea7n t\u1eed, nh\u01b0ng c\u00f3 chi ph\u00ed b\u1ed9 nh\u1edb v\u00e0 hi\u1ec7u su\u1ea5t cao h\u01a1n so v\u1edbi m\u1ea3ng truy\u1ec1n th\u1ed1ng.<\/span><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Ti\u00eau ch\u00ed&nbsp;<\/b><\/td><td><b>Array&nbsp;<\/b><\/td><td><b>ArrayList<\/b><\/td><\/tr><tr><td><b>K\u00edch th\u01b0\u1edbc<\/b><\/td><td><span style=\"font-weight: 400;\">K\u00edch th\u01b0\u1edbc c\u1ed1 \u0111\u1ecbnh, kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i sau khi kh\u1edfi t\u1ea1o.<\/span><\/td><td><span style=\"font-weight: 400;\">K\u00edch th\u01b0\u1edbc thay \u0111\u1ed5i \u0111\u1ed9ng khi th\u00eam\/b\u1edbt ph\u1ea7n t\u1eed.<\/span><\/td><\/tr><tr><td><b>Lo\u1ea1i d\u1eef li\u1ec7u<\/b><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 ch\u1ee9a c\u00e1c ph\u1ea7n t\u1eed c\u1ee7a ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y v\u00e0 \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/td><td><span style=\"font-weight: 400;\">Ch\u1ec9 ch\u1ee9a c\u00e1c ph\u1ea7n t\u1eed c\u1ee7a ki\u1ec3u \u0111\u1ed1i t\u01b0\u1ee3ng (Object).<\/span><\/td><\/tr><tr><td><b>Hi\u1ec7u su\u1ea5t<\/b><\/td><td><span style=\"font-weight: 400;\">Nhanh h\u01a1n trong truy c\u1eadp ph\u1ea7n t\u1eed do k\u00edch th\u01b0\u1edbc c\u1ed1 \u0111\u1ecbnh v\u00e0 l\u01b0u tr\u1eef li\u00ean ti\u1ebfp.<\/span><\/td><td><span style=\"font-weight: 400;\">Th\u1ea5p h\u01a1n v\u00ec ph\u1ea3i thay \u0111\u1ed5i k\u00edch th\u01b0\u1edbc v\u00e0 t\u1ea1o l\u1ea1i m\u1ea3ng khi c\u1ea7n.<\/span><\/td><\/tr><tr><td><b>D\u1ec5 s\u1eed d\u1ee5ng<\/b><\/td><td><span style=\"font-weight: 400;\">\u00cdt d\u1ec5 s\u1eed d\u1ee5ng h\u01a1n do kh\u00f4ng c\u00f3 c\u00e1c ph\u01b0\u01a1ng th\u1ee9c b\u1ed5 sung \u0111\u1ec3 thao t\u00e1c.<\/span><\/td><td><span style=\"font-weight: 400;\">D\u1ec5 s\u1eed d\u1ee5ng h\u01a1n nh\u1edd c\u00e1c ph\u01b0\u01a1ng th\u1ee9c nh\u01b0 <\/span><span style=\"font-weight: 400;\">add()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">remove()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">size()<\/span><span style=\"font-weight: 400;\">.<\/span><\/td><\/tr><tr><td><b>T\u00ednh linh ho\u1ea1t<\/b><\/td><td><span style=\"font-weight: 400;\">\u00cdt linh ho\u1ea1t, kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i k\u00edch th\u01b0\u1edbc sau khi kh\u1edfi t\u1ea1o.<\/span><\/td><td><span style=\"font-weight: 400;\">Linh ho\u1ea1t, c\u00f3 th\u1ec3 thay \u0111\u1ed5i k\u00edch th\u01b0\u1edbc t\u1ef1 \u0111\u1ed9ng khi ph\u1ea7n t\u1eed \u0111\u01b0\u1ee3c th\u00eam v\u00e0o ho\u1eb7c x\u00f3a.<\/span><\/td><\/tr><tr><td><b>S\u1eed d\u1ee5ng b\u1ed9 nh\u1edb<\/b><\/td><td><span style=\"font-weight: 400;\">D\u00f9ng b\u1ed9 nh\u1edb hi\u1ec7u qu\u1ea3 h\u01a1n v\u00ec kh\u00f4ng c\u00f3 s\u1ef1 b\u1ed5 sung chi ph\u00ed cho c\u1ea5u tr\u00fac d\u1eef li\u1ec7u.<\/span><\/td><td><span style=\"font-weight: 400;\">T\u1ed1n b\u1ed9 nh\u1edb h\u01a1n do ph\u1ea3i l\u01b0u tr\u1eef c\u1ea3 th\u00f4ng tin v\u1ec1 k\u00edch th\u01b0\u1edbc v\u00e0 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/td><\/tr><tr><td><b>Kh\u1ea3 n\u0103ng ch\u1ee9a ph\u1ea7n t\u1eed<\/b><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 ch\u1ee9a c\u00e1c ph\u1ea7n t\u1eed v\u1edbi c\u00f9ng ki\u1ec3u d\u1eef li\u1ec7u (ki\u1ec3u nguy\u00ean th\u1ee7y ho\u1eb7c \u0111\u1ed1i t\u01b0\u1ee3ng).<\/span><\/td><td><span style=\"font-weight: 400;\">Ch\u1ec9 ch\u1ee9a c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng, kh\u00f4ng th\u1ec3 ch\u1ee9a c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y tr\u1ef1c ti\u1ebfp.<\/span><\/td><\/tr><tr><td><b>Thao t\u00e1c th\u00eam\/x\u00f3a<\/b><\/td><td><span style=\"font-weight: 400;\">C\u1ea7n ph\u1ea3i t\u1ea1o m\u1ea3ng m\u1edbi \u0111\u1ec3 thay \u0111\u1ed5i k\u00edch th\u01b0\u1edbc.<\/span><\/td><td><span style=\"font-weight: 400;\">Cung c\u1ea5p c\u00e1c ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">add()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">remove()<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 th\u00eam\/x\u00f3a ph\u1ea7n t\u1eed d\u1ec5 d\u00e0ng.<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-phan-bi\u1ec7t-gi\u1eefa-arraylist-va-linkedlist\"><strong>Ph\u00e2n bi\u1ec7t gi\u1eefa ArrayList v\u00e0 LinkedList?<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>ArrayList<\/b><span style=\"font-weight: 400;\">: L\u00e0 m\u1ed9t l\u1edbp tri\u1ec3n khai c\u1ee7a <\/span><span style=\"font-weight: 400;\">List<\/span><span style=\"font-weight: 400;\"> s\u1eed d\u1ee5ng m\u1ea3ng \u0111\u1ed9ng \u0111\u1ec3 l\u01b0u tr\u1eef c\u00e1c ph\u1ea7n t\u1eed. N\u00f3 cung c\u1ea5p truy c\u1eadp nhanh \u0111\u1ebfn c\u00e1c ph\u1ea7n t\u1eed th\u00f4ng qua ch\u1ec9 m\u1ee5c (index) nh\u01b0ng hi\u1ec7u su\u1ea5t k\u00e9m khi th\u00eam ho\u1eb7c x\u00f3a ph\u1ea7n t\u1eed \u1edf gi\u1eefa danh s\u00e1ch v\u00ec ph\u1ea3i d\u1ecbch chuy\u1ec3n c\u00e1c ph\u1ea7n t\u1eed.<\/span><\/li>\n\n\n\n<li><b>LinkedList<\/b><span style=\"font-weight: 400;\">: L\u00e0 m\u1ed9t l\u1edbp tri\u1ec3n khai c\u1ee7a <\/span><span style=\"font-weight: 400;\">List<\/span><span style=\"font-weight: 400;\"> s\u1eed d\u1ee5ng danh s\u00e1ch li\u00ean k\u1ebft (linked list) \u0111\u1ec3 l\u01b0u tr\u1eef c\u00e1c ph\u1ea7n t\u1eed. N\u00f3 cho ph\u00e9p th\u00eam v\u00e0 x\u00f3a ph\u1ea7n t\u1eed nhanh ch\u00f3ng \u1edf \u0111\u1ea7u v\u00e0 cu\u1ed1i danh s\u00e1ch, nh\u01b0ng truy c\u1eadp c\u00e1c ph\u1ea7n t\u1eed theo ch\u1ec9 m\u1ee5c ch\u1eadm h\u01a1n v\u00ec ph\u1ea3i duy\u1ec7t qua c\u00e1c ph\u1ea7n t\u1eed tr\u01b0\u1edbc \u0111\u00f3.<\/span><\/li>\n<\/ul>\n\n\n\n<p>B\u1ea3ng so s\u00e1nh ArrayList vs LinkedList:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Ti\u00eau ch\u00ed&nbsp;<\/b><\/td><td><b>ArrayList<\/b><\/td><td><b>LinkedList<\/b><\/td><\/tr><tr><td><b>C\u1ea5u tr\u00fac d\u1eef li\u1ec7u<\/b><\/td><td><span style=\"font-weight: 400;\">D\u1ef1a tr\u00ean m\u1ea3ng (array)<\/span><\/td><td><span style=\"font-weight: 400;\">D\u1ef1a tr\u00ean danh s\u00e1ch li\u00ean k\u1ebft \u0111\u00f4i (doubly linked list)<\/span><\/td><\/tr><tr><td><b>Truy c\u1eadp ph\u1ea7n t\u1eed<\/b><\/td><td><span style=\"font-weight: 400;\">Nhanh (O(1) cho truy c\u1eadp tr\u1ef1c ti\u1ebfp)<\/span><\/td><td><span style=\"font-weight: 400;\">Ch\u1eadm (O(n) v\u00ec ph\u1ea3i duy\u1ec7t t\u1eeb \u0111\u1ea7u danh s\u00e1ch)<\/span><\/td><\/tr><tr><td><b>Th\u00eam\/X\u00f3a ph\u1ea7n t\u1eed gi\u1eefa danh s\u00e1ch<\/b><\/td><td><span style=\"font-weight: 400;\">Ch\u1eadm (O(n) do c\u1ea7n d\u1ecbch chuy\u1ec3n c\u00e1c ph\u1ea7n t\u1eed kh\u00e1c)<\/span><\/td><td><span style=\"font-weight: 400;\">Nhanh (O(1) khi c\u00f3 con tr\u1ecf \u0111\u1ebfn v\u1ecb tr\u00ed c\u1ea7n th\u00eam\/x\u00f3a)<\/span><\/td><\/tr><tr><td><b>Th\u00eam\/X\u00f3a ph\u1ea7n t\u1eed cu\u1ed1i danh s\u00e1ch<\/b><\/td><td><span style=\"font-weight: 400;\">Nhanh (O(1) n\u1ebfu ch\u01b0a \u0111\u1ea7y, O(n) khi c\u1ea7n m\u1edf r\u1ed9ng k\u00edch th\u01b0\u1edbc m\u1ea3ng)<\/span><\/td><td><span style=\"font-weight: 400;\">Nhanh (O(1) nh\u1edd s\u1eed d\u1ee5ng con tr\u1ecf cu\u1ed1i)<\/span><\/td><\/tr><tr><td><b>Th\u00eam\/X\u00f3a ph\u1ea7n t\u1eed \u0111\u1ea7u danh s\u00e1ch<\/b><\/td><td><span style=\"font-weight: 400;\">Ch\u1eadm (O(n) do c\u1ea7n d\u1ecbch chuy\u1ec3n c\u00e1c ph\u1ea7n t\u1eed kh\u00e1c)<\/span><\/td><td><span style=\"font-weight: 400;\">Nhanh (O(1) nh\u1edd con tr\u1ecf \u0111\u1ea7u)<\/span><\/td><\/tr><tr><td><b>B\u1ed9 nh\u1edb<\/b><\/td><td><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng b\u1ed9 nh\u1edb li\u00ean t\u1ee5c (m\u1ea3ng c\u00f3 k\u00edch th\u01b0\u1edbc c\u1ed1 \u0111\u1ecbnh ho\u1eb7c ph\u1ea3i t\u0103ng khi \u0111\u1ea7y)<\/span><\/td><td><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng b\u1ed9 nh\u1edb ph\u00e2n m\u1ea3nh (c\u1ea7n th\u00eam b\u1ed9 nh\u1edb cho con tr\u1ecf)<\/span><\/td><\/tr><tr><td><b>Tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng ph\u00f9 h\u1ee3p<\/b><\/td><td><span style=\"font-weight: 400;\">Khi truy c\u1eadp ph\u1ea7n t\u1eed theo ch\u1ec9 s\u1ed1 th\u01b0\u1eddng xuy\u00ean<\/span><\/td><td><span style=\"font-weight: 400;\">Khi th\u00eam\/x\u00f3a ph\u1ea7n t\u1eed \u1edf v\u1ecb tr\u00ed b\u1ea5t k\u1ef3 th\u01b0\u1eddng xuy\u00ean<\/span><\/td><\/tr><tr><td><b>Y\u00eau c\u1ea7u b\u1ed9 nh\u1edb<\/b><\/td><td><span style=\"font-weight: 400;\">B\u1ed9 nh\u1edb \u00edt h\u01a1n do kh\u00f4ng c\u1ea7n l\u01b0u th\u00f4ng tin v\u1ec1 li\u00ean k\u1ebft<\/span><\/td><td><span style=\"font-weight: 400;\">B\u1ed9 nh\u1edb nhi\u1ec1u h\u01a1n do c\u1ea7n l\u01b0u tr\u1eef th\u00f4ng tin li\u00ean k\u1ebft gi\u1eefa c\u00e1c ph\u1ea7n t\u1eed<\/span><\/td><\/tr><tr><td><b>Duy\u1ec7t danh s\u00e1ch<\/b><\/td><td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 d\u1ec5 d\u00e0ng duy\u1ec7t ng\u01b0\u1ee3c\/l\u1ea1i b\u1eb1ng ch\u1ec9 s\u1ed1<\/span><\/td><td><span style=\"font-weight: 400;\">Kh\u00f3 duy\u1ec7t ng\u01b0\u1ee3c\/l\u1ea1i n\u1ebfu kh\u00f4ng c\u00f3 con tr\u1ecf tr\u01b0\u1edbc \u0111\u00f3<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-hashmap-va-hashtable-khac-nhau-\u1edf-di\u1ec3m-nao\"><strong>HashMap v\u00e0 Hashtable kh\u00e1c nhau \u1edf \u0111i\u1ec3m n\u00e0o?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Ti\u00eau ch\u00ed&nbsp;<\/b><\/td><td><b>HashMap<\/b><\/td><td><b>Hashtable<\/b><\/td><\/tr><tr><td><b>Phi\u00ean b\u1ea3n<\/b><\/td><td><span style=\"font-weight: 400;\">Java 1.2 (thu\u1ed9c Collections Framework)<\/span><\/td><td><span style=\"font-weight: 400;\">Java 1.0 (thu\u1ed9c ph\u1ea7n l\u00f5i c\u1ee7a Java)<\/span><\/td><\/tr><tr><td><b>Thread-Safe<\/b><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng synchronized (kh\u00f4ng thread-safe)<\/span><\/td><td><span style=\"font-weight: 400;\">Synchronized (thread-safe)<\/span><\/td><\/tr><tr><td><b>Hi\u1ec7u su\u1ea5t<\/b><\/td><td><span style=\"font-weight: 400;\">Nhanh h\u01a1n v\u00ec kh\u00f4ng c\u00f3 synchronized<\/span><\/td><td><span style=\"font-weight: 400;\">Ch\u1eadm h\u01a1n do synchronized<\/span><\/td><\/tr><tr><td><b>Cho ph\u00e9p <\/b><b>null<\/b><\/td><td><span style=\"font-weight: 400;\">Cho ph\u00e9p m\u1ed9t kh\u00f3a null v\u00e0 nhi\u1ec1u gi\u00e1 tr\u1ecb null<\/span><\/td><td><span style=\"font-weight: 400;\">Kh\u00f4ng cho ph\u00e9p kh\u00f3a ho\u1eb7c gi\u00e1 tr\u1ecb null<\/span><\/td><\/tr><tr><td><b>S\u1eed d\u1ee5ng<\/b><\/td><td><span style=\"font-weight: 400;\">Th\u01b0\u1eddng d\u00f9ng khi kh\u00f4ng c\u1ea7n thread-safety<\/span><\/td><td><span style=\"font-weight: 400;\">D\u00f9ng khi c\u1ea7n thread-safety<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-cac-ph\u01b0\u01a1ng-phap-d\u1ec3-duy\u1ec7t-qua-m\u1ed9t-list-trong-java-for-each-iterator-stream\"><strong>C\u00e1c ph\u01b0\u01a1ng ph\u00e1p \u0111\u1ec3 duy\u1ec7t qua m\u1ed9t List trong Java? (for-each, Iterator, Stream)<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u00f3 ba ph\u01b0\u01a1ng ph\u00e1p ph\u1ed5 bi\u1ebfn \u0111\u1ec3 duy\u1ec7t qua m\u1ed9t <\/span><span style=\"font-weight: 400;\">List<\/span><span style=\"font-weight: 400;\"> trong Java:<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-dung-vong-l\u1eb7p-for-each\"><b>D\u00f9ng v\u00f2ng l\u1eb7p for-each<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u00e2y l\u00e0 c\u00e1ch \u0111\u01a1n gi\u1ea3n v\u00e0 d\u1ec5 hi\u1ec3u nh\u1ea5t. Ph\u01b0\u01a1ng ph\u00e1p n\u00e0y gi\u00fap b\u1ea1n duy\u1ec7t qua t\u1eebng ph\u1ea7n t\u1eed trong List m\u00e0 kh\u00f4ng c\u1ea7n quan t\u00e2m \u0111\u1ebfn ch\u1ec9 m\u1ee5c.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;String&gt; list = Arrays.asList(\"A\", \"B\", \"C\");\nfor (String item : list) {\n  System.out.println(item);\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-dung-iterator\"><b>D\u00f9ng Iterator<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Iterator cung c\u1ea5p m\u1ed9t c\u00e1ch ti\u1ebfp c\u1eadn linh ho\u1ea1t h\u01a1n, \u0111\u1eb7c bi\u1ec7t khi b\u1ea1n c\u1ea7n lo\u1ea1i b\u1ecf c\u00e1c ph\u1ea7n t\u1eed trong qu\u00e1 tr\u00ecnh duy\u1ec7t qua List. Iterator c\u0169ng h\u1ed7 tr\u1ee3 duy\u1ec7t qua c\u00e1c Collection m\u00e0 kh\u00f4ng c\u1ea7n quan t\u00e2m \u0111\u1ebfn lo\u1ea1i c\u1ee7a Collection \u0111\u00f3.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;String&gt; list = Arrays.asList(\"A\", \"B\", \"C\");\nIterator&lt;String&gt; iterator = list.iterator();\nwhile (iterator.hasNext()) {\n  System.out.println(iterator.next());\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-dung-stream-java-8-tr\u1edf-len\"><b>D\u00f9ng Stream (Java 8 tr\u1edf l\u00ean)<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng ph\u00e1p n\u00e0y s\u1eed d\u1ee5ng Stream API, cho ph\u00e9p b\u1ea1n \u00e1p d\u1ee5ng c\u00e1c ph\u00e9p to\u00e1n h\u00e0m (functional) nh\u01b0 filter, map, v\u00e0 forEach, gi\u00fap m\u00e3 ngu\u1ed3n g\u1ecdn g\u00e0ng v\u00e0 d\u1ec5 hi\u1ec3u h\u01a1n.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;String&gt; list = Arrays.asList(\"A\", \"B\", \"C\");\nlist.forEach (System.out::println);<\/code><\/pre>\n\n\n\n<p><b>Gi\u1ea3i th\u00edch th\u00eam<\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">for-each<\/span><span style=\"font-weight: 400;\"> l\u00e0 ph\u01b0\u01a1ng ph\u00e1p \u0111\u01a1n gi\u1ea3n nh\u1ea5t nh\u01b0ng kh\u00f4ng h\u1ed7 tr\u1ee3 c\u00e1c ph\u00e9p to\u00e1n ph\u1ee9c t\u1ea1p nh\u01b0 l\u1ecdc hay chuy\u1ec3n \u0111\u1ed5i d\u1eef li\u1ec7u.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Iterator<\/span><span style=\"font-weight: 400;\"> cho ph\u00e9p b\u1ea1n \u0111i\u1ec1u khi\u1ec3n qu\u00e1 tr\u00ecnh duy\u1ec7t linh ho\u1ea1t h\u01a1n, nh\u01b0ng c\u00fa ph\u00e1p c\u00f3 ph\u1ea7n ph\u1ee9c t\u1ea1p h\u01a1n.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\"> l\u00e0 l\u1ef1a ch\u1ecdn t\u1ed1t n\u1ebfu b\u1ea1n \u0111ang l\u00e0m vi\u1ec7c v\u1edbi Java 8 tr\u1edf l\u00ean v\u00e0 mu\u1ed1n t\u1eadn d\u1ee5ng c\u00e1c kh\u1ea3 n\u0103ng x\u1eed l\u00fd d\u1eef li\u1ec7u h\u00e0m nh\u01b0 l\u1ecdc, nh\u00f3m, ho\u1eb7c t\u00ednh to\u00e1n tr\u00ean c\u00e1c ph\u1ea7n t\u1eed c\u1ee7a List.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-treeset-la-gi-lam-th\u1ebf-nao-d\u1ec3-s\u1eafp-x\u1ebfp-m\u1ed9t-treeset\"><strong>TreeSet l\u00e0 g\u00ec? L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 s\u1eafp x\u1ebfp m\u1ed9t TreeSet?<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/?java\/util\/TreeSet.html\" target=\"_blank\" rel=\"noopener\"><b>TreeSet<\/b><\/a><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t l\u1edbp trong Java, thu\u1ed9c package <\/span><span style=\"font-weight: 400;\">java.util<\/span><span style=\"font-weight: 400;\">, tri\u1ec3n khai giao di\u1ec7n <\/span><span style=\"font-weight: 400;\">Set<\/span><span style=\"font-weight: 400;\">. N\u00f3 l\u01b0u tr\u1eef c\u00e1c ph\u1ea7n t\u1eed theo th\u1ee9 t\u1ef1 t\u1ef1 nhi\u00ean (natural order) ho\u1eb7c theo m\u1ed9t <\/span><span style=\"font-weight: 400;\">Comparator<\/span><span style=\"font-weight: 400;\"> n\u1ebfu \u0111\u01b0\u1ee3c cung c\u1ea5p. M\u1ed9t \u0111\u1eb7c \u0111i\u1ec3m quan tr\u1ecdng c\u1ee7a <\/span><span style=\"font-weight: 400;\">TreeSet<\/span><span style=\"font-weight: 400;\"> l\u00e0 kh\u00f4ng cho ph\u00e9p c\u00e1c ph\u1ea7n t\u1eed tr\u00f9ng l\u1eb7p v\u00e0 t\u1ef1 \u0111\u1ed9ng duy tr\u00ec th\u1ee9 t\u1ef1 s\u1eafp x\u1ebfp khi c\u00e1c ph\u1ea7n t\u1eed \u0111\u01b0\u1ee3c th\u00eam v\u00e0o.<\/span><\/p>\n\n\n\n<p><b>C\u00e1ch s\u1eafp x\u1ebfp m\u1ed9t TreeSet<\/b><span style=\"font-weight: 400;\">: M\u1eb7c \u0111\u1ecbnh, TreeSet s\u1eafp x\u1ebfp c\u00e1c ph\u1ea7n t\u1eed theo th\u1ee9 t\u1ef1 t\u1ef1 nhi\u00ean c\u1ee7a ch\u00fang (\u0111\u1ed1i v\u1edbi c\u00e1c ph\u1ea7n t\u1eed implement <\/span><span style=\"font-weight: 400;\">Comparable<\/span><span style=\"font-weight: 400;\">). N\u1ebfu b\u1ea1n mu\u1ed1n s\u1eafp x\u1ebfp theo m\u1ed9t ti\u00eau ch\u00ed kh\u00e1c, b\u1ea1n c\u00f3 th\u1ec3 cung c\u1ea5p m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng <\/span><span style=\"font-weight: 400;\">Comparator<\/span><span style=\"font-weight: 400;\"> khi kh\u1edfi t\u1ea1o TreeSet.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ S\u1eafp x\u1ebfp theo th\u1ee9 t\u1ef1 t\u1ef1 nhi\u00ean\nTreeSet&lt;Integer&gt; set = new TreeSet&lt;&gt;();\nset.add(10);\nset.add(5);\nset.add(20);\n\n\/\/ S\u1eafp x\u1ebfp theo m\u1ed9t Comparator (theo th\u1ee9 t\u1ef1 gi\u1ea3m d\u1ea7n)\nTreeSet&lt;Integer&gt; descendingSet = new\ndescendingSet.add(10);\ndescendingSet.add(5);\ndescendingSet.add(20);\nTreeSet&gt;(Comparator.reverseOrder());<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Nh\u01b0 v\u1eady, <\/span><span style=\"font-weight: 400;\">TreeSet<\/span><span style=\"font-weight: 400;\"> \u0111\u1ea3m b\u1ea3o r\u1eb1ng c\u00e1c ph\u1ea7n t\u1eed lu\u00f4n \u0111\u01b0\u1ee3c s\u1eafp x\u1ebfp theo m\u1ed9t quy t\u1eafc nh\u1ea5t \u0111\u1ecbnh (t\u1ef1 nhi\u00ean ho\u1eb7c theo <\/span><span style=\"font-weight: 400;\">Comparator<\/span><span style=\"font-weight: 400;\">), v\u00e0 kh\u00f4ng cho ph\u00e9p ph\u1ea7n t\u1eed tr\u00f9ng l\u1eb7p.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-comparator-va-comparable-khac-nhau-nh\u01b0-th\u1ebf-nao-khi-nao-dung-cai-nao\"><strong>Comparator v\u00e0 Comparable kh\u00e1c nhau nh\u01b0 th\u1ebf n\u00e0o? Khi n\u00e0o d\u00f9ng c\u00e1i n\u00e0o?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Comparable<\/span><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t interface \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 so s\u00e1nh c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng trong c\u00f9ng m\u1ed9t l\u1edbp. Khi m\u1ed9t l\u1edbp implement <\/span><span style=\"font-weight: 400;\">Comparable<\/span><span style=\"font-weight: 400;\">, c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp \u0111\u00f3 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eafp x\u1ebfp t\u1ef1 \u0111\u1ed9ng b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng c\u00e1c ph\u01b0\u01a1ng th\u1ee9c nh\u01b0 <\/span><span style=\"font-weight: 400;\">Collections.sort()<\/span><span style=\"font-weight: 400;\">. L\u1edbp n\u00e0y c\u1ea7n ph\u1ea3i implement ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">compareTo(T o)<\/span><span style=\"font-weight: 400;\">, gi\u00fap so s\u00e1nh \u0111\u1ed1i t\u01b0\u1ee3ng hi\u1ec7n t\u1ea1i v\u1edbi \u0111\u1ed1i t\u01b0\u1ee3ng kh\u00e1c.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Comparator<\/span><span style=\"font-weight: 400;\">, ng\u01b0\u1ee3c l\u1ea1i, l\u00e0 m\u1ed9t interface \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 so s\u00e1nh c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a nhi\u1ec1u l\u1edbp kh\u00e1c nhau ho\u1eb7c c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng m\u00e0 kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i m\u00e3 ngu\u1ed3n c\u1ee7a l\u1edbp. Khi s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">Comparator<\/span><span style=\"font-weight: 400;\">, b\u1ea1n c\u1ea7n implement ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">compare(T o1, T o2)<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 ch\u1ec9 r\u00f5 c\u00e1ch th\u1ee9c so s\u00e1nh hai \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/p>\n\n\n\n<p><b>Khi n\u00e0o s\u1eed d\u1ee5ng<\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">D\u00f9ng <\/span><span style=\"font-weight: 400;\">Comparable<\/span><span style=\"font-weight: 400;\"> khi b\u1ea1n mu\u1ed1n \u0111\u1ecbnh ngh\u0129a m\u1ed9t th\u1ee9 t\u1ef1 m\u1eb7c \u0111\u1ecbnh cho \u0111\u1ed1i t\u01b0\u1ee3ng trong m\u1ed9t l\u1edbp.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">D\u00f9ng <\/span><span style=\"font-weight: 400;\">Comparator<\/span><span style=\"font-weight: 400;\"> khi b\u1ea1n c\u1ea7n \u0111\u1ecbnh ngh\u0129a c\u00e1c c\u00e1ch so s\u00e1nh kh\u00e1c nhau ho\u1eb7c khi b\u1ea1n kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i m\u00e3 ngu\u1ed3n c\u1ee7a l\u1edbp.<\/span><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cac-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-java-lien-quan-d\u1ebfn-exception-handling-x\u1eed-ly-ngo\u1ea1i-l\u1ec7\"><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Java_lien_quan_den_Exception_Handling_Xu_ly_ngoai_le\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java li\u00ean quan \u0111\u1ebfn Exception Handling (X\u1eed l\u00fd ngo\u1ea1i l\u1ec7)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-ngo\u1ea1i-l\u1ec7-exception-trong-java-la-gi-phan-bi\u1ec7t-gi\u1eefa-checked-va-unchecked-exception\"><strong>Ngo\u1ea1i l\u1ec7 (Exception) trong Java l\u00e0 g\u00ec? Ph\u00e2n bi\u1ec7t gi\u1eefa Checked v\u00e0 Unchecked Exception.<\/strong><\/h3>\n\n\n\n<p><b>Ngo\u1ea1i l\u1ec7 (Exception)<\/b><span style=\"font-weight: 400;\"> trong Java<\/span><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t s\u1ef1 ki\u1ec7n kh\u00f4ng mong \u0111\u1ee3i x\u1ea3y ra trong qu\u00e1 tr\u00ecnh th\u1ef1c thi ch\u01b0\u01a1ng tr\u00ecnh, g\u00e2y gi\u00e1n \u0111o\u1ea1n lu\u1ed3ng \u0111i\u1ec1u khi\u1ec3n b\u00ecnh th\u01b0\u1eddng c\u1ee7a \u1ee9ng d\u1ee5ng. Khi m\u1ed9t ngo\u1ea1i l\u1ec7 x\u1ea3y ra, ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd t\u1ea1o ra m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng Exception v\u00e0 n\u00e9m (throw) n\u00f3 v\u00e0o m\u1ed9t n\u01a1i x\u1eed l\u00fd th\u00edch h\u1ee3p \u0111\u1ec3 \u1ee9ng d\u1ee5ng c\u00f3 th\u1ec3 ti\u1ebfp t\u1ee5c ho\u1eb7c k\u1ebft th\u00fac.<\/span><\/p>\n\n\n\n<p><b>Ph\u00e2n bi\u1ec7t gi\u1eefa Checked v\u00e0 Unchecked Exception:<\/b><\/p>\n\n\n\n<p><b>Checked Exception<\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">L\u00e0 c\u00e1c ngo\u1ea1i l\u1ec7 m\u00e0 tr\u00ecnh bi\u00ean d\u1ecbch y\u00eau c\u1ea7u ph\u1ea3i x\u1eed l\u00fd (b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">try-catch<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c khai b\u00e1o v\u1edbi <\/span><span style=\"font-weight: 400;\">throws<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Th\u01b0\u1eddng l\u00e0 c\u00e1c ngo\u1ea1i l\u1ec7 c\u00f3 th\u1ec3 d\u1ef1 \u0111o\u00e1n \u0111\u01b0\u1ee3c v\u00e0 c\u00f3 kh\u1ea3 n\u0103ng x\u1eed l\u00fd, ch\u1eb3ng h\u1ea1n nh\u01b0 <\/span><span style=\"font-weight: 400;\">IOException<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">SQLException<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n  FileReader file = new FileReader(\"file.txt\");\n} catch (IOException e) {\n  e.printStackTrace();\n}<\/code><\/pre>\n\n\n\n<p><b>Unchecked Exception<\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">L\u00e0 c\u00e1c ngo\u1ea1i l\u1ec7 kh\u00f4ng b\u1eaft bu\u1ed9c ph\u1ea3i x\u1eed l\u00fd, v\u00e0 th\u01b0\u1eddng x\u1ea3y ra do l\u1ed7i l\u1eadp tr\u00ecnh (v\u00ed d\u1ee5: chia cho 0, truy c\u1eadp m\u1ea3ng ngo\u00e0i ph\u1ea1m vi, v.v.).<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">K\u1ebf th\u1eeba t\u1eeb <\/span><span style=\"font-weight: 400;\">RuntimeException<\/span><span style=\"font-weight: 400;\"> v\u00e0 kh\u00f4ng y\u00eau c\u1ea7u khai b\u00e1o hay x\u1eed l\u00fd \u0111\u1eb7c bi\u1ec7t.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: <code>NullPointerException<\/code>, <code>ArrayIndexOutOfBoundsException<\/code>.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int&#91;] arr = new int&#91;5];\narr&#91;10] = 50; \/\/ Throws ArrayIndexOutOfBoundsException<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-cac-kh\u1ed1i-try-catch-finally-va-throw-throws-co-vai-tro-gi-trong-x\u1eed-ly-ngo\u1ea1i-l\u1ec7\"><strong>C\u00e1c kh\u1ed1i try, catch, finally v\u00e0 throw, throws c\u00f3 vai tr\u00f2 g\u00ec trong x\u1eed l\u00fd ngo\u1ea1i l\u1ec7?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong Java, c\u00e1c kh\u1ed1i <\/span><span style=\"font-weight: 400;\">try<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">catch<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">finally<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">throw<\/span><span style=\"font-weight: 400;\">, v\u00e0 <\/span><span style=\"font-weight: 400;\">throws<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec1u c\u00f3 vai tr\u00f2 quan tr\u1ecdng trong vi\u1ec7c x\u1eed l\u00fd ngo\u1ea1i l\u1ec7:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>try<\/b><span style=\"font-weight: 400;\">: \u0110\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 bao b\u1ecdc m\u00e3 c\u00f3 th\u1ec3 g\u00e2y ra ngo\u1ea1i l\u1ec7. N\u1ebfu m\u1ed9t ngo\u1ea1i l\u1ec7 x\u1ea3y ra trong kh\u1ed1i <\/span><span style=\"font-weight: 400;\">try<\/span><span style=\"font-weight: 400;\">, ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd chuy\u1ec3n sang kh\u1ed1i <\/span><span style=\"font-weight: 400;\">catch<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 x\u1eed l\u00fd ngo\u1ea1i l\u1ec7 \u0111\u00f3.<\/span><\/li>\n\n\n\n<li><b>catch<\/b><span style=\"font-weight: 400;\">: \u0110\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 b\u1eaft v\u00e0 x\u1eed l\u00fd ngo\u1ea1i l\u1ec7. Kh\u1ed1i <\/span><span style=\"font-weight: 400;\">catch<\/span><span style=\"font-weight: 400;\"> s\u1ebd x\u1eed l\u00fd nh\u1eefng ngo\u1ea1i l\u1ec7 \u0111\u01b0\u1ee3c n\u00e9m ra t\u1eeb kh\u1ed1i <\/span><span style=\"font-weight: 400;\">try<\/span><span style=\"font-weight: 400;\">. C\u00f3 th\u1ec3 c\u00f3 nhi\u1ec1u kh\u1ed1i <\/span><span style=\"font-weight: 400;\">catch<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c lo\u1ea1i ngo\u1ea1i l\u1ec7 kh\u00e1c nhau.<\/span><\/li>\n\n\n\n<li><b>finally<\/b><span style=\"font-weight: 400;\">: \u0110\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o m\u00e3 trong \u0111\u00f3 lu\u00f4n \u0111\u01b0\u1ee3c th\u1ef1c thi, d\u00f9 c\u00f3 x\u1ea3y ra ngo\u1ea1i l\u1ec7 hay kh\u00f4ng. Th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 \u0111\u00f3ng c\u00e1c t\u00e0i nguy\u00ean nh\u01b0 file, k\u1ebft n\u1ed1i c\u01a1 s\u1edf d\u1eef li\u1ec7u.<\/span><\/li>\n\n\n\n<li><b>throw<\/b><span style=\"font-weight: 400;\">: D\u00f9ng \u0111\u1ec3 n\u00e9m m\u1ed9t ngo\u1ea1i l\u1ec7 do ng\u01b0\u1eddi l\u1eadp tr\u00ecnh t\u1ea1o ra (th\u01b0\u1eddng l\u00e0 m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng thu\u1ed9c l\u1edbp <\/span><span style=\"font-weight: 400;\">Throwable<\/span><span style=\"font-weight: 400;\">). \u0110i\u1ec1u n\u00e0y cho ph\u00e9p \u0111i\u1ec1u khi\u1ec3n r\u00f5 r\u00e0ng v\u1ec1 c\u00e1ch x\u1eed l\u00fd ngo\u1ea1i l\u1ec7 trong \u1ee9ng d\u1ee5ng.<\/span><\/li>\n\n\n\n<li><b>throws<\/b><span style=\"font-weight: 400;\">: \u0110\u01b0\u1ee3c s\u1eed d\u1ee5ng trong khai b\u00e1o ph\u01b0\u01a1ng th\u1ee9c \u0111\u1ec3 ch\u1ec9 ra r\u1eb1ng ph\u01b0\u01a1ng th\u1ee9c c\u00f3 th\u1ec3 n\u00e9m m\u1ed9t ho\u1eb7c nhi\u1ec1u lo\u1ea1i ngo\u1ea1i l\u1ec7. \u0110i\u1ec1u n\u00e0y y\u00eau c\u1ea7u ph\u01b0\u01a1ng th\u1ee9c g\u1ecdi ph\u1ea3i x\u1eed l\u00fd ho\u1eb7c khai b\u00e1o ngo\u1ea1i l\u1ec7 \u0111\u00f3.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">M\u1ed7i ph\u1ea7n trong h\u1ec7 th\u1ed1ng x\u1eed l\u00fd ngo\u1ea1i l\u1ec7 gi\u00fap qu\u1ea3n l\u00fd v\u00e0 \u0111\u1ea3m b\u1ea3o \u1ee9ng d\u1ee5ng c\u00f3 th\u1ec3 x\u1eed l\u00fd c\u00e1c t\u00ecnh hu\u1ed1ng l\u1ed7i m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3 v\u00e0 an to\u00e0n.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-t\u1ea1i-sao-nen-s\u1eed-d\u1ee5ng-cac-ngo\u1ea1i-l\u1ec7-tuy-ch\u1ec9nh-custom-exceptions\"><strong>T\u1ea1i sao n\u00ean s\u1eed d\u1ee5ng c\u00e1c ngo\u1ea1i l\u1ec7 t\u00f9y ch\u1ec9nh (Custom Exceptions)?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><b>ngo\u1ea1i l\u1ec7 t\u00f9y ch\u1ec9nh trong Java (Custom Exceptions)<\/b><span style=\"font-weight: 400;\"> mang l\u1ea1i c\u00e1c l\u1ee3i \u00edch sau:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">T\u1ea1o ra c\u00e1c <\/span><b>th\u00f4ng b\u00e1o l\u1ed7i r\u00f5 r\u00e0ng v\u00e0 d\u1ec5 hi\u1ec3u h\u01a1n<\/b><span style=\"font-weight: 400;\">, gi\u00fap vi\u1ec7c x\u1eed l\u00fd v\u00e0 g\u1ee1 l\u1ed7i d\u1ec5 d\u00e0ng.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Cung c\u1ea5p kh\u1ea3 n\u0103ng <\/span><b>ki\u1ec3m so\u00e1t t\u1ed1t h\u01a1n<\/b><span style=\"font-weight: 400;\"> v\u1ec1 c\u00e1c t\u00ecnh hu\u1ed1ng c\u1ee5 th\u1ec3 trong \u1ee9ng d\u1ee5ng.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Cho ph\u00e9p <\/span><b>ph\u00e2n bi\u1ec7t c\u00e1c l\u1ed7i \u0111\u1eb7c th\u00f9<\/b><span style=\"font-weight: 400;\"> v\u00e0 x\u1eed l\u00fd ch\u00fang ch\u00ednh x\u00e1c.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Tr\u00e1nh ph\u1ea3i d\u1ef1a v\u00e0o <\/span><b>c\u00e1c ngo\u1ea1i l\u1ec7 m\u1eb7c \u0111\u1ecbnh c\u1ee7a Java<\/b><span style=\"font-weight: 400;\">, v\u00ec ch\u00fang c\u00f3 th\u1ec3 kh\u00f4ng \u0111\u1ee7 r\u00f5 r\u00e0ng ho\u1eb7c thi\u1ebfu th\u00f4ng tin v\u1ec1 nguy\u00ean nh\u00e2n l\u1ed7i.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-n\u1ebfu-finally-block-co-ch\u1ee9a-l\u1ec7nh-return-k\u1ebft-qu\u1ea3-s\u1ebd-nh\u01b0-th\u1ebf-nao\"><strong>N\u1ebfu finally block c\u00f3 ch\u1ee9a l\u1ec7nh return, k\u1ebft qu\u1ea3 s\u1ebd nh\u01b0 th\u1ebf n\u00e0o?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Khi m\u1ed9t l\u1ec7nh <\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> xu\u1ea5t hi\u1ec7n trong <\/span><span style=\"font-weight: 400;\">finally<\/span><span style=\"font-weight: 400;\"> block, n\u00f3 s\u1ebd ghi \u0111\u00e8 l\u00ean gi\u00e1 tr\u1ecb tr\u1ea3 v\u1ec1 t\u1eeb ph\u01b0\u01a1ng th\u1ee9c, k\u1ec3 c\u1ea3 khi c\u00f3 <\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> trong <\/span><span style=\"font-weight: 400;\">try<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">catch<\/span><span style=\"font-weight: 400;\"> block. \u0110i\u1ec1u n\u00e0y c\u00f3 ngh\u0129a l\u00e0 d\u00f9 tr\u01b0\u1edbc \u0111\u00f3 c\u00f3 x\u1ea3y ra l\u1ec7nh <\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> trong c\u00e1c block kh\u00e1c hay kh\u00f4ng, gi\u00e1 tr\u1ecb tr\u1ea3 v\u1ec1 cu\u1ed1i c\u00f9ng c\u1ee7a ph\u01b0\u01a1ng th\u1ee9c s\u1ebd l\u00e0 gi\u00e1 tr\u1ecb c\u1ee7a l\u1ec7nh <\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> trong <\/span><span style=\"font-weight: 400;\">finally<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Tuy nhi\u00ean, \u0111i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 g\u00e2y ra c\u00e1c h\u00e0nh vi kh\u00f4ng mong mu\u1ed1n v\u00e0 l\u00e0m cho m\u00e3 tr\u1edf n\u00ean kh\u00f3 hi\u1ec3u, v\u00ec <\/span><span style=\"font-weight: 400;\">finally<\/span><span style=\"font-weight: 400;\"> block th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 th\u1ef1c thi c\u00e1c thao t\u00e1c d\u1ecdn d\u1eb9p, ch\u1eb3ng h\u1ea1n nh\u01b0 \u0111\u00f3ng t\u00e0i nguy\u00ean. Vi\u1ec7c \u0111\u1eb7t l\u1ec7nh <\/span><span style=\"font-weight: 400;\">return<\/span><span style=\"font-weight: 400;\"> trong <\/span><span style=\"font-weight: 400;\">finally<\/span><span style=\"font-weight: 400;\"> c\u00f3 th\u1ec3 khi\u1ebfn vi\u1ec7c qu\u1ea3n l\u00fd lu\u1ed3ng \u0111i\u1ec1u khi\u1ec3n tr\u1edf n\u00ean ph\u1ee9c t\u1ea1p h\u01a1n.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Return Example {\n  public static void main(String&#91;] args) {\n    System.out.println(\"K\u1ebft qu\u1ea3: \" + testMethod());\n  }\n\n  public static int testMethod() {\n    try {\n      System.out.println(\"Trong try block\");\n      return 1; \/\/ L\u1ec7nh return trong try block\n    } catch (Exception e) {\n      System.out.println(\"Trong catch block\");\n      return 2; \/\/ L\u1ec7nh return trong catch block (n\u1ebfu c\u00f3 l\u1ed7i)\n    } finally {\n      System.out.println(\"Trong finally block\");\n      return 3; \/\/ L\u1ec7nh return trong finally block s\u1ebd ghi \u0111\u00e8\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p><b>Gi\u1ea3i th\u00edch \u0111o\u1ea1n code:<\/b><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">try block: Ph\u1ea7n n\u00e0y c\u1ed1 g\u1eafng th\u1ef1c thi \u0111o\u1ea1n m\u00e3. N\u1ebfu kh\u00f4ng c\u00f3 l\u1ed7i, ch\u01b0\u01a1ng tr\u00ecnh s\u1ebd th\u1ef1c thi l\u1ec7nh return 1;.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">catch block: N\u1ebfu x\u1ea3y ra ngo\u1ea1i l\u1ec7, \u0111o\u1ea1n m\u00e3 trong catch s\u1ebd ch\u1ea1y, v\u00e0 l\u1ec7nh return 2; s\u1ebd \u0111\u01b0\u1ee3c th\u1ef1c thi. Trong v\u00ed d\u1ee5 n\u00e0y, kh\u00f4ng c\u00f3 l\u1ed7i, n\u00ean catch block kh\u00f4ng \u0111\u01b0\u1ee3c g\u1ecdi.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">finally block: Ph\u1ea7n n\u00e0y lu\u00f4n lu\u00f4n \u0111\u01b0\u1ee3c th\u1ef1c thi b\u1ea5t k\u1ec3 c\u00f3 l\u1ed7i hay kh\u00f4ng. Khi c\u00f3 l\u1ec7nh return trong finally, n\u00f3 s\u1ebd ghi \u0111\u00e8 l\u00ean b\u1ea5t k\u1ef3 gi\u00e1 tr\u1ecb tr\u1ea3 v\u1ec1 n\u00e0o t\u1eeb try ho\u1eb7c catch.<\/span><\/li>\n<\/ol>\n\n\n\n<p><b>K\u1ebft qu\u1ea3:<\/b><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 c\u1ee7a ph\u01b0\u01a1ng th\u1ee9c testMethod() s\u1ebd l\u00e0 3, m\u1eb7c d\u00f9 try block \u0111\u00e3 c\u00f3 l\u1ec7nh return 1. L\u00fd do l\u00e0 l\u1ec7nh return 3 trong finally \u0111\u00e3 ghi \u0111\u00e8 l\u00ean gi\u00e1 tr\u1ecb tr\u1ea3 v\u1ec1 tr\u01b0\u1edbc \u0111\u00f3.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-nullpointerexception-la-gi-cac-cach-phong-tranh-th\u01b0\u1eddng-g\u1eb7p\"><strong>NullPointerException l\u00e0 g\u00ec? C\u00e1c c\u00e1ch ph\u00f2ng tr\u00e1nh th\u01b0\u1eddng g\u1eb7p?<\/strong><\/h3>\n\n\n\n<p><strong>NullPointerException<\/strong><span style=\"font-weight: 400;\"> l\u00e0 l\u1ed7i x\u1ea3y ra khi b\u1ea1n c\u1ed1 g\u1eafng truy c\u1eadp ho\u1eb7c thao t\u00e1c v\u1edbi m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng m\u00e0 ch\u01b0a \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o, t\u1ee9c l\u00e0 \u0111\u1ed1i t\u01b0\u1ee3ng c\u00f3 gi\u00e1 tr\u1ecb <\/span><span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\">. \u0110\u00e2y l\u00e0 m\u1ed9t trong nh\u1eefng l\u1ed7i ph\u1ed5 bi\u1ebfn nh\u1ea5t trong Java v\u00e0 c\u00f3 th\u1ec3 g\u00e2y ra s\u1ef1 c\u1ed1 khi ch\u1ea1y \u1ee9ng d\u1ee5ng.<\/span><\/p>\n\n\n\n<p>C\u00e1c c\u00e1ch ph\u00f2ng tr\u00e1nh:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Ki\u1ec3m tra gi\u00e1 tr\u1ecb null:<\/b><span style=\"font-weight: 400;\"> Tr\u01b0\u1edbc khi s\u1eed d\u1ee5ng m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng, h\u00e3y lu\u00f4n ki\u1ec3m tra xem n\u00f3 c\u00f3 ph\u1ea3i l\u00e0 <\/span><span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\"> kh\u00f4ng.<\/span><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>if (object != null) {\n  object.method();\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>S\u1eed d\u1ee5ng Optional:<\/b><span style=\"font-weight: 400;\"> Java 8 cung c\u1ea5p l\u1edbp <\/span><span style=\"font-weight: 400;\">Optional<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c gi\u00e1 tr\u1ecb c\u00f3 th\u1ec3 l\u00e0 <\/span><span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\"> m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i ki\u1ec3m tra <\/span><span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\"> tr\u1ef1c ti\u1ebfp.<\/span><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Optional&lt;String&gt; opt = Optional.ofNullable(value);\nopt.ifPresent(v -&gt; System.out.println(v));<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Kh\u1edfi t\u1ea1o gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh:<\/b><span style=\"font-weight: 400;\"> N\u1ebfu c\u00f3 th\u1ec3, h\u00e3y kh\u1edfi t\u1ea1o c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng v\u1edbi gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh thay v\u00ec \u0111\u1ec3 ch\u00fang l\u00e0 <\/span><span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>String name = \"Unknown\"; \/\/ thay v\u00ec null<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>S\u1eed d\u1ee5ng Annotations:<\/b><span style=\"font-weight: 400;\"> B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00e1c annotation nh\u01b0 <\/span><span style=\"font-weight: 400;\">@NonNull<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">@Nullable<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 ch\u1ec9 ra c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u00f3 th\u1ec3 ho\u1eb7c kh\u00f4ng th\u1ec3 c\u00f3 gi\u00e1 tr\u1ecb <\/span><span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\">, gi\u00fap c\u1ea3i thi\u1ec7n t\u00ednh an to\u00e0n trong m\u00e3 ngu\u1ed3n.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">B\u1eb1ng c\u00e1ch n\u00e0y, b\u1ea1n c\u00f3 th\u1ec3 tr\u00e1nh \u0111\u01b0\u1ee3c <\/span><span style=\"font-weight: 400;\">NullPointerException<\/span><span style=\"font-weight: 400;\"> v\u00e0 c\u1ea3i thi\u1ec7n s\u1ef1 \u1ed5n \u0111\u1ecbnh c\u1ee7a \u1ee9ng d\u1ee5ng.<\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cac-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-java-lien-quan-d\u1ebfn-java-multithreading-da-lu\u1ed3ng-trong-java\"><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Java_lien_quan_den_Java_Multithreading_Da_luong_trong_Java\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java li\u00ean quan \u0111\u1ebfn Java Multithreading (\u0110a lu\u1ed3ng trong Java)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-thread-la-gi-co-nh\u1eefng-cach-nao-d\u1ec3-t\u1ea1o-thread-trong-java\"><strong>Thread l\u00e0 g\u00ec? C\u00f3 nh\u1eefng c\u00e1ch n\u00e0o \u0111\u1ec3 t\u1ea1o thread trong Java?<\/strong><\/h3>\n\n\n\n<p><b>Thread<\/b><span style=\"font-weight: 400;\"> trong Java l\u00e0 m\u1ed9t \u0111\u01a1n v\u1ecb th\u1ef1c thi \u0111\u1ed9c l\u1eadp trong m\u1ed9t ch\u01b0\u01a1ng tr\u00ecnh. N\u00f3 cho ph\u00e9p th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 \u0111\u1ed3ng th\u1eddi, gi\u00fap t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t \u1ee9ng d\u1ee5ng, \u0111\u1eb7c bi\u1ec7t khi c\u00f3 nhi\u1ec1u t\u00e1c v\u1ee5 c\u1ea7n x\u1eed l\u00fd \u0111\u1ed3ng th\u1eddi (v\u00ed d\u1ee5: t\u1ea3i d\u1eef li\u1ec7u t\u1eeb server, x\u1eed l\u00fd giao di\u1ec7n ng\u01b0\u1eddi d\u00f9ng, v.v.).<\/span><\/p>\n\n\n\n<p><b>C\u00f3 hai c\u00e1ch ch\u00ednh \u0111\u1ec3 t\u1ea1o thread trong Java:<\/b><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-k\u1ebf-th\u1eeba-l\u1edbp-thread\"><b>K\u1ebf th\u1eeba l\u1edbp <\/b><b>Thread<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 t\u1ea1o m\u1ed9t l\u1edbp con k\u1ebf th\u1eeba l\u1edbp <\/span><span style=\"font-weight: 400;\">Thread<\/span><span style=\"font-weight: 400;\"> v\u00e0 ghi \u0111\u00e8 ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">run()<\/span><span style=\"font-weight: 400;\">. Sau \u0111\u00f3, t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp n\u00e0y v\u00e0 g\u1ecdi ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">start()<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 b\u1eaft \u0111\u1ea7u th\u1ef1c thi.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">class MyThread extends Thread {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;public void run() {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Thread is running\");<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">public class Main {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MyThread t = new MyThread();<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.start(); \/\/ B\u1eaft \u0111\u1ea7u thread<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-cai-d\u1eb7t-interface-runnable\"><b>C\u00e0i \u0111\u1eb7t interface <\/b><b>Runnable<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">L\u1edbp c\u00e0i \u0111\u1eb7t interface <\/span><span style=\"font-weight: 400;\">Runnable<\/span><span style=\"font-weight: 400;\"> ph\u1ea3i ghi \u0111\u00e8 ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">run()<\/span><span style=\"font-weight: 400;\">. B\u1ea1n t\u1ea1o m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp n\u00e0y v\u00e0 truy\u1ec1n n\u00f3 cho m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng <\/span><span style=\"font-weight: 400;\">Thread<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 ch\u1ea1y.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">class MyRunnable implements Runnable {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;public void run() {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Runnable is running\");<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">public class Main {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) {<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MyRunnable runnable = new MyRunnable();<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread t = new Thread(runnable);<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.start(); \/\/ B\u1eaft \u0111\u1ea7u thread<\/span>\n<span style=\"font-weight: 400;\"> &nbsp;&nbsp;&nbsp;}<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-s\u1ef1-khac-bi\u1ec7t-gi\u1eefa-runnable-va-thread-class-trong-java\"><strong>S\u1ef1 kh\u00e1c bi\u1ec7t gi\u1eefa Runnable v\u00e0 Thread class trong Java?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong Java, c\u1ea3 <\/span><span style=\"font-weight: 400;\">Runnable<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">Thread<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec1u d\u00f9ng \u0111\u1ec3 th\u1ef1c thi c\u00e1c t\u00e1c v\u1ee5 trong m\u1ed9t lu\u1ed3ng (thread), nh\u01b0ng c\u00f3 s\u1ef1 kh\u00e1c bi\u1ec7t quan tr\u1ecdng:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Runnable<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t interface, \u0111\u1ecbnh ngh\u0129a m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">run()<\/span><span style=\"font-weight: 400;\">. \u0110\u1ec3 s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">Runnable<\/span><span style=\"font-weight: 400;\">, b\u1ea1n c\u1ea7n t\u1ea1o m\u1ed9t l\u1edbp implement interface n\u00e0y v\u00e0 sau \u0111\u00f3 truy\u1ec1n \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp \u0111\u00f3 cho m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng <\/span><span style=\"font-weight: 400;\">Thread<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 th\u1ef1c thi.<\/span><\/li>\n\n\n\n<li><b>Thread<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t l\u1edbp con c\u1ee7a <\/span><span style=\"font-weight: 400;\">java.lang.Thread<\/span><span style=\"font-weight: 400;\"> v\u00e0 \u0111\u00e3 c\u00e0i s\u1eb5n ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">run()<\/span><span style=\"font-weight: 400;\">. B\u1ea1n c\u00f3 th\u1ec3 k\u1ebf th\u1eeba l\u1edbp <\/span><span style=\"font-weight: 400;\">Thread<\/span><span style=\"font-weight: 400;\"> v\u00e0 override ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">run()<\/span><span style=\"font-weight: 400;\">, ho\u1eb7c t\u1ea1o m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng <\/span><span style=\"font-weight: 400;\">Thread<\/span><span style=\"font-weight: 400;\"> v\u00e0 truy\u1ec1n v\u00e0o m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng <\/span><span style=\"font-weight: 400;\">Runnable<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 th\u1ef1c thi.<\/span><\/li>\n<\/ul>\n\n\n\n<p><b>M\u1ee5c \u0111\u00edch s\u1eed d\u1ee5ng:<\/b><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Runnable<\/span><span style=\"font-weight: 400;\"> ph\u00f9 h\u1ee3p h\u01a1n khi b\u1ea1n mu\u1ed1n chia s\u1ebb t\u00e0i nguy\u00ean gi\u1eefa c\u00e1c lu\u1ed3ng ho\u1eb7c khi b\u1ea1n kh\u00f4ng c\u1ea7n ph\u1ea3i k\u1ebf th\u1eeba <\/span><span style=\"font-weight: 400;\">Thread<\/span><span style=\"font-weight: 400;\"> (v\u00ec Java ch\u1ec9 h\u1ed7 tr\u1ee3 k\u1ebf th\u1eeba m\u1ed9t l\u1edbp duy nh\u1ea5t).<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">Thread<\/span><span style=\"font-weight: 400;\"> th\u00edch h\u1ee3p khi b\u1ea1n mu\u1ed1n ki\u1ec3m so\u00e1t tr\u1ef1c ti\u1ebfp lu\u1ed3ng v\u00e0 c\u00f3 th\u1ec3 override c\u00e1c ph\u01b0\u01a1ng th\u1ee9c c\u1ee7a <\/span><span style=\"font-weight: 400;\">Thread<\/span><span style=\"font-weight: 400;\"> nh\u01b0 <\/span><span style=\"font-weight: 400;\">start()<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n\n\n\n<p><b>V\u00ed d\u1ee5:<\/b><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">D\u00f9ng <\/span><span style=\"font-weight: 400;\">Runnable<\/span><span style=\"font-weight: 400;\"> khi mu\u1ed1n th\u1ef1c hi\u1ec7n nhi\u1ec1u t\u00e1c v\u1ee5 m\u00e0 kh\u00f4ng c\u1ea7n k\u1ebf th\u1eeba <\/span><span style=\"font-weight: 400;\">Thread<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">D\u00f9ng <\/span><span style=\"font-weight: 400;\">Thread<\/span><span style=\"font-weight: 400;\"> khi c\u1ea7n th\u00eam c\u00e1c ch\u1ee9c n\u0103ng \u0111\u1eb7c bi\u1ec7t c\u1ee7a l\u1edbp <\/span><span style=\"font-weight: 400;\">Thread<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-synchronized-keyword-co-tac-d\u1ee5ng-gi\"><strong>synchronized keyword c\u00f3 t\u00e1c d\u1ee5ng g\u00ec?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">synchronized<\/span><span style=\"font-weight: 400;\"> trong Java \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o t\u00ednh \u0111\u1ed3ng th\u1eddi (thread safety) khi nhi\u1ec1u thread c\u00f9ng truy c\u1eadp v\u00e0o m\u1ed9t t\u00e0i nguy\u00ean ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Khi m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c ho\u1eb7c kh\u1ed1i m\u00e3 \u0111\u01b0\u1ee3c \u0111\u00e1nh d\u1ea5u b\u1eb1ng <\/span><span style=\"font-weight: 400;\">synchronized<\/span><span style=\"font-weight: 400;\">, ch\u1ec9 c\u00f3 m\u1ed9t thread c\u00f3 th\u1ec3 th\u1ef1c thi n\u00f3 t\u1ea1i m\u1ed9t th\u1eddi \u0111i\u1ec3m. \u0110i\u1ec1u n\u00e0y gi\u00fap ng\u0103n ch\u1eb7n c\u00e1c l\u1ed7i khi nhi\u1ec1u thread truy c\u1eadp v\u00e0 thay \u0111\u1ed5i d\u1eef li\u1ec7u c\u00f9ng l\u00fac, l\u00e0m cho d\u1eef li\u1ec7u tr\u1edf n\u00ean kh\u00f4ng \u0111\u1ed3ng nh\u1ea5t.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-phan-bi\u1ec7t-gi\u1eefa-wait-va-sleep-trong-multithreading\"><strong>Ph\u00e2n bi\u1ec7t gi\u1eefa wait() v\u00e0 sleep() trong multithreading?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">wait()<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">sleep()<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec1u li\u00ean quan \u0111\u1ebfn vi\u1ec7c t\u1ea1m d\u1eebng th\u1ef1c thi c\u1ee7a m\u1ed9t lu\u1ed3ng, nh\u01b0ng ch\u00fang c\u00f3 m\u1ee5c \u0111\u00edch v\u00e0 c\u00e1ch s\u1eed d\u1ee5ng kh\u00e1c nhau:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">wait()<\/span><span style=\"font-weight: 400;\">: \u0110\u01b0\u1ee3c g\u1ecdi tr\u00ean m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng \u0111\u1ed3ng b\u1ed9, y\u00eau c\u1ea7u lu\u1ed3ng hi\u1ec7n t\u1ea1i tr\u1ea3 l\u1ea1i kh\u00f3a c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 chuy\u1ec3n v\u00e0o tr\u1ea1ng th\u00e1i ch\u1edd (waiting) cho \u0111\u1ebfn khi c\u00f3 th\u00f4ng b\u00e1o (notify) ho\u1eb7c th\u00f4ng b\u00e1o t\u1ea5t c\u1ea3 (notifyAll). Ph\u01b0\u01a1ng th\u1ee9c n\u00e0y c\u1ea7n ph\u1ea3i \u0111\u01b0\u1ee3c g\u1ecdi trong m\u1ed9t kh\u1ed1i synchronized.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">sleep()<\/span><span style=\"font-weight: 400;\">: D\u1eebng t\u1ea1m th\u1eddi m\u1ed9t lu\u1ed3ng trong m\u1ed9t kho\u1ea3ng th\u1eddi gian nh\u1ea5t \u0111\u1ecbnh m\u00e0 kh\u00f4ng li\u00ean quan \u0111\u1ebfn \u0111\u1ed1i t\u01b0\u1ee3ng \u0111\u1ed3ng b\u1ed9. Ph\u01b0\u01a1ng th\u1ee9c n\u00e0y kh\u00f4ng y\u00eau c\u1ea7u ph\u1ea3i g\u1ecdi trong kh\u1ed1i synchronized v\u00e0 kh\u00f4ng gi\u1ea3i ph\u00f3ng b\u1ea5t k\u1ef3 kh\u00f3a n\u00e0o.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-executor-framework-trong-java-la-gi-khi-nao-nen-s\u1eed-d\u1ee5ng\"><strong>Executor Framework trong Java l\u00e0 g\u00ec? Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng?<\/strong><\/h3>\n\n\n\n<p><b>Executor Framework trong Java<\/b><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c interface v\u00e0 l\u1edbp h\u1ed7 tr\u1ee3 qu\u1ea3n l\u00fd v\u00e0 th\u1ef1c thi c\u00e1c t\u00e1c v\u1ee5 b\u1ea5t \u0111\u1ed3ng b\u1ed9 (asynchronous tasks) trong m\u00f4i tr\u01b0\u1eddng \u0111a lu\u1ed3ng. N\u00f3 bao g\u1ed3m c\u00e1c th\u00e0nh ph\u1ea7n ch\u00ednh nh\u01b0 <\/span><span style=\"font-weight: 400;\">Executor<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">ExecutorService<\/span><span style=\"font-weight: 400;\">, v\u00e0 <\/span><span style=\"font-weight: 400;\">ScheduledExecutorService<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Thay v\u00ec t\u1ea1o v\u00e0 qu\u1ea3n l\u00fd c\u00e1c lu\u1ed3ng (threads) tr\u1ef1c ti\u1ebfp, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng Executor Framework \u0111\u1ec3 qu\u1ea3n l\u00fd c\u00e1c t\u00e1c v\u1ee5, gi\u00fap d\u1ec5 d\u00e0ng ki\u1ec3m so\u00e1t s\u1ed1 l\u01b0\u1ee3ng lu\u1ed3ng, x\u1eed l\u00fd l\u1ed7i, v\u00e0 qu\u1ea3n l\u00fd t\u00e0i nguy\u00ean.<\/span><\/p>\n\n\n\n<p>N\u00ean s\u1eed d\u1ee5ng Executor Framework khi:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Qu\u1ea3n l\u00fd nhi\u1ec1u t\u00e1c v\u1ee5 \u0111\u1ed3ng th\u1eddi:<\/b><span style=\"font-weight: 400;\"> Khi c\u1ea7n th\u1ef1c thi nhi\u1ec1u t\u00e1c v\u1ee5 trong c\u00e1c lu\u1ed3ng ri\u00eang bi\u1ec7t m\u00e0 kh\u00f4ng ph\u1ea3i qu\u1ea3n l\u00fd t\u1eebng lu\u1ed3ng m\u1ed9t c\u00e1ch th\u1ee7 c\u00f4ng.<\/span><\/li>\n\n\n\n<li><b>T\u0103ng hi\u1ec7u su\u1ea5t:<\/b><span style=\"font-weight: 400;\"> Khi \u1ee9ng d\u1ee5ng c\u1ea7n th\u1ef1c thi nhi\u1ec1u c\u00f4ng vi\u1ec7c song song m\u00e0 kh\u00f4ng ph\u1ea3i lo l\u1eafng v\u1ec1 vi\u1ec7c t\u1ea1o v\u00e0 qu\u1ea3n l\u00fd c\u00e1c lu\u1ed3ng ri\u00eang l\u1ebb.<\/span><\/li>\n\n\n\n<li><b>D\u1ec5 d\u00e0ng m\u1edf r\u1ed9ng:<\/b><span style=\"font-weight: 400;\"> Executor Framework h\u1ed7 tr\u1ee3 vi\u1ec7c \u0111i\u1ec1u ch\u1ec9nh s\u1ed1 l\u01b0\u1ee3ng lu\u1ed3ng theo nhu c\u1ea7u c\u1ee7a \u1ee9ng d\u1ee5ng m\u00e0 kh\u00f4ng l\u00e0m t\u0103ng \u0111\u1ed9 ph\u1ee9c t\u1ea1p trong m\u00e3 ngu\u1ed3n.<\/span><\/li>\n\n\n\n<li><b>Qu\u1ea3n l\u00fd t\u00e0i nguy\u00ean hi\u1ec7u qu\u1ea3:<\/b><span style=\"font-weight: 400;\"> Thay v\u00ec t\u1ea1o ra qu\u00e1 nhi\u1ec1u lu\u1ed3ng m\u00e0 kh\u00f4ng ki\u1ec3m so\u00e1t, Executor Framework cho ph\u00e9p qu\u1ea3n l\u00fd c\u00e1c lu\u1ed3ng qua c\u00e1c pool, gi\u00fap h\u1ea1n ch\u1ebf vi\u1ec7c s\u1eed d\u1ee5ng t\u00e0i nguy\u00ean qu\u00e1 m\u1ee9c.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-deadlock-la-gi-d\u01b0a-ra-vi-d\u1ee5-va-gi\u1ea3i-phap-phong-tranh\"><strong>Deadlock l\u00e0 g\u00ec? \u0110\u01b0a ra v\u00ed d\u1ee5 v\u00e0 gi\u1ea3i ph\u00e1p ph\u00f2ng tr\u00e1nh?<\/strong><\/h3>\n\n\n\n<p><b>Deadlock<\/b><span style=\"font-weight: 400;\"> l\u00e0 t\u00ecnh hu\u1ed1ng trong l\u1eadp tr\u00ecnh \u0111a lu\u1ed3ng khi hai hay nhi\u1ec1u lu\u1ed3ng (threads) ch\u1edd nhau \u0111\u1ec3 gi\u1ea3i ph\u00f3ng t\u00e0i nguy\u00ean m\u00e0 ch\u00fang c\u1ea7n, d\u1eabn \u0111\u1ebfn t\u00ecnh tr\u1ea1ng c\u00e1c lu\u1ed3ng n\u00e0y kh\u00f4ng th\u1ec3 ti\u1ebfn h\u00e0nh c\u00f4ng vi\u1ec7c ti\u1ebfp theo. \u0110i\u1ec1u n\u00e0y th\u01b0\u1eddng x\u1ea3y ra khi m\u1ed7i lu\u1ed3ng gi\u1eef m\u1ed9t t\u00e0i nguy\u00ean v\u00e0 ch\u1edd t\u00e0i nguy\u00ean c\u00f2n l\u1ea1i m\u00e0 m\u1ed9t lu\u1ed3ng kh\u00e1c \u0111ang n\u1eafm gi\u1eef.<\/span><\/p>\n\n\n\n<p><b>V\u00ed d\u1ee5 v\u1ec1 Deadlock:<\/b><span style=\"font-weight: 400;\"> Gi\u1ea3 s\u1eed c\u00f3 hai lu\u1ed3ng, <\/span><b>Thread1<\/b><span style=\"font-weight: 400;\"> v\u00e0 <\/span><b>Thread2<\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><b>Thread1<\/b><span style=\"font-weight: 400;\"> gi\u1eef t\u00e0i nguy\u00ean <\/span><b>A<\/b><span style=\"font-weight: 400;\"> v\u00e0 c\u1ea7n t\u00e0i nguy\u00ean <\/span><b>B<\/b><span style=\"font-weight: 400;\"> \u0111\u1ec3 ti\u1ebfp t\u1ee5c.<\/span><\/li>\n\n\n\n<li><b>Thread2<\/b><span style=\"font-weight: 400;\"> gi\u1eef t\u00e0i nguy\u00ean <\/span><b>B<\/b><span style=\"font-weight: 400;\"> v\u00e0 c\u1ea7n t\u00e0i nguy\u00ean <\/span><b>A<\/b><span style=\"font-weight: 400;\"> \u0111\u1ec3 ti\u1ebfp t\u1ee5c.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 l\u00e0 c\u1ea3 hai lu\u1ed3ng s\u1ebd ch\u1edd nhau v\u00f4 h\u1ea1n, kh\u00f4ng th\u1ec3 ti\u1ebfp t\u1ee5c c\u00f4ng vi\u1ec7c, g\u00e2y ra deadlock.<\/span><\/p>\n\n\n\n<p><b>Gi\u1ea3i ph\u00e1p ph\u00f2ng tr\u00e1nh:<\/b><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><b>Tr\u00e1nh vi\u1ec7c gi\u1eef t\u00e0i nguy\u00ean l\u00e2u d\u00e0i:<\/b><span style=\"font-weight: 400;\"> H\u1ea1n ch\u1ebf vi\u1ec7c gi\u1eef t\u00e0i nguy\u00ean trong th\u1eddi gian d\u00e0i, v\u00e0 thay v\u00e0o \u0111\u00f3, ch\u1ec9 gi\u1eef t\u00e0i nguy\u00ean khi th\u1ef1c s\u1ef1 c\u1ea7n thi\u1ebft.<\/span><\/li>\n\n\n\n<li><b>S\u1eafp x\u1ebfp th\u1ee9 t\u1ef1 truy c\u1eadp t\u00e0i nguy\u00ean:<\/b><span style=\"font-weight: 400;\"> M\u1ed9t c\u00e1ch \u0111\u01a1n gi\u1ea3n l\u00e0 quy \u0111\u1ecbnh th\u1ee9 t\u1ef1 m\u00e0 c\u00e1c lu\u1ed3ng ph\u1ea3i truy c\u1eadp t\u00e0i nguy\u00ean. V\u00ed d\u1ee5, t\u1ea5t c\u1ea3 c\u00e1c lu\u1ed3ng ph\u1ea3i truy c\u1eadp t\u00e0i nguy\u00ean <\/span><b>A<\/b><span style=\"font-weight: 400;\"> tr\u01b0\u1edbc, sau \u0111\u00f3 m\u1edbi \u0111\u1ebfn t\u00e0i nguy\u00ean <\/span><b>B<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n\n\n\n<li><b>S\u1eed d\u1ee5ng th\u1eddi gian ch\u1edd (Timeout):<\/b><span style=\"font-weight: 400;\"> Khi m\u1ed9t lu\u1ed3ng kh\u00f4ng th\u1ec3 c\u00f3 \u0111\u01b0\u1ee3c t\u00e0i nguy\u00ean trong m\u1ed9t kho\u1ea3ng th\u1eddi gian nh\u1ea5t \u0111\u1ecbnh, c\u00f3 th\u1ec3 h\u1ee7y b\u1ecf thao t\u00e1c ho\u1eb7c th\u1eed l\u1ea1i sau m\u1ed9t th\u1eddi gian.<\/span><\/li>\n\n\n\n<li><b>S\u1eed d\u1ee5ng c\u00f4ng c\u1ee5 ki\u1ec3m tra Deadlock:<\/b><span style=\"font-weight: 400;\"> S\u1eed d\u1ee5ng c\u00e1c c\u00f4ng c\u1ee5 nh\u01b0 <\/span><span style=\"font-weight: 400;\">jstack<\/span><span style=\"font-weight: 400;\"> trong Java \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh c\u00e1c deadlock trong h\u1ec7 th\u1ed1ng v\u00e0 ng\u1eebng ch\u00fang.<\/span><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cac-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-java-lien-quan-d\u1ebfn-java-stream-api\"><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Java_lien_quan_den_Java_Stream_API\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java li\u00ean quan \u0111\u1ebfn Java Stream API<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-stream-api-la-gi-tinh-nang-n\u1ed5i-b\u1eadt-c\u1ee7a-no-trong-java-8\"><strong>Stream API l\u00e0 g\u00ec? T\u00ednh n\u0103ng n\u1ed5i b\u1eadt c\u1ee7a n\u00f3 trong Java 8?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Stream API trong Java 8 l\u00e0 m\u1ed9t t\u00ednh n\u0103ng m\u1edbi gi\u00fap x\u1eed l\u00fd d\u1eef li\u1ec7u theo ki\u1ec3u &#8220;functional&#8221; (ch\u1ee9c n\u0103ng), cho ph\u00e9p th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c nh\u01b0 l\u1ecdc, \u00e1nh x\u1ea1, gi\u1ea3m (reduce), s\u1eafp x\u1ebfp, v\u00e0 c\u00e1c ph\u00e9p to\u00e1n kh\u00e1c tr\u00ean c\u00e1c collection (danh s\u00e1ch, t\u1eadp h\u1ee3p, v.v.) m\u1ed9t c\u00e1ch d\u1ec5 d\u00e0ng v\u00e0 hi\u1ec7u qu\u1ea3.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">T\u00ednh n\u0103ng n\u1ed5i b\u1eadt c\u1ee7a Stream API:<\/span><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><b>X\u1eed l\u00fd song song<\/b><span style=\"font-weight: 400;\">: Stream API h\u1ed7 tr\u1ee3 x\u1eed l\u00fd d\u1eef li\u1ec7u song song (parallel processing) m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i quan t\u00e2m \u0111\u1ebfn c\u00e1c v\u1ea5n \u0111\u1ec1 \u0111\u1ed3ng b\u1ed9 h\u00f3a, gi\u00fap t\u0103ng hi\u1ec7u su\u1ea5t trong c\u00e1c t\u00e1c v\u1ee5 x\u1eed l\u00fd d\u1eef li\u1ec7u l\u1edbn.<\/span><\/li>\n\n\n\n<li><b>Chaining operations<\/b><span style=\"font-weight: 400;\">: C\u00e1c thao t\u00e1c tr\u00ean Stream c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c n\u1ed1i ti\u1ebfp nhau m\u1ed9t c\u00e1ch d\u1ec5 d\u00e0ng, gi\u00fap vi\u1ebft m\u00e3 ng\u1eafn g\u1ecdn v\u00e0 d\u1ec5 hi\u1ec3u.<\/span><\/li>\n\n\n\n<li><b>Lazy evaluation<\/b><span style=\"font-weight: 400;\">: C\u00e1c thao t\u00e1c tr\u00ean Stream kh\u00f4ng \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n ngay l\u1eadp t\u1ee9c m\u00e0 ch\u1ec9 khi c\u1ea7n thi\u1ebft, gi\u00fap t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t, gi\u1ea3m thi\u1ec3u vi\u1ec7c t\u00ednh to\u00e1n kh\u00f4ng c\u1ea7n thi\u1ebft.<\/span><\/li>\n\n\n\n<li><b>Thao t\u00e1c theo ki\u1ec3u functional<\/b><span style=\"font-weight: 400;\">: Stream API h\u1ed7 tr\u1ee3 c\u00e1c ph\u00e9p to\u00e1n h\u00e0m b\u1eadc cao nh\u01b0 <\/span><span style=\"font-weight: 400;\">map<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">filter<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">reduce<\/span><span style=\"font-weight: 400;\">, gi\u00fap x\u1eed l\u00fd d\u1eef li\u1ec7u theo ki\u1ec3u h\u00e0m, d\u1ec5 \u0111\u1ecdc v\u00e0 b\u1ea3o tr\u00ec m\u00e3.<\/span><\/li>\n<\/ol>\n\n\n\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 s\u1eed d\u1ee5ng Stream API:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;String&gt; names = Arrays.asList(\"John\", \"Jane\", \"Adam\", \"Chris\");\nlong count = names.stream()\n                  .filter(name -&gt; name.startsWith(\"J\"))\n                  .count();\nSystem.out.println(count); \/\/ K\u1ebft qu\u1ea3: 2<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Stream API gi\u00fap gi\u1ea3m thi\u1ec3u m\u00e3 l\u1eb7p l\u1ea1i v\u00e0 l\u00e0m m\u00e3 d\u1ec5 hi\u1ec3u h\u01a1n, \u0111\u1ed3ng th\u1eddi c\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t x\u1eed l\u00fd d\u1eef li\u1ec7u trong c\u00e1c t\u00ecnh hu\u1ed1ng c\u1ea7n x\u1eed l\u00fd l\u01b0\u1ee3ng d\u1eef li\u1ec7u l\u1edbn.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-phan-bi\u1ec7t-map-va-flatmap-trong-stream-api\"><strong>Ph\u00e2n bi\u1ec7t map v\u00e0 flatMap trong Stream API?<\/strong><\/h3>\n\n\n\n<p><b>map()<\/b><span style=\"font-weight: 400;\">: L\u00e0 ph\u01b0\u01a1ng th\u1ee9c d\u00f9ng \u0111\u1ec3 bi\u1ebfn \u0111\u1ed5i (transform) t\u1eebng ph\u1ea7n t\u1eed c\u1ee7a m\u1ed9t <\/span><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\">. N\u00f3 nh\u1eadn m\u1ed9t <\/span><span style=\"font-weight: 400;\">Function&lt;T, R&gt;<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 bi\u1ebfn \u0111\u1ed5i m\u1ed7i ph\u1ea7n t\u1eed <\/span><span style=\"font-weight: 400;\">T<\/span><span style=\"font-weight: 400;\"> c\u1ee7a <\/span><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\"> th\u00e0nh <\/span><span style=\"font-weight: 400;\">R<\/span><span style=\"font-weight: 400;\">. K\u1ebft qu\u1ea3 s\u1ebd l\u00e0 m\u1ed9t <\/span><span style=\"font-weight: 400;\">Stream&lt;R&gt;<\/span><span style=\"font-weight: 400;\"> ch\u1ee9a c\u00e1c ph\u1ea7n t\u1eed \u0111\u00e3 \u0111\u01b0\u1ee3c bi\u1ebfn \u0111\u1ed5i.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;String&gt; names = Arrays.asList(\"John\", \"Jane\", \"Jack\");\nList&lt;Integer&gt; lengths = names.stream()\n                             .map(String:: length)\n                             .collect(Collectors.toList());\n\/\/ lengths: &#91;4, 4, 4]<\/code><\/pre>\n\n\n\n<p><b>flatMap()<\/b><span style=\"font-weight: 400;\">: L\u00e0 ph\u01b0\u01a1ng th\u1ee9c d\u00f9ng \u0111\u1ec3 &#8220;l\u00e0m ph\u1eb3ng&#8221; c\u00e1c <\/span><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\"> ch\u1ee9a c\u00e1c c\u1ea5u tr\u00fac d\u1eef li\u1ec7u l\u1ed3ng nhau. N\u00f3 nh\u1eadn m\u1ed9t <\/span><span style=\"font-weight: 400;\">Function&lt;T, Stream&lt;R&gt;&gt;<\/span><span style=\"font-weight: 400;\"> v\u00e0 k\u1ebft qu\u1ea3 s\u1ebd l\u00e0 m\u1ed9t <\/span><span style=\"font-weight: 400;\">Stream&lt;R&gt;<\/span><span style=\"font-weight: 400;\">. N\u00f3i c\u00e1ch kh\u00e1c, <\/span><span style=\"font-weight: 400;\">flatMap()<\/span><span style=\"font-weight: 400;\"> gi\u00fap h\u1ee3p nh\u1ea5t nhi\u1ec1u <\/span><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\"> con th\u00e0nh m\u1ed9t <\/span><span style=\"font-weight: 400;\">Stream<\/span><span style=\"font-weight: 400;\"> duy nh\u1ea5t.<\/span><span style=\"font-weight: 400;\"><br><\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;List&lt;String&gt;&gt; namesList = Arrays.asList(\n  Arrays.asList(\"John\", \"Jane\"),\n  Arrays.asList(\"Jack\", \"Jill\")\n);\n\nList&lt;String&gt; allNames = namesList.stream()\n                                 .flatMap(List::stream)\n                                 .collect (Collectors.toList());\n\/\/ allNames: &#91;\"John\", \"Jane\", \"Jack\", \"Jill\"]<\/code><\/pre>\n\n\n\n<p><b>Ph\u00e2n bi\u1ec7t Map v\u00e0 FlatMap:<\/b><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><b>Ti\u00eau ch\u00ed&nbsp;&nbsp;<\/b><\/td><td><b>Map<\/b><\/td><td><b>FlatMap<\/b><\/td><\/tr><tr><td><b>M\u1ee5c \u0111\u00edch<\/b><\/td><td><span style=\"font-weight: 400;\">\u00c1p d\u1ee5ng m\u1ed9t h\u00e0m chuy\u1ec3n \u0111\u1ed5i v\u00e0o t\u1eebng ph\u1ea7n t\u1eed trong Stream.<\/span><\/td><td><span style=\"font-weight: 400;\">\u00c1p d\u1ee5ng m\u1ed9t h\u00e0m chuy\u1ec3n \u0111\u1ed5i v\u00e0 &#8220;ph\u1eb3ng h\u00f3a&#8221; k\u1ebft qu\u1ea3 v\u1ec1 m\u1ed9t Stream m\u1edbi.<\/span><\/td><\/tr><tr><td><b>Ki\u1ec3u tr\u1ea3 v\u1ec1<\/b><\/td><td><span style=\"font-weight: 400;\">Tr\u1ea3 v\u1ec1 m\u1ed9t Stream ch\u1ee9a c\u00e1c ph\u1ea7n t\u1eed \u0111\u00e3 \u0111\u01b0\u1ee3c chuy\u1ec3n \u0111\u1ed5i (v\u1eabn l\u00e0 m\u1ed9t Stream 1 chi\u1ec1u).<\/span><\/td><td><span style=\"font-weight: 400;\">Tr\u1ea3 v\u1ec1 m\u1ed9t Stream c\u00f3 th\u1ec3 ch\u1ee9a nhi\u1ec1u ph\u1ea7n t\u1eed t\u1eeb m\u1ed9t Stream kh\u00e1c, l\u00e0m gi\u1ea3m c\u1ea5p \u0111\u1ed9 c\u1ee7a Stream.<\/span><\/td><\/tr><tr><td><b>\u0110\u1ea7u v\u00e0o h\u00e0m<\/b><\/td><td><span style=\"font-weight: 400;\">H\u00e0m nh\u1eadn m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 tr\u1ea3 v\u1ec1 m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/td><td><span style=\"font-weight: 400;\">H\u00e0m nh\u1eadn m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 tr\u1ea3 v\u1ec1 m\u1ed9t Stream (c\u00f3 th\u1ec3 c\u00f3 nhi\u1ec1u ph\u1ea7n t\u1eed).<\/span><\/td><\/tr><tr><td><b>\u1ee8ng d\u1ee5ng<\/b><\/td><td><span style=\"font-weight: 400;\">Th\u01b0\u1eddng d\u00f9ng khi c\u1ea7n chuy\u1ec3n \u0111\u1ed5i t\u1eebng ph\u1ea7n t\u1eed th\u00e0nh m\u1ed9t gi\u00e1 tr\u1ecb kh\u00e1c.<\/span><\/td><td><span style=\"font-weight: 400;\">Th\u01b0\u1eddng d\u00f9ng khi k\u1ebft qu\u1ea3 c\u1ee7a h\u00e0m chuy\u1ec3n \u0111\u1ed5i l\u00e0 m\u1ed9t Stream, v\u00e0 b\u1ea1n mu\u1ed1n h\u1ee3p nh\u1ea5t t\u1ea5t c\u1ea3 c\u00e1c Stream con th\u00e0nh m\u1ed9t Stream duy nh\u1ea5t.<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lazy-evaluation-trong-stream-api-la-gi\"><strong>Lazy evaluation trong Stream API l\u00e0 g\u00ec?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Lazy evaluation trong Stream API l\u00e0 k\u1ef9 thu\u1eadt x\u1eed l\u00fd d\u1eef li\u1ec7u m\u00e0 c\u00e1c ph\u1ea7n t\u1eed trong stream ch\u1ec9 \u0111\u01b0\u1ee3c x\u1eed l\u00fd khi ch\u00fang th\u1ef1c s\u1ef1 c\u1ea7n thi\u1ebft.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110i\u1ec1u n\u00e0y c\u00f3 ngh\u0129a l\u00e0 c\u00e1c ph\u00e9p to\u00e1n tr\u00ean stream, nh\u01b0 map, filter, hay flatMap, kh\u00f4ng th\u1ef1c hi\u1ec7n ngay khi ch\u00fang \u0111\u01b0\u1ee3c g\u1ecdi, m\u00e0 s\u1ebd ch\u1ec9 \u0111\u01b0\u1ee3c th\u1ef1c thi khi stream \u0111\u01b0\u1ee3c &#8220;consume&#8221;, v\u00ed d\u1ee5 khi g\u1ecdi ph\u01b0\u01a1ng th\u1ee9c nh\u01b0 <code>collect()<\/code>, <code>forEach()<\/code>, ho\u1eb7c <code>reduce()<\/code>.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110i\u1ec1u n\u00e0y gi\u00fap t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t v\u00ec c\u00e1c ph\u00e9p to\u00e1n ch\u1ec9 \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n khi c\u1ea7n thi\u1ebft v\u00e0 c\u00f3 th\u1ec3 d\u1eebng l\u1ea1i s\u1edbm n\u1ebfu k\u1ebft qu\u1ea3 \u0111\u00e3 \u0111\u01b0\u1ee3c t\u00ecm th\u1ea5y.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-khai-ni\u1ec7m-parallelstream-la-gi-khi-nao-nen-s\u1eed-d\u1ee5ng-parallelstream\"><strong>Kh\u00e1i ni\u1ec7m parallelStream l\u00e0 g\u00ec? Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng parallelStream?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">parallelStream<\/span><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c trong Java \u0111\u01b0\u1ee3c cung c\u1ea5p b\u1edfi API Stream (t\u1eeb Java 8 tr\u1edf \u0111i) \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c ph\u1ea7n t\u1eed c\u1ee7a m\u1ed9t Stream theo c\u00e1ch song song. Khi b\u1ea1n g\u1ecdi <\/span><span style=\"font-weight: 400;\">parallelStream<\/span><span style=\"font-weight: 400;\"> tr\u00ean m\u1ed9t Collection, n\u00f3 s\u1ebd t\u1ef1 \u0111\u1ed9ng chia nh\u1ecf c\u00e1c ph\u1ea7n t\u1eed v\u00e0 x\u1eed l\u00fd ch\u00fang tr\u00ean nhi\u1ec1u l\u00f5i (cores) c\u1ee7a CPU, gi\u00fap t\u0103ng t\u1ed1c \u0111\u1ed9 x\u1eed l\u00fd khi l\u00e0m vi\u1ec7c v\u1edbi l\u01b0\u1ee3ng d\u1eef li\u1ec7u l\u1edbn.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">B\u1ea1n n\u00ean s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">parallelStream<\/span><span style=\"font-weight: 400;\"> trong c\u00e1c t\u00ecnh hu\u1ed1ng khi:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">D\u1eef li\u1ec7u c\u1ea7n x\u1eed l\u00fd l\u00e0 l\u1edbn v\u00e0 kh\u00f4ng c\u00f3 s\u1ef1 ph\u1ee5 thu\u1ed9c gi\u1eefa c\u00e1c ph\u1ea7n t\u1eed, t\u1ee9c l\u00e0 c\u00e1c ph\u1ea7n t\u1eed c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c x\u1eed l\u00fd \u0111\u1ed9c l\u1eadp.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00e1c t\u00e1c v\u1ee5 trong Stream (nh\u01b0 <\/span><span style=\"font-weight: 400;\">map<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">filter<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">reduce<\/span><span style=\"font-weight: 400;\">,&#8230;) c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n \u0111\u1ed3ng th\u1eddi m\u00e0 kh\u00f4ng g\u1eb7p ph\u1ea3i v\u1ea5n \u0111\u1ec1 v\u1ec1 \u0111\u1ed3ng b\u1ed9 h\u00f3a.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">H\u1ec7 th\u1ed1ng c\u1ee7a b\u1ea1n c\u00f3 nhi\u1ec1u l\u00f5i (CPU cores), gi\u00fap t\u1eadn d\u1ee5ng t\u1ed1i \u0111a kh\u1ea3 n\u0103ng x\u1eed l\u00fd song song.<\/span><\/li>\n<\/ul>\n\n\n\n<p><span style=\"font-weight: 400;\">Tuy nhi\u00ean, c\u1ea7n tr\u00e1nh s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">parallelStream<\/span><span style=\"font-weight: 400;\"> khi:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">D\u1eef li\u1ec7u c\u00f3 k\u00edch th\u01b0\u1edbc nh\u1ecf, v\u00ec vi\u1ec7c chia nh\u1ecf c\u00f4ng vi\u1ec7c c\u00f3 th\u1ec3 g\u00e2y overhead (chi ph\u00ed cho vi\u1ec7c ph\u00e2n chia v\u00e0 g\u1ed9p k\u1ebft qu\u1ea3).<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">C\u00e1c t\u00e1c v\u1ee5 trong Stream c\u00f3 s\u1ef1 ph\u1ee5 thu\u1ed9c l\u1eabn nhau ho\u1eb7c y\u00eau c\u1ea7u \u0111\u1ed3ng b\u1ed9 h\u00f3a, \u0111i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 l\u00e0m gi\u1ea3m hi\u1ec7u su\u1ea5t khi x\u1eed l\u00fd song song.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-phan-bi\u1ec7t-findany-va-findfirst-trong-java-stream\"><strong>Ph\u00e2n bi\u1ec7t findAny() v\u00e0 findFirst() trong Java Stream<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u1ea3 <\/span><span style=\"font-weight: 400;\">findAny()<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">findFirst()<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec1u \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 t\u00ecm m\u1ed9t ph\u1ea7n t\u1eed trong Stream, nh\u01b0ng ch\u00fang c\u00f3 nh\u1eefng \u0111i\u1ec3m kh\u00e1c bi\u1ec7t v\u1ec1 c\u00e1ch th\u1ee9c ho\u1ea1t \u0111\u1ed9ng v\u00e0 m\u1ee5c \u0111\u00edch s\u1eed d\u1ee5ng.<\/span><\/p>\n\n\n\n<p><b>Gi\u1ea3i th\u00edch<\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">findFirst()<\/span><span style=\"font-weight: 400;\">: Tr\u1ea3 v\u1ec1 ph\u1ea7n t\u1eed \u0111\u1ea7u ti\u00ean trong Stream theo th\u1ee9 t\u1ef1 m\u00e0 c\u00e1c ph\u1ea7n t\u1eed xu\u1ea5t hi\u1ec7n (n\u1ebfu c\u00f3), ho\u1eb7c <\/span><span style=\"font-weight: 400;\">Optional.empty()<\/span><span style=\"font-weight: 400;\"> n\u1ebfu Stream tr\u1ed1ng. <\/span><span style=\"font-weight: 400;\">findFirst()<\/span><span style=\"font-weight: 400;\"> \u0111\u1ea3m b\u1ea3o r\u1eb1ng ph\u1ea7n t\u1eed \u0111\u01b0\u1ee3c tr\u1ea3 v\u1ec1 l\u00e0 ph\u1ea7n t\u1eed \u0111\u1ea7u ti\u00ean trong Stream.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">findAny()<\/span><span style=\"font-weight: 400;\">: Tr\u1ea3 v\u1ec1 m\u1ed9t ph\u1ea7n t\u1eed b\u1ea5t k\u1ef3 t\u1eeb Stream, c\u00f3 th\u1ec3 l\u00e0 b\u1ea5t k\u1ef3 ph\u1ea7n t\u1eed n\u00e0o trong Stream, t\u00f9y thu\u1ed9c v\u00e0o c\u00e1ch Stream \u0111\u01b0\u1ee3c x\u1eed l\u00fd. Trong c\u00e1c Stream song song, <\/span><span style=\"font-weight: 400;\">findAny()<\/span><span style=\"font-weight: 400;\"> c\u00f3 th\u1ec3 tr\u1ea3 v\u1ec1 ph\u1ea7n t\u1eed kh\u00f4ng ph\u1ea3i l\u00e0 ph\u1ea7n t\u1eed \u0111\u1ea7u ti\u00ean.<\/span><\/li>\n<\/ul>\n\n\n\n<p><b>S\u1ef1 kh\u00e1c bi\u1ec7t ch\u00ednh<\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">findFirst()<\/span><span style=\"font-weight: 400;\"> lu\u00f4n \u0111\u1ea3m b\u1ea3o tr\u1ea3 v\u1ec1 ph\u1ea7n t\u1eed theo th\u1ee9 t\u1ef1 xu\u1ea5t hi\u1ec7n trong Stream.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">findAny()<\/span><span style=\"font-weight: 400;\"> c\u00f3 th\u1ec3 tr\u1ea3 v\u1ec1 b\u1ea5t k\u1ef3 ph\u1ea7n t\u1eed n\u00e0o trong Stream v\u00e0 c\u00f3 th\u1ec3 mang l\u1ea1i hi\u1ec7u qu\u1ea3 t\u1ed1t h\u01a1n trong c\u00e1c Stream song song.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-filter-map-collect-co-vai-tro-gi-trong-stream-api\"><strong>filter, map, collect c\u00f3 vai tr\u00f2 g\u00ec trong Stream API?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong Stream API c\u1ee7a Java, c\u00e1c ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">filter<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">map<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">collect<\/span><span style=\"font-weight: 400;\"> \u0111\u00f3ng vai tr\u00f2 quan tr\u1ecdng trong vi\u1ec7c x\u1eed l\u00fd v\u00e0 chuy\u1ec3n \u0111\u1ed5i d\u1eef li\u1ec7u theo d\u00f2ng ch\u1ea3y (stream). C\u1ee5 th\u1ec3:<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-filter\"><b>filter<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 l\u1ecdc c\u00e1c ph\u1ea7n t\u1eed trong stream theo m\u1ed9t \u0111i\u1ec1u ki\u1ec7n nh\u1ea5t \u0111\u1ecbnh. N\u00f3 nh\u1eadn v\u00e0o m\u1ed9t <\/span><span style=\"font-weight: 400;\">Predicate<\/span><span style=\"font-weight: 400;\"> (h\u00e0m \u0111i\u1ec1u ki\u1ec7n) v\u00e0 tr\u1ea3 v\u1ec1 m\u1ed9t stream m\u1edbi ch\u1ec9 ch\u1ee9a c\u00e1c ph\u1ea7n t\u1eed th\u1ecfa m\u00e3n \u0111i\u1ec1u ki\u1ec7n \u0111\u00f3.<\/span><\/p>\n\n\n\n<p><b>V\u00ed d\u1ee5<\/b><span style=\"font-weight: 400;\">: L\u1ecdc c\u00e1c s\u1ed1 ch\u1eb5n t\u1eeb m\u1ed9t danh s\u00e1ch c\u00e1c s\u1ed1.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">list.stream().filter(n -&gt; n % 2 == 0).forEach(System.out::println);<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-map\"><b>map<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 bi\u1ebfn \u0111\u1ed5i m\u1ed7i ph\u1ea7n t\u1eed trong stream th\u00e0nh m\u1ed9t gi\u00e1 tr\u1ecb kh\u00e1c, th\u01b0\u1eddng l\u00e0 khi b\u1ea1n mu\u1ed1n chuy\u1ec3n \u0111\u1ed5i ki\u1ec3u d\u1eef li\u1ec7u. N\u00f3 nh\u1eadn v\u00e0o m\u1ed9t <\/span><span style=\"font-weight: 400;\">Function<\/span><span style=\"font-weight: 400;\"> v\u00e0 tr\u1ea3 v\u1ec1 m\u1ed9t stream m\u1edbi v\u1edbi c\u00e1c ph\u1ea7n t\u1eed \u0111\u00e3 \u0111\u01b0\u1ee3c bi\u1ebfn \u0111\u1ed5i.<\/span><\/p>\n\n\n\n<p><b>V\u00ed d\u1ee5<\/b><span style=\"font-weight: 400;\">: Chuy\u1ec3n \u0111\u1ed5i m\u1ed7i s\u1ed1 trong danh s\u00e1ch th\u00e0nh b\u00ecnh ph\u01b0\u01a1ng c\u1ee7a n\u00f3.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">list.stream().map(n -&gt; n * n).forEach(System.out::println);<\/span><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-collect\"><b>collect<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 thu th\u1eadp k\u1ebft qu\u1ea3 t\u1eeb m\u1ed9t stream v\u00e0 chuy\u1ec3n \u0111\u1ed5i n\u00f3 th\u00e0nh m\u1ed9t d\u1ea1ng d\u1eef li\u1ec7u kh\u00e1c nh\u01b0 List, Set, ho\u1eb7c Map. \u0110\u00e2y l\u00e0 m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c terminal, ngh\u0129a l\u00e0 khi g\u1ecdi <\/span><span style=\"font-weight: 400;\">collect<\/span><span style=\"font-weight: 400;\">, stream s\u1ebd b\u1ecb ti\u00eau th\u1ee5 v\u00e0 kh\u00f4ng th\u1ec3 s\u1eed d\u1ee5ng l\u1ea1i.<\/span><\/p>\n\n\n\n<p><b>V\u00ed d\u1ee5<\/b><span style=\"font-weight: 400;\">: Thu th\u1eadp c\u00e1c s\u1ed1 ch\u1eb5n v\u00e0o m\u1ed9t danh s\u00e1ch.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">List&lt;Integer&gt; evenNumbers = list.stream().filter(n -&gt; n % 2 == 0).collect(Collectors.toList());<\/span><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-cac-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-java-v\u1ec1-tinh-nang-n\u1ed5i-b\u1eadt-c\u1ee7a-java-8-m\u1edbi-nh\u1ea5t\"><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Java_ve_tinh_nang_noi_bat_cua_Java_8_moi_nhat\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java v\u1ec1 t\u00ednh n\u0103ng n\u1ed5i b\u1eadt c\u1ee7a Java 8 m\u1edbi nh\u1ea5t<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-cac-tinh-nang-m\u1edbi-trong-java-8-nh\u01b0-lambda-expressions-stream-api-va-functional-interfaces-la-gi\"><strong>C\u00e1c t\u00ednh n\u0103ng m\u1edbi trong Java 8 nh\u01b0 Lambda expressions, Stream API, v\u00e0 Functional Interfaces l\u00e0 g\u00ec?<\/strong><\/h3>\n\n\n\n<p><span style=\"font-weight: 400;\">Java 8 \u0111\u00e3 mang \u0111\u1ebfn nhi\u1ec1u t\u00ednh n\u0103ng m\u1edbi gi\u00fap n\u00e2ng cao hi\u1ec7u su\u1ea5t v\u00e0 kh\u1ea3 n\u0103ng l\u1eadp tr\u00ecnh theo phong c\u00e1ch h\u00e0m (functional programming).<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Ba t\u00ednh n\u0103ng quan tr\u1ecdng nh\u1ea5t l\u00e0:<\/span><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><b>Lambda expressions<\/b><span style=\"font-weight: 400;\">: L\u00e0 m\u1ed9t c\u00e1ch \u0111\u1ec3 vi\u1ebft c\u00e1c \u0111o\u1ea1n m\u00e3 ng\u1eafn g\u1ecdn v\u00e0 r\u00f5 r\u00e0ng h\u01a1n, thay th\u1ebf cho vi\u1ec7c s\u1eed d\u1ee5ng c\u00e1c l\u1edbp \u1ea9n danh (anonymous classes). Lambda gi\u00fap bi\u1ec3u di\u1ec5n h\u00e0nh vi d\u01b0\u1edbi d\u1ea1ng m\u1ed9t h\u00e0m, gi\u00fap vi\u1ec7c vi\u1ebft m\u00e3 tr\u1edf n\u00ean ng\u1eafn g\u1ecdn v\u00e0 d\u1ec5 hi\u1ec3u h\u01a1n. V\u00ed d\u1ee5: <\/span><span style=\"font-weight: 400;\">(a, b) -> a + b<\/span><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t lambda expression cho ph\u00e9p c\u1ed9ng hai s\u1ed1.<\/span><\/li>\n\n\n\n<li><b>Stream API<\/b><span style=\"font-weight: 400;\">: Cung c\u1ea5p c\u00e1ch ti\u1ebfp c\u1eadn m\u1edbi \u0111\u1ec3 x\u1eed l\u00fd d\u1eef li\u1ec7u t\u1eadp h\u1ee3p (collections) m\u1ed9t c\u00e1ch linh ho\u1ea1t v\u00e0 m\u1ea1nh m\u1ebd. Stream cho ph\u00e9p th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p to\u00e1n nh\u01b0 l\u1ecdc, bi\u1ebfn \u0111\u1ed5i, v\u00e0 t\u00ednh to\u00e1n tr\u00ean c\u00e1c t\u1eadp h\u1ee3p d\u1eef li\u1ec7u m\u00e0 kh\u00f4ng thay \u0111\u1ed5i c\u1ea5u tr\u00fac ban \u0111\u1ea7u c\u1ee7a ch\u00fang. C\u00e1c thao t\u00e1c n\u00e0y c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n theo ki\u1ec3u tu\u1ea7n t\u1ef1 ho\u1eb7c song song \u0111\u1ec3 t\u1ed1i \u01b0u hi\u1ec7u su\u1ea5t.<\/span><\/li>\n\n\n\n<li><b>Functional Interfaces<\/b><span style=\"font-weight: 400;\">: L\u00e0 c\u00e1c interface ch\u1ec9 c\u00f3 m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c tr\u1eebu t\u01b0\u1ee3ng. Ch\u00fang \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c lambda expressions v\u00e0 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy\u1ec1n l\u00e0m \u0111\u1ed1i s\u1ed1 cho c\u00e1c ph\u01b0\u01a1ng th\u1ee9c. V\u00ed d\u1ee5, <\/span><span style=\"font-weight: 400;\">Runnable<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">Callable<\/span><span style=\"font-weight: 400;\">, v\u00e0 <\/span><span style=\"font-weight: 400;\">Predicate<\/span><span style=\"font-weight: 400;\"> l\u00e0 c\u00e1c functional interfaces.<\/span><\/li>\n<\/ol>\n\n\n\n<p><span style=\"font-weight: 400;\">Nh\u1eefng t\u00ednh n\u0103ng n\u00e0y gi\u00fap c\u1ea3i thi\u1ec7n t\u00ednh hi\u1ec7u qu\u1ea3 v\u00e0 kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng c\u1ee7a m\u00e3 ngu\u1ed3n Java, \u0111\u1ed3ng th\u1eddi h\u1ed7 tr\u1ee3 l\u1eadp tr\u00ecnh vi\u00ean l\u1eadp tr\u00ecnh theo phong c\u00e1ch h\u00e0m.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">T\u00ecm hi\u1ec3u chi ti\u1ebft phi\u00ean b\u1ea3n Java 8 v\u1edbi nhi\u1ec1u t\u00ednh n\u0103ng m\u1edbi \u0111\u1ea7y \u0111\u1ee7: <\/span><a href=\"https:\/\/www.javatpoint.com\/java-8-features\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">https:\/\/www.javatpoint.com\/java-8-features<\/span><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-lambda-expression-la-gi-neu-vi-d\u1ee5-s\u1eed-d\u1ee5ng\"><strong>Lambda expression l\u00e0 g\u00ec? N\u00eau v\u00ed d\u1ee5 s\u1eed d\u1ee5ng.<\/strong><\/h3>\n\n\n\n<p><b>Lambda expression<\/b><span style=\"font-weight: 400;\"> trong Java<\/span><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t t\u00ednh n\u0103ng \u0111\u01b0\u1ee3c gi\u1edbi thi\u1ec7u t\u1eeb Java 8, cho ph\u00e9p b\u1ea1n truy\u1ec1n m\u1ed9t \u0111o\u1ea1n m\u00e3 (bi\u1ec3u th\u1ee9c h\u00e0m) m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i t\u1ea1o ra m\u1ed9t l\u1edbp \u1ea9n (anonymous class) \u0111\u1ec3 tri\u1ec3n khai m\u1ed9t interface c\u00f3 \u0111\u00fang m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c tr\u1eebu t\u01b0\u1ee3ng (functional interface). Lambda expression gi\u00fap gi\u1ea3m b\u1edbt s\u1ef1 ph\u1ee9c t\u1ea1p v\u00e0 l\u00e0m m\u00e3 ngu\u1ed3n ng\u1eafn g\u1ecdn, d\u1ec5 \u0111\u1ecdc.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">C\u1ea5u tr\u00fac c\u01a1 b\u1ea3n c\u1ee7a Lambda expression l\u00e0:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: 400;\">(parameters) -&gt; expression<\/span><\/code><\/pre>\n\n\n\n<p><b>V\u00ed d\u1ee5 s\u1eed d\u1ee5ng<\/b><span style=\"font-weight: 400;\">: Gi\u1ea3 s\u1eed b\u1ea1n c\u00f3 m\u1ed9t danh s\u00e1ch c\u00e1c s\u1ed1 nguy\u00ean v\u00e0 mu\u1ed1n in ra c\u00e1c s\u1ed1 ch\u1eb5n trong danh s\u00e1ch \u0111\u00f3, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng Lambda expression nh\u01b0 sau:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Arrays;\nimport java.util.List;\n\npublic class LambdaExample {\n  public static void main(String&#91;] args) {\n    List&lt;Integer&gt; numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);\n\n    \/\/ S\u1eed d\u1ee5ng Lambda expression \u0111\u1ec3 in c\u00e1c s\u1ed1 ch\u1eb5n\n    numbers.forEach(n -&gt; {\n      if (n % 2 == 0) {\n        System.out.println(n);\n      }\n    });\n  }\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong v\u00ed d\u1ee5 tr\u00ean, <\/span><span style=\"font-weight: 400;\">n -&gt; { if (n % 2 == 0) System.out.println(n); }<\/span><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t Lambda expression thay th\u1ebf cho vi\u1ec7c t\u1ea1o m\u1ed9t anonymous class.<\/span><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-default-va-static-methods-trong-interface-la-gi-khac-gi-v\u1edbi-cac-ph\u01b0\u01a1ng-th\u1ee9c-thong-th\u01b0\u1eddng\"><strong>Default v\u00e0 Static methods trong Interface l\u00e0 g\u00ec? Kh\u00e1c g\u00ec v\u1edbi c\u00e1c ph\u01b0\u01a1ng th\u1ee9c th\u00f4ng th\u01b0\u1eddng?<\/strong><\/h3>\n\n\n\n<p><b>Default method<\/b><span style=\"font-weight: 400;\"> v\u00e0 <\/span><b>Static method<\/b><span style=\"font-weight: 400;\"> trong <\/span><b>interface<\/b><span style=\"font-weight: 400;\"> c\u1ee7a Java l\u00e0 nh\u1eefng t\u00ednh n\u0103ng \u0111\u01b0\u1ee3c gi\u1edbi thi\u1ec7u t\u1eeb Java 8, nh\u1eb1m cung c\u1ea5p t\u00ednh linh ho\u1ea1t h\u01a1n trong vi\u1ec7c ph\u00e1t tri\u1ec3n v\u00e0 duy tr\u00ec c\u00e1c interface.<\/span><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-default-method\"><b>Default method<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u00e2y l\u00e0 m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c trong interface c\u00f3 ph\u1ea7n th\u00e2n (c\u00f3 th\u1ec3 c\u00f3 m\u00e3 th\u1ef1c thi) v\u00e0 s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">default<\/span><span style=\"font-weight: 400;\">. C\u00e1c ph\u01b0\u01a1ng th\u1ee9c n\u00e0y cho ph\u00e9p interface cung c\u1ea5p m\u1ed9t c\u00e0i \u0111\u1eb7t m\u1eb7c \u0111\u1ecbnh, gi\u00fap c\u00e1c l\u1edbp tri\u1ec3n khai kh\u00f4ng c\u1ea7n ph\u1ea3i c\u00e0i \u0111\u1eb7t l\u1ea1i ph\u01b0\u01a1ng th\u1ee9c \u0111\u00f3 n\u1ebfu kh\u00f4ng mu\u1ed1n thay \u0111\u1ed5i h\u00e0nh vi m\u1eb7c \u0111\u1ecbnh.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface MyInterface {\n  default void sayHello() {\n    System.out.println(\"Hello\");\n  }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-static-method\"><b>Static method<\/b><\/h4>\n\n\n\n<p><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng th\u1ee9c t\u0129nh trong interface c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c g\u1ecdi m\u00e0 kh\u00f4ng c\u1ea7n t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a interface. C\u00e1c ph\u01b0\u01a1ng th\u1ee9c n\u00e0y c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 cung c\u1ea5p c\u00e1c ch\u1ee9c n\u0103ng ti\u1ec7n \u00edch ho\u1eb7c h\u1ed7 tr\u1ee3 li\u00ean quan \u0111\u1ebfn interface.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface MyInterface {\n  static void printMessage() {\n    System.out.println(\"This is a static method\");\n  }\n}<\/code><\/pre>\n\n\n\n<p><b>S\u1ef1 kh\u00e1c bi\u1ec7t gi\u1eefa Default v\u00e0 Static so v\u1edbi ph\u01b0\u01a1ng th\u1ee9c th\u00f4ng th\u01b0\u1eddng:<\/b><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng th\u1ee9c th\u00f4ng th\u01b0\u1eddng trong interface kh\u00f4ng th\u1ec3 c\u00f3 ph\u1ea7n th\u00e2n, ch\u00fang ch\u1ec9 c\u00f3 th\u1ec3 khai b\u00e1o m\u00e0 kh\u00f4ng c\u00f3 m\u00e3 th\u1ef1c thi, v\u00e0 y\u00eau c\u1ea7u c\u00e1c l\u1edbp th\u1ef1c thi ph\u1ea3i c\u00e0i \u0111\u1eb7t ph\u01b0\u01a1ng th\u1ee9c \u0111\u00f3.<\/span><\/li>\n\n\n\n<li><b>Default<\/b><span style=\"font-weight: 400;\"> v\u00e0 <\/span><b>Static<\/b><span style=\"font-weight: 400;\"> methods c\u00f3 m\u00e3 th\u1ef1c thi, trong khi ph\u01b0\u01a1ng th\u1ee9c th\u00f4ng th\u01b0\u1eddng th\u00ec kh\u00f4ng.<\/span><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-t\u1ed5ng-k\u1ebft-cau-h\u1ecfi-ph\u1ecfng-v\u1ea5n-java\"><span class=\"ez-toc-section\" id=\"Tong_ket_cau_hoi_phong_van_Java\"><\/span><span style=\"font-weight: 400;\"><strong>T\u1ed5ng k\u1ebft c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java<\/strong><\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><span style=\"font-weight: 400;\">Trong b\u00e0i vi\u1ebft n\u00e0y, ch\u00fang ta \u0111\u00e3 kh\u00e1m ph\u00e1 m\u1ed9t s\u1ed1 c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java ph\u1ed5 bi\u1ebfn d\u00e0nh cho c\u00e1c l\u1eadp tr\u00ecnh vi\u00ean Java. C\u00e1c c\u00e2u h\u1ecfi n\u00e0y kh\u00f4ng ch\u1ec9 ki\u1ec3m tra ki\u1ebfn th\u1ee9c chuy\u00ean m\u00f4n m\u00e0 c\u00f2n \u0111\u00e1nh gi\u00e1 kh\u1ea3 n\u0103ng gi\u1ea3i quy\u1ebft v\u1ea5n \u0111\u1ec1, t\u01b0 duy logic v\u00e0 kh\u1ea3 n\u0103ng l\u00e0m vi\u1ec7c nh\u00f3m c\u1ee7a \u1ee9ng vi\u00ean.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 chu\u1ea9n b\u1ecb t\u1ed1t nh\u1ea5t cho c\u00e1c cu\u1ed9c ph\u1ecfng v\u1ea5n, \u1ee9ng vi\u00ean c\u1ea7n n\u1eafm v\u1eefng l\u00fd thuy\u1ebft v\u00e0 th\u1ef1c h\u00e0nh nhi\u1ec1u v\u00ed d\u1ee5 th\u1ef1c t\u1ebf, \u0111\u1ed3ng th\u1eddi th\u1ec3 hi\u1ec7n s\u1ef1 linh ho\u1ea1t trong vi\u1ec7c \u00e1p d\u1ee5ng c\u00e1c ki\u1ebfn th\u1ee9c v\u00e0o gi\u1ea3i quy\u1ebft c\u00e1c t\u00ecnh hu\u1ed1ng th\u1ef1c t\u1ebf trong c\u00f4ng vi\u1ec7c.<\/span><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Sau khi \u0111\u00e3 luy\u1ec7n t\u1eadp c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java, h\u00e3y \u1ee9ng tuy\u1ec3n <strong><a href=\"https:\/\/itviec.com\/it-jobs\/java?utm_medium=anchor_text_low&amp;utm_source=blog&amp;utm_campaign=viec_lam_java&amp;utm_content=cau_hoi_phong_van_java\" target=\"_blank\" rel=\"noreferrer noopener\">vi\u1ec7c l\u00e0m Java<\/a><\/strong> m\u1edbi nh\u1ea5t tr\u00ean to\u00e0n qu\u1ed1c. Ch\u00fac b\u1ea1n may m\u1eafn trong bu\u1ed5i ph\u1ecfng v\u1ea5n!<\/em><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>T\u1ed5ng h\u1ee3p 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java ph\u1ed5 bi\u1ebfn d\u00e0nh cho c\u00e1c l\u1eadp tr\u00ecnh vi\u00ean Java, t\u1eeb nh\u1eefng c\u00e2u h\u1ecfi c\u01a1 b\u1ea3n v\u1ec1 c\u00fa ph\u00e1p v\u00e0 t\u00ednh n\u0103ng ng\u00f4n ng\u1eef \u0111\u1ebfn nh\u1eefng v\u1ea5n \u0111\u1ec1 n\u00e2ng cao nh\u01b0 x\u1eed l\u00fd \u0111\u1ed3ng th\u1eddi, t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t v\u00e0 hi\u1ec3u bi\u1ebft v\u1ec1 c\u00e1c framework ph\u1ed5 bi\u1ebfn. [&hellip;]<\/p>\n","protected":false},"author":203,"featured_media":82290,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109,105],"tags":[],"class_list":["post-82081","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it","category-phong-van-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>Top 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java nh\u1ea5t \u0111\u1ecbnh c\u00f3 trong bu\u1ed5i ph\u1ecfng v\u1ea5n - ITviec Blog<\/title>\n<meta name=\"description\" content=\"T\u1ed5ng h\u1ee3p 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java ph\u1ed5 bi\u1ebfn, t\u1eeb c\u00fa ph\u00e1p v\u00e0 t\u00ednh n\u0103ng ng\u00f4n ng\u1eef \u0111\u1ebfn nh\u1eefng v\u1ea5n \u0111\u1ec1 n\u00e2ng cao v\u00e0 c\u00e1c framework ph\u1ed5 bi\u1ebfn.\" \/>\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\/cau-hoi-phong-van-java\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Top 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java nh\u1ea5t \u0111\u1ecbnh c\u00f3 trong bu\u1ed5i ph\u1ecfng v\u1ea5n\" \/>\n<meta property=\"og:description\" content=\"T\u1ed5ng h\u1ee3p 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java ph\u1ed5 bi\u1ebfn d\u00e0nh cho c\u00e1c l\u1eadp tr\u00ecnh vi\u00ean Java, t\u1eeb nh\u1eefng c\u00e2u h\u1ecfi c\u01a1 b\u1ea3n v\u1ec1 c\u00fa ph\u00e1p v\u00e0 t\u00ednh n\u0103ng ng\u00f4n ng\u1eef \u0111\u1ebfn nh\u1eefng v\u1ea5n \u0111\u1ec1\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-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-11-29T04:48:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-27T16:23:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-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=\"56 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Top 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java nh\u1ea5t \u0111\u1ecbnh c\u00f3 trong bu\u1ed5i ph\u1ecfng v\u1ea5n - ITviec Blog","description":"T\u1ed5ng h\u1ee3p 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java ph\u1ed5 bi\u1ebfn, t\u1eeb c\u00fa ph\u00e1p v\u00e0 t\u00ednh n\u0103ng ng\u00f4n ng\u1eef \u0111\u1ebfn nh\u1eefng v\u1ea5n \u0111\u1ec1 n\u00e2ng cao v\u00e0 c\u00e1c framework ph\u1ed5 bi\u1ebfn.","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\/cau-hoi-phong-van-java\/","og_locale":"vi_VN","og_type":"article","og_title":"Top 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java nh\u1ea5t \u0111\u1ecbnh c\u00f3 trong bu\u1ed5i ph\u1ecfng v\u1ea5n","og_description":"T\u1ed5ng h\u1ee3p 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java ph\u1ed5 bi\u1ebfn d\u00e0nh cho c\u00e1c l\u1eadp tr\u00ecnh vi\u00ean Java, t\u1eeb nh\u1eefng c\u00e2u h\u1ecfi c\u01a1 b\u1ea3n v\u1ec1 c\u00fa ph\u00e1p v\u00e0 t\u00ednh n\u0103ng ng\u00f4n ng\u1eef \u0111\u1ebfn nh\u1eefng v\u1ea5n \u0111\u1ec1","og_url":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-java\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2024-11-29T04:48:58+00:00","article_modified_time":"2025-09-27T16:23:38+00:00","og_image":[{"width":1500,"height":790,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-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":"56 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-java\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-java\/"},"author":{"name":"Tien Tran","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/1595d671c49cfa2a48cd3c0a047a1298"},"headline":"Top 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java nh\u1ea5t \u0111\u1ecbnh c\u00f3 trong bu\u1ed5i ph\u1ecfng v\u1ea5n","datePublished":"2024-11-29T04:48:58+00:00","dateModified":"2025-09-27T16:23:38+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-java\/"},"wordCount":15168,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-java\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Java-vippro.jpg","articleSection":["Chuy\u00ean m\u00f4n IT","Ph\u1ecfng v\u1ea5n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-java\/","url":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-java\/","name":"Top 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java nh\u1ea5t \u0111\u1ecbnh c\u00f3 trong bu\u1ed5i ph\u1ecfng v\u1ea5n - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-java\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-java\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Java-vippro.jpg","datePublished":"2024-11-29T04:48:58+00:00","dateModified":"2025-09-27T16:23:38+00:00","description":"T\u1ed5ng h\u1ee3p 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java ph\u1ed5 bi\u1ebfn, t\u1eeb c\u00fa ph\u00e1p v\u00e0 t\u00ednh n\u0103ng ng\u00f4n ng\u1eef \u0111\u1ebfn nh\u1eefng v\u1ea5n \u0111\u1ec1 n\u00e2ng cao v\u00e0 c\u00e1c framework ph\u1ed5 bi\u1ebfn.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-java\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/cau-hoi-phong-van-java\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-java\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Java-vippro.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Java-vippro.jpg","width":1500,"height":790,"caption":"ca\u0302u ho\u0309i pho\u0309ng va\u0302\u0301n Java - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-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":"Top 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Java nh\u1ea5t \u0111\u1ecbnh c\u00f3 trong bu\u1ed5i ph\u1ecfng v\u1ea5n"}]},{"@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\/82081","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=82081"}],"version-history":[{"count":1,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/82081\/revisions"}],"predecessor-version":[{"id":91722,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/82081\/revisions\/91722"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/82290"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=82081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=82081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=82081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}