{"id":82079,"date":"2024-11-29T10:44:51","date_gmt":"2024-11-29T03:44:51","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=82079"},"modified":"2024-11-29T11:09:30","modified_gmt":"2024-11-29T04:09:30","slug":"cau-hoi-phong-van-kotlin","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-kotlin\/","title":{"rendered":"Top 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin s\u1ebd g\u1eb7p trong bu\u1ed5i ph\u1ecfng v\u1ea5n"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 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-kotlin\/#Kotlin_Developer_la_ai_Can_nhung_ky_nang_gi\" >Kotlin Developer l\u00e0 ai? C\u1ea7n nh\u1eefng k\u1ef9 n\u0103ng g\u00ec?<\/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-kotlin\/#Cac_cau_hoi_phong_van_Kotlin_ve_kien_thuc_Kotlin_co_ban\" >C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin v\u1ec1 ki\u1ebfn th\u1ee9c Kotlin 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-kotlin\/#Cau_hoi_phong_van_Kotlin_ve_lap_trinh_huong_doi_tuong_OOP_trong_Kotlin\" >C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin v\u1ec1 l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng (OOP) trong Kotlin<\/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-kotlin\/#Cau_hoi_phong_van_Kotlin_ve_xu_ly_bat_dong_bo_va_luong_du_lieu_Concurrency\" >C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin v\u1ec1 x\u1eed l\u00fd b\u1ea5t \u0111\u1ed3ng b\u1ed9 v\u00e0 lu\u1ed3ng d\u1eef li\u1ec7u (Concurrency)<\/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-kotlin\/#Cau_hoi_phong_van_Kotlin_ve_kien_thuc_lap_trinh_ham\" >C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin v\u1ec1 ki\u1ebfn th\u1ee9c l\u1eadp tr\u00ecnh h\u00e0m<\/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-kotlin\/#Cau_hoi_phong_van_Kotlin_ve_hieu_suat_va_toi_uu_hoa\" >C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin v\u1ec1 hi\u1ec7u su\u1ea5t v\u00e0 t\u1ed1i \u01b0u h\u00f3a<\/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-kotlin\/#Tong_ket_cau_hoi_phong_van_Kotlin\" >T\u1ed5ng k\u1ebft c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin<\/a><\/li><\/ul><\/nav><\/div>\n<p><em><strong>B\u00e0i vi\u1ebft t\u1ed5ng h\u1ee3p 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin c\u00f9ng c\u00e2u tr\u1ea3 l\u1eddi c\u1ef1c k\u1ef3 chi ti\u1ebft. Th\u00f4ng qua c\u00e1c c\u00e2u h\u1ecfi v\u1ec1 c\u00fa ph\u00e1p, th\u01b0 vi\u1ec7n, framework, v\u00e0 c\u00e1c t\u00ednh n\u0103ng \u0111\u1eb7c tr\u01b0ng c\u1ee7a Kotlin, \u1ee9ng vi\u00ean c\u00f3 th\u1ec3 th\u1ec3 hi\u1ec7n s\u1ef1 hi\u1ec3u bi\u1ebft s\u00e2u s\u1eafc v\u1ec1 ng\u00f4n ng\u1eef n\u00e0y v\u00e0 kh\u1ea3 n\u0103ng \u00e1p d\u1ee5ng v\u00e0o c\u00e1c d\u1ef1 \u00e1n th\u1ef1c t\u1ebf.<\/strong><\/em><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u th\u00eam v\u1ec1:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin v\u1ec1 ki\u1ebfn th\u1ee9c c\u01a1 b\u1ea3n c\u1ee7a Kotlin<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin v\u1ec1 l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng(OOP) trong kotlin<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin v\u1ec1 x\u1eed l\u00fd b\u1ea5t \u0111\u1ed3ng b\u1ed9 v\u00e0 lu\u1ed3ng d\u1eef li\u1ec7u(Concurrency)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin v\u1ec1 l\u1eadp tr\u00ecnh h\u00e0m trong Kotlin\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin v\u1ec1 hi\u1ec7u su\u1ea5t v\u00e0 t\u1ed1i \u01b0u ho\u00e1 trong Kotlin<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Kotlin_Developer_la_ai_Can_nhung_ky_nang_gi\"><\/span><strong>Kotlin Developer l\u00e0 ai? C\u1ea7n nh\u1eefng k\u1ef9 n\u0103ng g\u00ec?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Kotlin Developer l\u00e0 ng\u01b0\u1eddi chuy\u00ean ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng, \u0111\u1eb7c bi\u1ec7t l\u00e0 \u1ee9ng d\u1ee5ng Android, v\u1edbi ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh Kotlin. <\/span><span style=\"font-weight: 400;\">H\u1ecd ch\u1ecbu tr\u00e1ch nhi\u1ec7m thi\u1ebft k\u1ebf, ph\u00e1t tri\u1ec3n v\u00e0 b\u1ea3o tr\u00ec \u1ee9ng d\u1ee5ng, \u0111\u1ea3m b\u1ea3o t\u00ednh \u1ed5n \u0111\u1ecbnh, hi\u1ec7u su\u1ea5t cao v\u00e0 ph\u00f9 h\u1ee3p v\u1edbi nhu c\u1ea7u ng\u01b0\u1eddi d\u00f9ng. M\u1ed9t Kotlin Developer gi\u1ecfi s\u1ebd v\u1eeba \u0111\u1ea3m b\u1ea3o ch\u1ea5t l\u01b0\u1ee3ng s\u1ea3n ph\u1ea9m, v\u1eeba \u0111\u00f3ng g\u00f3p t\u00edch c\u1ef1c v\u00e0o s\u1ef1 ph\u00e1t tri\u1ec3n c\u1ee7a d\u1ef1 \u00e1n v\u00e0 tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 th\u00e0nh c\u00f4ng, Kotlin Developer c\u1ea7n:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Am hi\u1ec3u v\u1ec1 Android SDK, c\u00e1c th\u00e0nh ph\u1ea7n ch\u00ednh c\u1ee7a Android, v\u00e0 c\u00e1c th\u01b0 vi\u1ec7n ph\u1ed5 bi\u1ebfn nh\u01b0 Retrofit, Coroutines, v\u00e0 Room.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">C\u00e1c c\u00f4ng c\u1ee5 nh\u01b0 Android Studio, Git, v\u00e0 c\u00e1c c\u00f4ng c\u1ee5 ki\u1ec3m th\u1eed nh\u01b0 JUnit v\u00e0 Espresso c\u0169ng kh\u00f4ng th\u1ec3 thi\u1ebfu trong c\u00f4ng vi\u1ec7c h\u00e0ng ng\u00e0y, gi\u00fap t\u1ed1i \u01b0u h\u00f3a qu\u00e1 tr\u00ecnh ph\u00e1t tri\u1ec3n v\u00e0 ki\u1ec3m tra \u1ee9ng d\u1ee5ng.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">B\u00ean c\u1ea1nh k\u1ef9 n\u0103ng chuy\u00ean m\u00f4n, h\u1ecd c\u1ea7n c\u00f3 kh\u1ea3 n\u0103ng giao ti\u1ebfp, l\u00e0m vi\u1ec7c nh\u00f3m v\u00e0 qu\u1ea3n l\u00fd th\u1eddi gian.<\/span><\/li>\n<\/ul>\n<p>T\u00ecm hi\u1ec3u t\u1ed5ng quan v\u1ec1 Kotlin v\u00e0 Kotlin Developer qua c\u00e1c b\u00e0i vi\u1ebft sau:<\/p>\n<ul>\n<li><a href=\"https:\/\/itviec.com\/blog\/kotlin-la-gi\/\" target=\"_blank\" rel=\"noopener\">Kotlin l\u00e0 g\u00ec? \u0110\u1ecbnh ngh\u0129a, \u01b0u \u0111i\u1ec3m v\u00e0 nh\u1eefng c\u00fa ph\u00e1p c\u01a1 b\u1ea3n c\u1ee7a Kotlin<\/a><\/li>\n<li><a href=\"https:\/\/itviec.com\/blog\/hoc-kotlin\/\" target=\"_blank\" rel=\"noopener\">H\u1ecdc Kotlin t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn n\u00e2ng cao cho ng\u01b0\u1eddi m\u1edbi b\u1eaft \u0111\u1ea7u<\/a><\/li>\n<li><a href=\"https:\/\/itviec.com\/blog\/kotlin-roadmap\/\" target=\"_blank\" rel=\"noopener\">Kotlin roadmap: L\u1ed9 tr\u00ecnh h\u1ecdc t\u1eadp v\u00e0 ph\u00e1t tri\u1ec3n c\u1ee7a Kotlin Developer<\/a><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Cac_cau_hoi_phong_van_Kotlin_ve_kien_thuc_Kotlin_co_ban\"><\/span><strong>C\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin v\u1ec1 ki\u1ebfn th\u1ee9c Kotlin c\u01a1 b\u1ea3n<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><strong>Kotlin c\u00f3 nh\u1eefng t\u00ednh n\u0103ng \u0111\u1eb7c bi\u1ec7t n\u00e0o so v\u1edbi Java?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Kotlin c\u00f3 nhi\u1ec1u t\u00ednh n\u0103ng \u0111\u1eb7c bi\u1ec7t gi\u00fap n\u00f3 n\u1ed5i b\u1eadt h\u01a1n so v\u1edbi Java, \u0111\u1eb7c bi\u1ec7t trong ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng Android. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t s\u1ed1 t\u00ednh n\u0103ng ch\u00ednh:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>Null Safety<\/strong>: T\u00ednh n\u0103ng n\u1ed5i b\u1eadt gi\u00fap gi\u1ea3m thi\u1ec3u l\u1ed7i null pointer exception (NPE), m\u1ed9t trong nh\u1eefng l\u1ed7i ph\u1ed5 bi\u1ebfn nh\u1ea5t khi l\u1eadp tr\u00ecnh v\u1edbi Java. Kotlin gi\u00fap x\u1eed l\u00fd null an to\u00e0n h\u01a1n b\u1eb1ng c\u00e1ch ph\u00e2n bi\u1ec7t gi\u1eefa c\u00e1c bi\u1ebfn c\u00f3 th\u1ec3 null (<\/span><span style=\"font-weight: 400;\">String?<\/span><span style=\"font-weight: 400;\">) v\u00e0 kh\u00f4ng th\u1ec3 null (<\/span><span style=\"font-weight: 400;\">String<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>Extension Functions<\/strong>: Kotlin cho ph\u00e9p m\u1edf r\u1ed9ng ch\u1ee9c n\u0103ng c\u1ee7a c\u00e1c l\u1edbp hi\u1ec7n c\u00f3 m\u00e0 kh\u00f4ng c\u1ea7n s\u1eeda \u0111\u1ed5i m\u00e3 g\u1ed1c. \u0110i\u1ec1u n\u00e0y gi\u00fap vi\u1ebft code g\u1ecdn g\u00e0ng, d\u1ec5 hi\u1ec3u v\u00e0 t\u0103ng kh\u1ea3 n\u0103ng t\u00e1i s\u1eed d\u1ee5ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>Data Classes<\/strong>: Kotlin h\u1ed7 tr\u1ee3 lo\u1ea1i l\u1edbp \u0111\u1eb7c bi\u1ec7t g\u1ecdi l\u00e0 <\/span><span style=\"font-weight: 400;\">data class<\/span><span style=\"font-weight: 400;\"> gi\u00fap ti\u1ebft ki\u1ec7m th\u1eddi gian khi t\u1ea1o c\u00e1c l\u1edbp ch\u1ec9 d\u00f9ng \u0111\u1ec3 l\u01b0u d\u1eef li\u1ec7u. C\u00e1c l\u1edbp n\u00e0y t\u1ef1 \u0111\u1ed9ng sinh ra c\u00e1c ph\u01b0\u01a1ng th\u1ee9c c\u01a1 b\u1ea3n nh\u01b0 <\/span><span style=\"font-weight: 400;\">equals()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">hashCode()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">toString()<\/span><span style=\"font-weight: 400;\">, gi\u00fap t\u1ed1i \u01b0u h\u00f3a v\u00e0 \u0111\u01a1n gi\u1ea3n h\u00f3a m\u00e3.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>Coroutines<\/strong>: Kotlin cung c\u1ea5p coroutines \u0111\u1ec3 h\u1ed7 tr\u1ee3 l\u1eadp tr\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9 v\u00e0 x\u1eed l\u00fd \u0111a lu\u1ed3ng d\u1ec5 d\u00e0ng h\u01a1n so v\u1edbi Java. Coroutines gi\u00fap gi\u1ea3m thi\u1ec3u callback hell v\u00e0 \u0111\u01a1n gi\u1ea3n h\u00f3a code khi th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 n\u1ec1n, nh\u01b0 g\u1ecdi API hay truy c\u1eadp d\u1eef li\u1ec7u.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>Type Inference<\/strong>: V\u1edbi kh\u1ea3 n\u0103ng suy lu\u1eadn ki\u1ec3u, Kotlin gi\u00fap gi\u1ea3m thi\u1ec3u vi\u1ec7c ph\u1ea3i khai b\u00e1o ki\u1ec3u d\u1eef li\u1ec7u t\u01b0\u1eddng minh, gi\u00fap code ng\u1eafn g\u1ecdn v\u00e0 d\u1ec5 \u0111\u1ecdc h\u01a1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>Smart Casts<\/strong>: Kotlin c\u00f3 t\u00ednh n\u0103ng smart casting, gi\u00fap t\u1ef1 \u0111\u1ed9ng chuy\u1ec3n \u0111\u1ed5i ki\u1ec3u m\u1ed9t c\u00e1ch an to\u00e0n. \u0110i\u1ec1u n\u00e0y gi\u00fap lo\u1ea1i b\u1ecf vi\u1ec7c ki\u1ec3m tra ki\u1ec3u th\u1ee7 c\u00f4ng b\u1eb1ng c\u00e1ch x\u00e1c \u0111\u1ecbnh ki\u1ec3u d\u1eef li\u1ec7u ngay khi n\u00f3 \u0111\u01b0\u1ee3c ki\u1ec3m tra.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>Higher-Order Functions v\u00e0 Lambdas<\/strong>: Kotlin h\u1ed7 tr\u1ee3 l\u1eadp tr\u00ecnh h\u00e0m v\u1edbi c\u00e1c h\u00e0m b\u1eadc cao v\u00e0 bi\u1ec3u th\u1ee9c lambda, gi\u00fap vi\u1ebft code linh ho\u1ea1t v\u00e0 d\u1ec5 b\u1ea3o tr\u00ec. \u0110i\u1ec1u n\u00e0y r\u1ea5t h\u1eefu \u00edch trong vi\u1ec7c x\u1eed l\u00fd c\u00e1c thao t\u00e1c l\u1eb7p, x\u1eed l\u00fd s\u1ef1 ki\u1ec7n, v\u00e0 l\u1eadp tr\u00ecnh h\u00e0m.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><strong>Primary Constructors v\u00e0 Named Arguments<\/strong>: Kotlin c\u00f3 c\u1ea5u tr\u00fac h\u00e0m kh\u1edfi t\u1ea1o ch\u00ednh (primary constructor) v\u00e0 h\u1ed7 tr\u1ee3 c\u00e1c tham s\u1ed1 c\u00f3 t\u00ean, gi\u00fap code ng\u1eafn g\u1ecdn v\u00e0 d\u1ec5 hi\u1ec3u h\u01a1n, \u0111\u1eb7c bi\u1ec7t khi kh\u1edfi t\u1ea1o c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng v\u1edbi nhi\u1ec1u tham s\u1ed1.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Nh\u1edd c\u00e1c t\u00ednh n\u0103ng n\u00e0y, Kotlin kh\u00f4ng ch\u1ec9 gi\u00fap vi\u1ebft code g\u1ecdn g\u00e0ng, an to\u00e0n m\u00e0 c\u00f2n n\u00e2ng cao hi\u1ec7u su\u1ea5t v\u00e0 tr\u1ea3i nghi\u1ec7m l\u1eadp tr\u00ecnh, \u0111\u1eb7c bi\u1ec7t l\u00e0 trong ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng Android.<\/span><\/p>\n<blockquote><p>\u0110\u1ecdc th\u00eam: <a href=\"https:\/\/itviec.com\/blog\/kotlin-vs-java\/\" target=\"_blank\" rel=\"noopener\"><strong>Kotlin vs Java: Khi n\u00e0o n\u00ean ch\u1ecdn Kotlin? Khi n\u00e0o n\u00ean ch\u1ecdn Java?<\/strong><\/a><\/p><\/blockquote>\n<h3><strong>\u0110i\u1ec3m kh\u00e1c nhau gi\u1eefa val v\u00e0 var trong Kotlin l\u00e0 g\u00ec?<\/strong><\/h3>\n<p><strong>val<\/strong><span style=\"font-weight: 400;\"> &#8211; Bi\u1ebfn b\u1ea5t bi\u1ebfn (Immutable):<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">val<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 khai b\u00e1o m\u1ed9t bi\u1ebfn kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i gi\u00e1 tr\u1ecb sau khi \u0111\u00e3 \u0111\u01b0\u1ee3c g\u00e1n ban \u0111\u1ea7u, gi\u1ed1ng nh\u01b0 t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\"> trong Java.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi khai b\u00e1o bi\u1ebfn b\u1eb1ng <\/span><span style=\"font-weight: 400;\">val<\/span><span style=\"font-weight: 400;\">, b\u1ea1n ch\u1ec9 c\u00f3 th\u1ec3 g\u00e1n gi\u00e1 tr\u1ecb cho bi\u1ebfn m\u1ed9t l\u1ea7n duy nh\u1ea5t. Sau \u0111\u00f3, kh\u00f4ng th\u1ec3 g\u00e1n gi\u00e1 tr\u1ecb m\u1edbi cho bi\u1ebfn n\u00e0y.<\/span><\/li>\n<\/ul>\n<p><strong>var<\/strong><span style=\"font-weight: 400;\"> &#8211; Bi\u1ebfn c\u00f3 th\u1ec3 thay \u0111\u1ed5i (Mutable):<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">var<\/span><span style=\"font-weight: 400;\"> cho ph\u00e9p khai b\u00e1o m\u1ed9t bi\u1ebfn c\u00f3 th\u1ec3 thay \u0111\u1ed5i gi\u00e1 tr\u1ecb nhi\u1ec1u l\u1ea7n trong qu\u00e1 tr\u00ecnh th\u1ef1c thi.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 g\u00e1n v\u00e0 thay \u0111\u1ed5i gi\u00e1 tr\u1ecb c\u1ee7a bi\u1ebfn khai b\u00e1o b\u1eb1ng <\/span><span style=\"font-weight: 400;\">var<\/span><span style=\"font-weight: 400;\"> b\u1ea5t k\u1ef3 l\u00fac n\u00e0o.<\/span><\/li>\n<\/ul>\n<h3><strong>Gi\u1ea3i th\u00edch v\u1ec1 t\u00ednh n\u0103ng null safety trong Kotlin<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">T\u00ednh n\u0103ng <\/span><strong>null safety<\/strong><span style=\"font-weight: 400;\"> trong Kotlin gi\u00fap ng\u0103n ng\u1eeba l\u1ed7i <\/span><i><span style=\"font-weight: 400;\">null pointer exception<\/span><\/i><span style=\"font-weight: 400;\"> (NPE), m\u1ed9t trong nh\u1eefng l\u1ed7i th\u01b0\u1eddng g\u1eb7p v\u00e0 g\u00e2y kh\u00f3 ch\u1ecbu nh\u1ea5t, \u0111\u1eb7c bi\u1ec7t trong c\u00e1c ng\u00f4n ng\u1eef nh\u01b0 Java.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Kotlin qu\u1ea3n l\u00fd \u0111i\u1ec1u n\u00e0y b\u1eb1ng c\u00e1ch bu\u1ed9c l\u1eadp tr\u00ecnh vi\u00ean ph\u1ea3i ch\u1ec9 \u0111\u1ecbnh r\u00f5 r\u00e0ng bi\u1ebfn n\u00e0o c\u00f3 th\u1ec3 null v\u00e0 bi\u1ebfn n\u00e0o kh\u00f4ng th\u1ec3 null. Khi khai b\u00e1o m\u1ed9t bi\u1ebfn th\u00f4ng th\u01b0\u1eddng, bi\u1ebfn \u0111\u00f3 m\u1eb7c \u0111\u1ecbnh kh\u00f4ng th\u1ec3 nh\u1eadn gi\u00e1 tr\u1ecb null. N\u1ebfu mu\u1ed1n bi\u1ebfn c\u00f3 th\u1ec3 null, ch\u00fang ta ph\u1ea3i th\u00eam d\u1ea5u <\/span><span style=\"font-weight: 400;\">?<\/span><span style=\"font-weight: 400;\"> v\u00e0o sau ki\u1ec3u d\u1eef li\u1ec7u, v\u00ed d\u1ee5 <\/span><span style=\"font-weight: 400;\">String?<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110i\u1ec1u n\u00e0y quan tr\u1ecdng v\u00ec null safety gi\u00fap ch\u00fang ta ph\u00e1t hi\u1ec7n v\u00e0 ki\u1ec3m so\u00e1t c\u00e1c l\u1ed7i li\u00ean quan \u0111\u1ebfn null ngay t\u1eeb khi bi\u00ean d\u1ecbch, tr\u00e1nh vi\u1ec7c l\u1ed7i n\u00e0y g\u00e2y gi\u00e1n \u0111o\u1ea1n khi \u1ee9ng d\u1ee5ng \u0111ang ch\u1ea1y.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Kotlin c\u00f2n cung c\u1ea5p c\u00e1c c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd nh\u01b0 to\u00e1n t\u1eed safe call (<\/span><span style=\"font-weight: 400;\">?.<\/span><span style=\"font-weight: 400;\">), to\u00e1n t\u1eed Elvis (<\/span><span style=\"font-weight: 400;\">?:<\/span><span style=\"font-weight: 400;\">), v\u00e0 not-null assertion (<\/span><span style=\"font-weight: 400;\">!!<\/span><span style=\"font-weight: 400;\">) \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c bi\u1ebfn c\u00f3 th\u1ec3 null m\u1ed9t c\u00e1ch linh ho\u1ea1t v\u00e0 an to\u00e0n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Nh\u1edd c\u00f3 null safety, Developer c\u00f3 th\u1ec3 vi\u1ebft m\u00e3 ngu\u1ed3n an to\u00e0n, tr\u00e1nh l\u1ed7i runtime li\u00ean quan \u0111\u1ebfn null v\u00e0 t\u1eadp trung h\u01a1n v\u00e0o vi\u1ec7c ph\u00e1t tri\u1ec3n t\u00ednh n\u0103ng cho \u1ee9ng d\u1ee5ng.<\/span><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">T\u00ecm hi\u1ec3u th\u00eam v\u1ec1 null safety: <\/span><a href=\"https:\/\/kotlinlang.org\/docs\/null-safety.html\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">https:\/\/kotlinlang.org\/docs\/null-safety.html<\/span><\/a><\/p><\/blockquote>\n<h3><strong>lateinit v\u00e0 lazy kh\u00e1c nhau nh\u01b0 th\u1ebf n\u00e0o trong Kotlin?<\/strong><\/h3>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">\u0110\u1eb7c \u0111i\u1ec3m\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">lateinit\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">lazy<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Lo\u1ea1i bi\u1ebfn\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">D\u00e0nh cho bi\u1ebfn <\/span><span style=\"font-weight: 400;\">var<\/span><span style=\"font-weight: 400;\"> (mutable).<\/span><\/td>\n<td><span style=\"font-weight: 400;\">D\u00e0nh cho bi\u1ebfn <\/span><span style=\"font-weight: 400;\">val<\/span><span style=\"font-weight: 400;\"> (immutable).<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">kh\u1edfi t\u1ea1o\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u1edfi t\u1ea1o sau, nh\u01b0ng ph\u1ea3i \u0111\u01b0\u1ee3c g\u00e1n gi\u00e1 tr\u1ecb tr\u01b0\u1edbc khi s\u1eed d\u1ee5ng.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u1edfi t\u1ea1o khi c\u1ea7n (l\u1ea7n \u0111\u1ea7u ti\u00ean \u0111\u01b0\u1ee3c truy c\u1eadp).<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Nullable<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng th\u1ec3 s\u1eed d\u1ee5ng v\u1edbi ki\u1ec3u nullable.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 s\u1eed d\u1ee5ng v\u1edbi ki\u1ec3u nullable.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Th\u01b0\u1eddng d\u00f9ng khi kh\u00f4ng th\u1ec3 kh\u1edfi t\u1ea1o ngay l\u1eadp t\u1ee9c (v\u00ed d\u1ee5: trong Android).<\/span><\/td>\n<td><span style=\"font-weight: 400;\">D\u00f9ng khi c\u1ea7n tr\u00ec ho\u00e3n kh\u1edfi t\u1ea1o v\u00e0 ch\u1ec9 c\u1ea7n kh\u1edfi t\u1ea1o m\u1ed9t l\u1ea7n duy nh\u1ea5t.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Ki\u1ec3m tra kh\u1edfi t\u1ea1o\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">N\u1ebfu kh\u00f4ng \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o tr\u01b0\u1edbc khi s\u1eed d\u1ee5ng, s\u1ebd g\u00e2y l\u1ed7i <\/span><span style=\"font-weight: 400;\">UninitializedPropertyAccessException<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\u0110\u1ea3m b\u1ea3o ch\u1ec9 kh\u1edfi t\u1ea1o m\u1ed9t l\u1ea7n duy nh\u1ea5t v\u00e0 t\u1ef1 \u0111\u1ed9ng kh\u1edfi t\u1ea1o khi c\u1ea7n.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><strong>Kotlin h\u1ed7 tr\u1ee3 ki\u1ec3u d\u1eef li\u1ec7u n\u00e0o \u0111\u1ec3 thay th\u1ebf switch-case trong Java?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Kotlin h\u1ed7 tr\u1ee3 <\/span><span style=\"font-weight: 400;\">when<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 thay th\u1ebf cho c\u00e2u l\u1ec7nh <\/span><span style=\"font-weight: 400;\">switch-case<\/span><span style=\"font-weight: 400;\"> trong Java, v\u00e0 th\u1ef1c s\u1ef1, <\/span><span style=\"font-weight: 400;\">when<\/span><span style=\"font-weight: 400;\"> trong Kotlin c\u00f2n m\u1ea1nh m\u1ebd h\u01a1n so v\u1edbi <\/span><span style=\"font-weight: 400;\">switch<\/span><span style=\"font-weight: 400;\"> trong Java.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00e1ch ho\u1ea1t \u0111\u1ed9ng c\u1ee7a <\/span><span style=\"font-weight: 400;\">when<\/span><span style=\"font-weight: 400;\"> trong Kotlin:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u00ednh linh ho\u1ea1t: <\/span><span style=\"font-weight: 400;\">when<\/span><span style=\"font-weight: 400;\"> trong Kotlin c\u00f3 th\u1ec3 ho\u1ea1t \u0111\u1ed9ng gi\u1ed1ng nh\u01b0 m\u1ed9t <\/span><span style=\"font-weight: 400;\">switch<\/span><span style=\"font-weight: 400;\"> nh\u01b0ng linh ho\u1ea1t h\u01a1n, h\u1ed7 tr\u1ee3 nhi\u1ec1u ki\u1ec3u d\u1eef li\u1ec7u kh\u00e1c nhau nh\u01b0 s\u1ed1, chu\u1ed7i, th\u1eadm ch\u00ed c\u1ea3 c\u00e1c \u0111i\u1ec1u ki\u1ec7n ph\u1ee9c t\u1ea1p.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Kh\u00f4ng c\u1ea7n <\/span><span style=\"font-weight: 400;\">break<\/span><span style=\"font-weight: 400;\">: Trong Kotlin, kh\u00f4ng c\u1ea7n ph\u1ea3i s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">break<\/span><span style=\"font-weight: 400;\"> nh\u01b0 trong Java, v\u00ec <\/span><span style=\"font-weight: 400;\">when<\/span><span style=\"font-weight: 400;\"> s\u1ebd t\u1ef1 \u0111\u1ed9ng d\u1eebng khi t\u00ecm \u0111\u01b0\u1ee3c kh\u1edbp v\u1edbi \u0111i\u1ec1u ki\u1ec7n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ki\u1ec3m tra nhi\u1ec1u \u0111i\u1ec1u ki\u1ec7n: B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">when<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 ki\u1ec3m tra nhi\u1ec1u \u0111i\u1ec1u ki\u1ec7n c\u00f9ng l\u00fac, thay v\u00ec ph\u1ea3i vi\u1ebft nhi\u1ec1u c\u00e2u l\u1ec7nh <\/span><span style=\"font-weight: 400;\">case<\/span><span style=\"font-weight: 400;\"> nh\u01b0 trong Java.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 ki\u1ec3u d\u1eef li\u1ec7u ph\u1ee9c t\u1ea1p: <\/span><span style=\"font-weight: 400;\">when<\/span><span style=\"font-weight: 400;\"> c\u00f3 th\u1ec3 so s\u00e1nh v\u1edbi c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 s\u1eed d\u1ee5ng c\u00e1c bi\u1ec3u th\u1ee9c ph\u1ee9c t\u1ea1p, \u0111i\u1ec1u m\u00e0 <\/span><span style=\"font-weight: 400;\">switch<\/span><span style=\"font-weight: 400;\"> trong Java kh\u00f4ng l\u00e0m \u0111\u01b0\u1ee3c.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 v\u1ec1 <\/span><span style=\"font-weight: 400;\">when<\/span><span style=\"font-weight: 400;\"> trong Kotlin:<\/span><\/p>\n<pre>val x = 2\r\nwhen (x) {\r\n  1 -&gt; println(\"One\")\r\n  2 -&gt; println(\"Two\")\r\n  3 -&gt; println(\"Three\")\r\n  else -&gt; println(\"Unknown\")\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">\u01afu \u0111i\u1ec3m c\u1ee7a <\/span><span style=\"font-weight: 400;\">when<\/span><span style=\"font-weight: 400;\"> so v\u1edbi <\/span><span style=\"font-weight: 400;\">switch<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">D\u1ec5 \u0111\u1ecdc v\u00e0 g\u1ecdn g\u00e0ng h\u01a1n: Kh\u00f4ng c\u1ea7n d\u00f9ng <\/span><span style=\"font-weight: 400;\">break<\/span><span style=\"font-weight: 400;\">, v\u00e0 c\u00fa ph\u00e1p c\u1ee7a <\/span><span style=\"font-weight: 400;\">when<\/span><span style=\"font-weight: 400;\"> cho ph\u00e9p vi\u1ebft m\u00e3 ng\u1eafn g\u1ecdn h\u01a1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 nhi\u1ec1u ki\u1ec3u d\u1eef li\u1ec7u v\u00e0 \u0111i\u1ec1u ki\u1ec7n: B\u1ea1n c\u00f3 th\u1ec3 so s\u00e1nh kh\u00f4ng ch\u1ec9 v\u1edbi gi\u00e1 tr\u1ecb s\u1ed1 hay chu\u1ed7i m\u00e0 c\u00f2n v\u1edbi c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng ho\u1eb7c bi\u1ec3u th\u1ee9c ph\u1ee9c t\u1ea1p.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng: B\u1ea1n c\u00f3 th\u1ec3 d\u1ec5 d\u00e0ng m\u1edf r\u1ed9ng <\/span><span style=\"font-weight: 400;\">when<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 ki\u1ec3m tra c\u00e1c \u0111i\u1ec1u ki\u1ec7n kh\u00e1c nhau, ch\u1eb3ng h\u1ea1n nh\u01b0 ki\u1ec3m tra c\u00e1c ph\u1ea1m vi s\u1ed1 h\u1ecdc ho\u1eb7c c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u kh\u00e1c.<\/span><\/li>\n<\/ul>\n<h3><strong>C\u00e1c lo\u1ea1i d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y (primitive types) trong Kotlin l\u00e0 g\u00ec?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, c\u00e1c lo\u1ea1i d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y (primitive types) \u0111\u01b0\u1ee3c x\u1eed l\u00fd kh\u00e1 linh ho\u1ea1t, v\u00e0 ch\u00fang th\u01b0\u1eddng \u0111\u01b0\u1ee3c \u0111\u1ea1i di\u1ec7n d\u01b0\u1edbi d\u1ea1ng c\u00e1c l\u1edbp \u0111\u1ed1i t\u01b0\u1ee3ng (wrapper classes) c\u1ee7a Java. Tuy nhi\u00ean, Kotlin c\u0169ng cung c\u1ea5p c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u c\u01a1 b\u1ea3n t\u01b0\u01a1ng t\u1ef1 nh\u01b0 trong Java nh\u01b0ng v\u1edbi c\u00fa ph\u00e1p g\u1ecdn g\u00e0ng h\u01a1n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">D\u01b0\u1edbi \u0111\u00e2y l\u00e0 c\u00e1c lo\u1ea1i d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y trong Kotlin:<\/span><\/p>\n<h4><strong>Int<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">\u0110\u1ea1i di\u1ec7n cho s\u1ed1 nguy\u00ean, c\u00f3 ph\u1ea1m vi t\u1eeb -2,147,483,648 \u0111\u1ebfn 2,147,483,647. \u0110\u00e2y l\u00e0 ki\u1ec3u s\u1ed1 nguy\u00ean ph\u1ed5 bi\u1ebfn \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c ph\u00e9p to\u00e1n c\u01a1 b\u1ea3n. <\/span><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">val a: Int = 42<\/span><\/pre>\n<h4><strong>Long<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">L\u00e0 ki\u1ec3u s\u1ed1 nguy\u00ean d\u00e0i, c\u00f3 ph\u1ea1m vi r\u1ed9ng h\u01a1n <\/span><span style=\"font-weight: 400;\">Int<\/span><span style=\"font-weight: 400;\">, t\u1eeb -9,223,372,036,854,775,808 \u0111\u1ebfn 9,223,372,036,854,775,807. \u0110\u01b0\u1ee3c s\u1eed d\u1ee5ng khi c\u1ea7n l\u01b0u tr\u1eef c\u00e1c gi\u00e1 tr\u1ecb s\u1ed1 nguy\u00ean l\u1edbn. <\/span><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">val b: Long = 1234567890123L<\/span><\/pre>\n<h4><strong>Double<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">\u0110\u1ea1i di\u1ec7n cho s\u1ed1 th\u1ef1c v\u1edbi \u0111\u1ed9 ch\u00ednh x\u00e1c g\u1ea5p \u0111\u00f4i. <\/span><span style=\"font-weight: 400;\">Double<\/span><span style=\"font-weight: 400;\"> c\u00f3 ph\u1ea1m vi v\u00e0 \u0111\u1ed9 ch\u00ednh x\u00e1c cao h\u01a1n so v\u1edbi <\/span><span style=\"font-weight: 400;\">Float<\/span><span style=\"font-weight: 400;\"> v\u00e0 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 l\u01b0u tr\u1eef c\u00e1c gi\u00e1 tr\u1ecb s\u1ed1 th\u1ef1c. <\/span><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">val c: Double = 3.14<\/span><\/pre>\n<h4><strong>Float<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">L\u00e0 ki\u1ec3u s\u1ed1 th\u1ef1c v\u1edbi \u0111\u1ed9 ch\u00ednh x\u00e1c \u0111\u01a1n, th\u01b0\u1eddng d\u00f9ng khi c\u1ea7n ti\u1ebft ki\u1ec7m b\u1ed9 nh\u1edb ho\u1eb7c khi \u0111\u1ed9 ch\u00ednh x\u00e1c kh\u00f4ng qu\u00e1 quan tr\u1ecdng. <\/span><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">val d: Float = 3.14f<\/span><\/pre>\n<h4><strong>Char<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">\u0110\u1ea1i di\u1ec7n cho m\u1ed9t k\u00fd t\u1ef1 Unicode. M\u1ed7i gi\u00e1 tr\u1ecb ki\u1ec3u <\/span><span style=\"font-weight: 400;\">Char<\/span><span style=\"font-weight: 400;\"> s\u1ebd l\u01b0u tr\u1eef m\u1ed9t k\u00fd t\u1ef1 \u0111\u01a1n, nh\u01b0 &#8216;a&#8217;, &#8216;1&#8217;, ho\u1eb7c &#8216;A&#8217;. <\/span><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">val e: Char = 'K'<\/span><\/pre>\n<h4><strong>Boolean<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">\u0110\u1ea1i di\u1ec7n cho gi\u00e1 tr\u1ecb \u0111\u00fang ho\u1eb7c sai. <\/span><span style=\"font-weight: 400;\">Boolean<\/span><span style=\"font-weight: 400;\"> c\u00f3 hai gi\u00e1 tr\u1ecb c\u00f3 th\u1ec3: <\/span><span style=\"font-weight: 400;\">true<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">false<\/span><span style=\"font-weight: 400;\">. <\/span><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">val f: Boolean = true<\/span><\/pre>\n<h4><strong>Byte<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">L\u00e0 ki\u1ec3u s\u1ed1 nguy\u00ean 8 bit, v\u1edbi ph\u1ea1m vi t\u1eeb -128 \u0111\u1ebfn 127. Th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c t\u00ecnh hu\u1ed1ng c\u1ea7n ti\u1ebft ki\u1ec7m b\u1ed9 nh\u1edb. <\/span><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">val g: Byte = 100<\/span><\/pre>\n<h4><strong>Short<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">L\u00e0 ki\u1ec3u s\u1ed1 nguy\u00ean 16 bit, c\u00f3 ph\u1ea1m vi t\u1eeb -32,768 \u0111\u1ebfn 32,767. \u0110\u01b0\u1ee3c s\u1eed d\u1ee5ng khi c\u1ea7n ti\u1ebft ki\u1ec7m b\u1ed9 nh\u1edb h\u01a1n <\/span><span style=\"font-weight: 400;\">Int<\/span><span style=\"font-weight: 400;\"> nh\u01b0ng ph\u1ea1m vi nh\u1ecf h\u01a1n. <\/span><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">val h: Short = 1000<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">C\u00e1c ki\u1ec3u d\u1eef li\u1ec7u n\u00e0y trong Kotlin c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng nh\u01b0 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng, nh\u01b0ng ch\u00fang c\u0169ng h\u1ed7 tr\u1ee3 c\u00e1c thao t\u00e1c t\u1ed1i \u01b0u v\u00e0 hi\u1ec7u qu\u1ea3 h\u01a1n khi c\u1ea7n x\u1eed l\u00fd d\u1eef li\u1ec7u \u1edf c\u1ea5p \u0111\u1ed9 nguy\u00ean th\u1ee7y. Kotlin cung c\u1ea5p s\u1ef1 t\u01b0\u01a1ng th\u00edch ho\u00e0n h\u1ea3o v\u1edbi Java, do \u0111\u00f3 c\u00e1c ki\u1ec3u n\u00e0y s\u1ebd \u0111\u01b0\u1ee3c t\u1ef1 \u0111\u1ed9ng chuy\u1ec3n \u0111\u1ed5i gi\u1eefa c\u00e1c l\u1edbp \u0111\u1ed1i t\u01b0\u1ee3ng t\u01b0\u01a1ng \u1ee9ng khi c\u1ea7n thi\u1ebft.<\/span><\/p>\n<h3><strong>Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng sealed class v\u00e0 enum class?<\/strong><\/h3>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">Ti\u00eau ch\u00ed<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Sealed Class\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Enum Class<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">M\u1ee5c \u0111\u00edch s\u1eed d\u1ee5ng\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\u0110\u1ec3 \u0111\u1ecbnh ngh\u0129a m\u1ed9t nh\u00f3m c\u00e1c l\u1edbp con c\u00f3 quan h\u1ec7 k\u1ebf th\u1eeba, s\u1ed1 l\u01b0\u1ee3ng l\u1edbp con l\u00e0 c\u1ed1 \u0111\u1ecbnh v\u00e0 h\u1ea1n ch\u1ebf.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\u0110\u1ec3 \u0111\u1ecbnh ngh\u0129a m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c h\u1eb1ng s\u1ed1 c\u1ed1 \u0111\u1ecbnh, kh\u00f4ng thay \u0111\u1ed5i.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">K\u1ebf th\u1eeba<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Cho ph\u00e9p k\u1ebf th\u1eeba (c\u00f3 th\u1ec3 c\u00f3 l\u1edbp con v\u1edbi c\u00e1c h\u00e0nh vi v\u00e0 thu\u1ed9c t\u00ednh ri\u00eang).<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng cho ph\u00e9p k\u1ebf th\u1eeba, ch\u1ec9 \u0111\u1ecbnh ngh\u0129a c\u00e1c gi\u00e1 tr\u1ecb c\u1ed1 \u0111\u1ecbnh.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">S\u01b0 linh ho\u1ea1t\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Linh ho\u1ea1t h\u01a1n, c\u00f3 th\u1ec3 ch\u1ee9a d\u1eef li\u1ec7u v\u00e0 ph\u01b0\u01a1ng th\u1ee9c trong c\u00e1c l\u1edbp con.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\u00cdt linh ho\u1ea1t h\u01a1n, ch\u1ee7 y\u1ebfu ch\u1ec9 ch\u1ee9a c\u00e1c gi\u00e1 tr\u1ecb c\u1ed1 \u0111\u1ecbnh, c\u00f3 th\u1ec3 c\u00f3 ph\u01b0\u01a1ng th\u1ee9c nh\u01b0ng kh\u00f4ng th\u1ec3 m\u1edf r\u1ed9ng th\u00eam gi\u00e1 tr\u1ecb.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Ki\u1ec3m so\u00e1t c\u00e1c tr\u01b0\u1eddng h\u1ee3p\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kotlin c\u00f3 th\u1ec3 ki\u1ec3m tra t\u1ea5t c\u1ea3 c\u00e1c l\u1edbp con khi s\u1eed d\u1ee5ng trong <\/span><span style=\"font-weight: 400;\">when<\/span><span style=\"font-weight: 400;\"> expression, gi\u00fap \u0111\u1ea3m b\u1ea3o r\u1eb1ng t\u1ea5t c\u1ea3 tr\u01b0\u1eddng h\u1ee3p \u0111\u1ec1u \u0111\u01b0\u1ee3c x\u1eed l\u00fd.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng ki\u1ec3m so\u00e1t \u0111\u01b0\u1ee3c c\u00e1c tr\u01b0\u1eddng h\u1ee3p ph\u1ee9c t\u1ea1p nh\u01b0 <\/span><span style=\"font-weight: 400;\">sealed class<\/span><span style=\"font-weight: 400;\">, ch\u1ec9 c\u00f3 th\u1ec3 l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c gi\u00e1 tr\u1ecb c\u1ed1 \u0111\u1ecbnh.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Khi b\u1ea1n c\u1ea7n m\u00f4 h\u00ecnh h\u00f3a c\u00e1c tr\u1ea1ng th\u00e1i ho\u1eb7c lo\u1ea1i d\u1eef li\u1ec7u c\u00f3 th\u1ec3 thay \u0111\u1ed5i v\u1edbi c\u00e1c h\u00e0nh vi k\u1ebf th\u1eeba.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Khi b\u1ea1n ch\u1ec9 c\u1ea7n m\u1ed9t danh s\u00e1ch c\u00e1c gi\u00e1 tr\u1ecb kh\u00f4ng thay \u0111\u1ed5i, v\u00ed d\u1ee5 nh\u01b0 c\u00e1c lo\u1ea1i \u01b0u ti\u00ean, m\u1ee9c \u0111\u1ed9 l\u1ed7i, tr\u1ea1ng th\u00e1i c\u1ed1 \u0111\u1ecbnh.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><strong>C\u00e1c c\u00e1ch \u0111\u1ec3 t\u1ea1o m\u1ed9t class singleton trong Kotlin l\u00e0 g\u00ec?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, c\u00f3 m\u1ed9t s\u1ed1 c\u00e1ch \u0111\u1ec3 t\u1ea1o m\u1ed9t class singleton, gi\u00fap \u0111\u1ea3m b\u1ea3o r\u1eb1ng ch\u1ec9 c\u00f3 m\u1ed9t instance duy nh\u1ea5t c\u1ee7a class \u0111\u00f3 trong su\u1ed1t v\u00f2ng \u0111\u1eddi c\u1ee7a \u1ee9ng d\u1ee5ng. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 ba c\u00e1ch ph\u1ed5 bi\u1ebfn:<\/span><\/p>\n<h4><strong>S\u1eed d\u1ee5ng t\u1eeb kh\u00f3a Object<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">C\u00e1ch \u0111\u01a1n gi\u1ea3n v\u00e0 ph\u1ed5 bi\u1ebfn nh\u1ea5t \u0111\u1ec3 t\u1ea1o m\u1ed9t singleton trong Kotlin l\u00e0 s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><strong>object<\/strong><span style=\"font-weight: 400;\">. T\u1eeb kh\u00f3a n\u00e0y s\u1ebd t\u1ea1o ra m\u1ed9t class singleton t\u1ef1 \u0111\u1ed9ng v\u00e0 an to\u00e0n v\u1edbi thread.<\/span><\/p>\n<pre>object MySingleton {\r\n  val data = \"Some Data\"\r\n  fun printData() {\r\n    println(data)\r\n  }\r\n}<\/pre>\n<h4><strong>S\u1eed d\u1ee5ng companion Object<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng <strong>companion object<\/strong> \u0111\u1ec3 t\u1ea1o m\u1ed9t singleton b\u00ean trong m\u1ed9t class. Tuy nhi\u00ean, c\u00e1ch n\u00e0y kh\u00e1c v\u1edbi vi\u1ec7c s\u1eed d\u1ee5ng object v\u00ec n\u00f3 cho ph\u00e9p b\u1ea1n gi\u1eef c\u00e1c th\u00e0nh ph\u1ea7n t\u0129nh b\u00ean trong class.<\/span><\/p>\n<pre>class MySingleton {\r\n  companion object {\r\n    val instance = MySingleton()\r\n  }\r\n}<\/pre>\n<h4><strong>Lazily Initialized Singleton<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 \u0111\u1ea3m b\u1ea3o r\u1eb1ng singleton ch\u1ec9 \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o khi c\u1ea7n thi\u1ebft (lazy initialization), b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng <\/span><strong>lazy<\/strong><span style=\"font-weight: 400;\"> \u0111\u1ec3 kh\u1edfi t\u1ea1o instance m\u1ed9t c\u00e1ch l\u01b0\u1eddi bi\u1ebfng.<\/span><\/p>\n<pre>class MySingleton private constructor() {\r\n  companion object {\r\n    val instance by lazy { MySingleton() }\r\n  }\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">lazy<\/span><span style=\"font-weight: 400;\"> \u0111\u1ea3m b\u1ea3o r\u1eb1ng instance c\u1ee7a singleton s\u1ebd ch\u1ec9 \u0111\u01b0\u1ee3c t\u1ea1o ra khi l\u1ea7n \u0111\u1ea7u ti\u00ean \u0111\u01b0\u1ee3c y\u00eau c\u1ea7u v\u00e0 v\u1eabn gi\u1eef t\u00ednh an to\u00e0n v\u1edbi nhi\u1ec1u thread.<\/span><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">T\u00ecm hi\u1ec3u th\u00eam v\u1ec1 singleton trong Kotlin: <\/span><a href=\"https:\/\/in-kotlin.com\/design-patterns\/singleton\/\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">https:\/\/in-kotlin.com\/design-patterns\/singleton\/<\/span><\/a><\/p><\/blockquote>\n<h3><strong>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 khai b\u00e1o m\u1ed9t h\u00e0m trong Kotlin c\u00f3 tham s\u1ed1 m\u1eb7c \u0111\u1ecbnh?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, b\u1ea1n c\u00f3 th\u1ec3 khai b\u00e1o m\u1ed9t h\u00e0m v\u1edbi tham s\u1ed1 m\u1eb7c \u0111\u1ecbnh b\u1eb1ng c\u00e1ch ch\u1ec9 \u0111\u1ecbnh gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh cho c\u00e1c tham s\u1ed1 trong ph\u1ea7n \u0111\u1ecbnh ngh\u0129a h\u00e0m. \u0110i\u1ec1u n\u00e0y cho ph\u00e9p b\u1ea1n g\u1ecdi h\u00e0m m\u00e0 kh\u00f4ng c\u1ea7n truy\u1ec1n gi\u00e1 tr\u1ecb cho c\u00e1c tham s\u1ed1 \u0111\u00f3, v\u00e0 n\u1ebfu kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb n\u00e0o \u0111\u01b0\u1ee3c truy\u1ec1n, gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh s\u1ebd \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00fa ph\u00e1p khai b\u00e1o h\u00e0m c\u00f3 tham s\u1ed1 m\u1eb7c \u0111\u1ecbnh:<\/span><\/p>\n<pre>fun functionName (param1: Type1 = defaultValuel, param2: Type2 = defaultValue2) {\r\n  \/\/ Th\u00e2n h\u00e0m\r\n}<\/pre>\n<h3><strong>inline<\/strong><span style=\"font-weight: 400;\"><strong> function trong Kotlin l\u00e0 g\u00ec, v\u00e0 khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng?<\/strong>\u00a0<\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Inline function trong Kotlin l\u00e0 m\u1ed9t t\u00ednh n\u0103ng gi\u00fap t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t khi s\u1eed d\u1ee5ng c\u00e1c h\u00e0m b\u1eadc cao (higher-order functions).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Khi m\u1ed9t h\u00e0m \u0111\u01b0\u1ee3c \u0111\u00e1nh d\u1ea5u l\u00e0 <\/span><span style=\"font-weight: 400;\">inline<\/span><span style=\"font-weight: 400;\">, Kotlin s\u1ebd thay th\u1ebf l\u1eddi g\u1ecdi h\u00e0m b\u1eb1ng m\u00e3 c\u1ee7a ch\u00ednh h\u00e0m \u0111\u00f3 t\u1ea1i ch\u1ed7, thay v\u00ec t\u1ea1o ra m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng h\u00e0m m\u1edbi v\u00e0 g\u1ecdi n\u00f3. \u0110i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 gi\u00fap gi\u1ea3m chi ph\u00ed hi\u1ec7u su\u1ea5t, \u0111\u1eb7c bi\u1ec7t trong tr\u01b0\u1eddng h\u1ee3p h\u00e0m \u0111\u01b0\u1ee3c g\u1ecdi nhi\u1ec1u l\u1ea7n ho\u1eb7c h\u00e0m c\u00f3 c\u00e1c h\u00e0m con b\u00ean trong.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1eb7c \u0111i\u1ec3m c\u1ee7a inline functions:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Gi\u1ea3m chi ph\u00ed g\u1ecdi h\u00e0m: Khi m\u1ed9t h\u00e0m l\u00e0 <\/span><span style=\"font-weight: 400;\">inline<\/span><span style=\"font-weight: 400;\">, Kotlin s\u1ebd &#8220;nh\u00fang&#8221; m\u00e3 c\u1ee7a h\u00e0m v\u00e0o c\u00e1c v\u1ecb tr\u00ed g\u1ecdi h\u00e0m, thay v\u00ec t\u1ea1o ra m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng h\u00e0m v\u00e0 g\u1ecdi n\u00f3 theo c\u00e1ch th\u00f4ng th\u01b0\u1eddng. \u0110i\u1ec1u n\u00e0y gi\u00fap gi\u1ea3m overhead c\u1ee7a vi\u1ec7c g\u1ecdi h\u00e0m.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u1ed1i \u01b0u h\u00f3a v\u1edbi c\u00e1c h\u00e0m b\u1eadc cao: H\u00e0m b\u1eadc cao l\u00e0 nh\u1eefng h\u00e0m nh\u1eadn c\u00e1c h\u00e0m kh\u00e1c l\u00e0m \u0111\u1ed1i s\u1ed1. Vi\u1ec7c s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">inline<\/span><span style=\"font-weight: 400;\"> c\u00f3 th\u1ec3 gi\u00fap t\u1ed1i \u01b0u vi\u1ec7c truy\u1ec1n c\u00e1c lambda expressions v\u00e0o h\u00e0m m\u00e0 kh\u00f4ng t\u1ea1o ra \u0111\u1ed1i t\u01b0\u1ee3ng lambda m\u1edbi, gi\u1ea3m thi\u1ec3u chi ph\u00ed b\u1ed9 nh\u1edb.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng trong c\u00e1c h\u00e0m ch\u1ee9a lambda: M\u1ed9t trong nh\u1eefng l\u00fd do ch\u00ednh \u0111\u1ec3 s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">inline<\/span><span style=\"font-weight: 400;\"> l\u00e0 khi h\u00e0m nh\u1eadn c\u00e1c tham s\u1ed1 lambda. N\u1ebfu kh\u00f4ng s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">inline<\/span><span style=\"font-weight: 400;\">, m\u1ed7i l\u1ea7n g\u1ecdi h\u00e0m v\u1edbi lambda, Kotlin s\u1ebd ph\u1ea3i t\u1ea1o m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng lambda, \u0111i\u1ec1u n\u00e0y g\u00e2y t\u1ed1n b\u1ed9 nh\u1edb v\u00e0 l\u00e0m ch\u1eadm hi\u1ec7u su\u1ea5t.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 v\u1ec1 Inline function trong Kotlin:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">inline fun &lt;T&gt; performAction(action: () -&gt; T): T {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0println(\"Before action\")<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0val result = action()\u00a0 \/\/ G\u1ecdi action, m\u1ed9t lambda<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0println(\"After action\")<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0return result<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">fun main() {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0val result = performAction {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0println(\"Action is being performed\")<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a042\u00a0 \/\/ Tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb t\u1eeb lambda<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0println(\"Result: $result\")<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Cau_hoi_phong_van_Kotlin_ve_lap_trinh_huong_doi_tuong_OOP_trong_Kotlin\"><\/span><strong>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin v\u1ec1 l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng (OOP) trong Kotlin<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u0110\u1ecdc th\u00eam:<\/p>\n<ul>\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<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<h3><strong>Kotlin c\u00f3 h\u1ed7 tr\u1ee3 l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng nh\u01b0 th\u1ebf n\u00e0o?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Kotlin h\u1ed7 tr\u1ee3 l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng (OOP) r\u1ea5t m\u1ea1nh m\u1ebd v\u1edbi c\u00e1c t\u00ednh n\u0103ng c\u01a1 b\u1ea3n nh\u01b0:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Class v\u00e0 Object: Kotlin cho ph\u00e9p khai b\u00e1o class v\u00e0 t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng, \u0111\u1ed3ng th\u1eddi h\u1ed7 tr\u1ee3 singleton b\u1eb1ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">object<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">K\u1ebf th\u1eeba (Inheritance): Kotlin h\u1ed7 tr\u1ee3 k\u1ebf th\u1eeba, nh\u01b0ng c\u00e1c class m\u1eb7c \u0111\u1ecbnh l\u00e0 <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\">, b\u1ea1n c\u1ea7n d\u00f9ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">open<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 cho ph\u00e9p k\u1ebf th\u1eeba.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Polymorphism (\u0110a h\u00ecnh): Kotlin cho ph\u00e9p ghi \u0111\u00e8 ph\u01b0\u01a1ng th\u1ee9c v\u1edbi t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">override<\/span><span style=\"font-weight: 400;\">, gi\u00fap tri\u1ec3n khai \u0111a h\u00ecnh.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Interface: Kotlin h\u1ed7 tr\u1ee3 giao di\u1ec7n v\u1edbi c\u00e1c ph\u01b0\u01a1ng th\u1ee9c c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c c\u00e0i \u0111\u1eb7t trong c\u00e1c l\u1edbp con.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Encapsulation (\u0110\u00f3ng g\u00f3i): S\u1eed d\u1ee5ng c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c v\u1edbi c\u00e1c visibility modifiers nh\u01b0 <\/span><span style=\"font-weight: 400;\">private<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">protected<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">internal<\/span><span style=\"font-weight: 400;\">, v\u00e0 <\/span><span style=\"font-weight: 400;\">public<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Data Classes: Cung c\u1ea5p <\/span><span style=\"font-weight: 400;\">data class<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 t\u1ef1 \u0111\u1ed9ng t\u1ea1o c\u00e1c ph\u01b0\u01a1ng th\u1ee9c nh\u01b0 <\/span><span style=\"font-weight: 400;\">toString()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">equals()<\/span><span style=\"font-weight: 400;\">, v\u00e0 <\/span><span style=\"font-weight: 400;\">hashCode()<\/span><span style=\"font-weight: 400;\"> cho c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Kotlin gi\u00fap l\u1eadp tr\u00ecnh vi\u00ean d\u1ec5 d\u00e0ng s\u1eed d\u1ee5ng c\u00e1c t\u00ednh n\u0103ng OOP v\u00e0 c\u0169ng h\u1ed7 tr\u1ee3 k\u1ebft h\u1ee3p l\u1eadp tr\u00ecnh h\u00e0m, mang l\u1ea1i s\u1ef1 linh ho\u1ea1t v\u00e0 hi\u1ec7u qu\u1ea3 trong vi\u1ec7c ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng.<\/span><\/p>\n<h3><strong>Interface trong Kotlin kh\u00e1c g\u00ec so v\u1edbi Java?<\/strong><\/h3>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">T\u00ednh n\u0103ng\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Java<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kotlin<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng th\u1ee9c c\u00f3 th\u00e2n h\u00e0m<\/span><\/td>\n<td><span style=\"font-weight: 400;\">T\u1eeb Java 8, s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">default<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 khai b\u00e1o ph\u01b0\u01a1ng th\u1ee9c v\u1edbi th\u00e2n h\u00e0m.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 khai b\u00e1o ph\u01b0\u01a1ng th\u1ee9c c\u00f3 th\u00e2n h\u00e0m tr\u1ef1c ti\u1ebfp trong interface m\u00e0 kh\u00f4ng c\u1ea7n t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">default<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Khai b\u00e1o implement interface<\/span><\/td>\n<td><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">implements<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng d\u1ea5u <\/span><span style=\"font-weight: 400;\">:<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 implement interface.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">K\u1ebf th\u1eeba nhi\u1ec1u interface<\/span><\/td>\n<td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 k\u1ebf th\u1eeba nhi\u1ec1u interface.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 k\u1ebf th\u1eeba nhi\u1ec1u interface.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng th\u1ee9c abstract m\u1eb7c \u0111\u1ecbnh<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00e1c ph\u01b0\u01a1ng th\u1ee9c trong interface m\u1eb7c \u0111\u1ecbnh l\u00e0 abstract (tr\u1eeb khi c\u00f3 t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">default<\/span><span style=\"font-weight: 400;\">).<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00e1c ph\u01b0\u01a1ng th\u1ee9c trong interface m\u1eb7c \u0111\u1ecbnh l\u00e0 abstract n\u1ebfu kh\u00f4ng c\u00f3 ph\u1ea7n th\u00e2n h\u00e0m. Kh\u00f4ng c\u1ea7n t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">abstract<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng th\u1ee9c v\u1edbi th\u00e2n h\u00e0m m\u1eb7c \u0111\u1ecbnh<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ph\u1ea3i d\u00f9ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">default<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 cung c\u1ea5p ph\u01b0\u01a1ng th\u1ee9c m\u1eb7c \u0111\u1ecbnh.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng c\u1ea7n t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">default<\/span><span style=\"font-weight: 400;\">, c\u00f3 th\u1ec3 cung c\u1ea5p ph\u01b0\u01a1ng th\u1ee9c m\u1eb7c \u0111\u1ecbnh tr\u1ef1c ti\u1ebfp trong interface.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><strong>data class l\u00e0 g\u00ec?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, data class l\u00e0 m\u1ed9t lo\u1ea1i class \u0111\u1eb7c bi\u1ec7t \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf \u0111\u1ec3 l\u01b0u tr\u1eef d\u1eef li\u1ec7u m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i vi\u1ebft c\u00e1c ph\u01b0\u01a1ng th\u1ee9c nh\u01b0 <\/span><span style=\"font-weight: 400;\">toString()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">equals()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">hashCode()<\/span><span style=\"font-weight: 400;\">, v\u00e0 <\/span><span style=\"font-weight: 400;\">copy()<\/span><span style=\"font-weight: 400;\">. Kotlin t\u1ef1 \u0111\u1ed9ng t\u1ea1o ra c\u00e1c ph\u01b0\u01a1ng th\u1ee9c n\u00e0y cho b\u1ea1n khi b\u1ea1n khai b\u00e1o m\u1ed9t class l\u00e0 <\/span><span style=\"font-weight: 400;\">data class<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00e1c t\u00ednh n\u0103ng c\u1ee7a data class:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u1ef1 \u0111\u1ed9ng t\u1ea1o c\u00e1c ph\u01b0\u01a1ng th\u1ee9c:<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">toString()<\/span><span style=\"font-weight: 400;\">: Tr\u1ea3 v\u1ec1 m\u1ed9t chu\u1ed7i \u0111\u1ea1i di\u1ec7n cho \u0111\u1ed1i t\u01b0\u1ee3ng (t\u00ean class v\u00e0 c\u00e1c thu\u1ed9c t\u00ednh c\u1ee7a n\u00f3).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">equals()<\/span><span style=\"font-weight: 400;\">: So s\u00e1nh gi\u00e1 tr\u1ecb c\u1ee7a c\u00e1c thu\u1ed9c t\u00ednh \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh xem hai \u0111\u1ed1i t\u01b0\u1ee3ng c\u00f3 b\u1eb1ng nhau kh\u00f4ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">hashCode()<\/span><span style=\"font-weight: 400;\">: T\u1ea1o m\u00e3 b\u0103m cho \u0111\u1ed1i t\u01b0\u1ee3ng d\u1ef1a tr\u00ean gi\u00e1 tr\u1ecb c\u1ee7a c\u00e1c thu\u1ed9c t\u00ednh.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400;\">copy()<\/span><span style=\"font-weight: 400;\">: T\u1ea1o b\u1ea3n sao c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng, c\u00f3 th\u1ec3 thay \u0111\u1ed5i m\u1ed9t ho\u1eb7c v\u00e0i thu\u1ed9c t\u00ednh c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng sao ch\u00e9p m\u00e0 kh\u00f4ng thay \u0111\u1ed5i \u0111\u1ed1i t\u01b0\u1ee3ng g\u1ed1c.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng trong l\u01b0u tr\u1eef d\u1eef li\u1ec7u: Data class r\u1ea5t h\u1eefu \u00edch khi b\u1ea1n ch\u1ec9 c\u1ea7n s\u1eed d\u1ee5ng \u0111\u1ed1i t\u01b0\u1ee3ng \u0111\u1ec3 l\u01b0u tr\u1eef d\u1eef li\u1ec7u m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i vi\u1ebft c\u00e1c ph\u01b0\u01a1ng th\u1ee9c so s\u00e1nh hay chuy\u1ec3n \u0111\u1ed5i th\u1ee7 c\u00f4ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u00ednh b\u1ea5t bi\u1ebfn (Immutability): C\u00e1c thu\u1ed9c t\u00ednh trong data class c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c khai b\u00e1o l\u00e0 <\/span><span style=\"font-weight: 400;\">val<\/span><span style=\"font-weight: 400;\"> (immutable) ho\u1eb7c <\/span><span style=\"font-weight: 400;\">var<\/span><span style=\"font-weight: 400;\"> (mutable). Tuy nhi\u00ean, trong nhi\u1ec1u tr\u01b0\u1eddng h\u1ee3p, b\u1ea1n s\u1ebd s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">val<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o t\u00ednh b\u1ea5t bi\u1ebfn c\u1ee7a d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ul>\n<h3><strong>T\u1ea1i sao data class h\u1eefu \u00edch trong Kotlin?<\/strong><\/h3>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Gi\u1ea3m m\u00e3 l\u1eb7p l\u1ea1i: B\u1ea1n kh\u00f4ng c\u1ea7n ph\u1ea3i vi\u1ebft c\u00e1c ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">toString()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">equals()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">hashCode()<\/span><span style=\"font-weight: 400;\">, v\u00e0 <\/span><span style=\"font-weight: 400;\">copy()<\/span><span style=\"font-weight: 400;\"> th\u1ee7 c\u00f4ng. Kotlin t\u1ef1 \u0111\u1ed9ng sinh ra c\u00e1c ph\u01b0\u01a1ng th\u1ee9c n\u00e0y cho b\u1ea1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u1ea3i thi\u1ec7n hi\u1ec7u qu\u1ea3 khi l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u: Data class th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c t\u00ecnh hu\u1ed1ng khi b\u1ea1n c\u1ea7n l\u01b0u tr\u1eef v\u00e0 x\u1eed l\u00fd d\u1eef li\u1ec7u (v\u00ed d\u1ee5: trong c\u00e1c \u1ee9ng d\u1ee5ng x\u1eed l\u00fd d\u1eef li\u1ec7u t\u1eeb API, c\u01a1 s\u1edf d\u1eef li\u1ec7u ho\u1eb7c l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u t\u1eeb c\u00e1c l\u1edbp d\u1eef li\u1ec7u).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng b\u1ea5t bi\u1ebfn d\u1ec5 d\u00e0ng: Data class gi\u00fap b\u1ea1n d\u1ec5 d\u00e0ng t\u1ea1o c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u00f3 t\u00ednh b\u1ea5t bi\u1ebfn, gi\u00fap \u0111\u1ea3m b\u1ea3o d\u1eef li\u1ec7u kh\u00f4ng b\u1ecb thay \u0111\u1ed5i m\u1ed9t c\u00e1ch b\u1ea5t ng\u1edd trong su\u1ed1t v\u00f2ng \u0111\u1eddi c\u1ee7a \u1ee9ng d\u1ee5ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng d\u1ec5 d\u00e0ng trong c\u00e1c c\u1ea5u tr\u00fac d\u1eef li\u1ec7u: V\u1edbi c\u00e1c ph\u01b0\u01a1ng th\u1ee9c nh\u01b0 <\/span><span style=\"font-weight: 400;\">copy()<\/span><span style=\"font-weight: 400;\"> v\u00e0 kh\u1ea3 n\u0103ng so s\u00e1nh c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng d\u1ec5 d\u00e0ng, data class r\u1ea5t h\u1eefu \u00edch trong c\u00e1c t\u00ecnh hu\u1ed1ng c\u1ea7n x\u1eed l\u00fd ho\u1eb7c truy\u1ec1n t\u1ea3i d\u1eef li\u1ec7u m\u00e0 kh\u00f4ng c\u1ea7n th\u00eam m\u00e3 ph\u1ee5 tr\u1ee3 ph\u1ee9c t\u1ea1p.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">T\u00f3m l\u1ea1i, data class trong Kotlin gi\u00fap gi\u1ea3m b\u1edbt m\u00e3 ngu\u1ed3n, t\u1ef1 \u0111\u1ed9ng t\u1ea1o c\u00e1c ph\u01b0\u01a1ng th\u1ee9c c\u1ea7n thi\u1ebft v\u00e0 l\u00e0m vi\u1ec7c hi\u1ec7u qu\u1ea3 h\u01a1n v\u1edbi d\u1eef li\u1ec7u, \u0111\u1eb7c bi\u1ec7t trong c\u00e1c t\u00ecnh hu\u1ed1ng khi b\u1ea1n ch\u1ec9 c\u1ea7n l\u01b0u tr\u1eef v\u00e0 x\u1eed l\u00fd th\u00f4ng tin m\u00e0 kh\u00f4ng c\u1ea7n t\u1ea1o c\u00e1c ph\u01b0\u01a1ng th\u1ee9c th\u1ee7 c\u00f4ng.<\/span><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">T\u00ecm hi\u1ec3u th\u00eam v\u1ec1 Data Class trong Kotlin: <\/span><a href=\"https:\/\/kotlinlang.org\/docs\/data-classes.html\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">https:\/\/kotlinlang.org\/docs\/data-classes.html<\/span><\/a><span style=\"font-weight: 400;\">\u00a0<\/span><\/p><\/blockquote>\n<h3><strong>C\u00f3 th\u1ec3 k\u1ebf th\u1eeba (inheritance) trong Kotlin nh\u01b0 th\u1ebf n\u00e0o?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, k\u1ebf th\u1eeba \u0111\u01b0\u1ee3c h\u1ed7 tr\u1ee3 th\u00f4ng qua t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">open<\/span><span style=\"font-weight: 400;\"> cho l\u1edbp cha, cho ph\u00e9p l\u1edbp con k\u1ebf th\u1eeba v\u00e0 ghi \u0111\u00e8 c\u00e1c ph\u01b0\u01a1ng th\u1ee9c b\u1eb1ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">override<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">M\u1eb7c \u0111\u1ecbnh, t\u1ea5t c\u1ea3 c\u00e1c l\u1edbp trong Kotlin l\u00e0 <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\">, ngh\u0129a l\u00e0 kh\u00f4ng th\u1ec3 k\u1ebf th\u1eeba tr\u1eeb khi l\u1edbp \u0111\u00f3 \u0111\u01b0\u1ee3c \u0111\u00e1nh d\u1ea5u <\/span><span style=\"font-weight: 400;\">open<\/span><span style=\"font-weight: 400;\">. Kotlin c\u0169ng h\u1ed7 tr\u1ee3 k\u1ebf th\u1eeba t\u1eeb nhi\u1ec1u interface, v\u00e0 c\u00e1c interface c\u00f3 th\u1ec3 ch\u1ee9a ph\u01b0\u01a1ng th\u1ee9c c\u00f3 ph\u1ea7n th\u00e2n h\u00e0m (default methods).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Tuy nhi\u00ean, Kotlin kh\u00f4ng cho ph\u00e9p k\u1ebf th\u1eeba t\u1eeb data class \u0111\u1ec3 tr\u00e1nh xung \u0111\u1ed9t v\u1edbi c\u00e1c ph\u01b0\u01a1ng th\u1ee9c t\u1ef1 \u0111\u1ed9ng sinh ra.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre>interface Animal {\r\n  fun sound() \/\/ Ph\u01b0\u01a1ng th\u1ee9c trong interface, kh\u00f4ng c\u00f3 th\u00e2n h\u00e0m\r\n}\r\n\r\ninterface Canine {\r\n  fun bark() {\r\n    println(\"Barking\")\r\n  }\r\n}\r\n\r\nclass Dog : Animal, Canine {\r\n  override fun sound() {\r\n    println(\"Woof\")\r\n  }\r\n}\r\n\r\nval dog = Dog ()\r\ndog.sound() \/\/ Output: Woof\r\ndog.bark() \/\/ Output: Barking<\/pre>\n<h3><strong>Gi\u1ea3i th\u00edch v\u1ec1 c\u00e1c modifiers trong Kotlin nh\u01b0 public, protected, internal, private<\/strong><\/h3>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">Modifiers<\/span><\/td>\n<td><span style=\"font-weight: 400;\">M\u00f4 t\u1ea3\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ph\u1ea1m vi truy c\u1eadp\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Public<\/span><\/td>\n<td><span style=\"font-weight: 400;\">M\u1eb7c \u0111\u1ecbnh, cho ph\u00e9p truy c\u1eadp t\u1eeb m\u1ecdi n\u01a1i.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 truy c\u1eadp t\u1eeb m\u1ecdi n\u01a1i, bao g\u1ed3m c\u00e1c module kh\u00e1c.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Khi b\u1ea1n mu\u1ed1n th\u00e0nh ph\u1ea7n c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp \u1edf b\u1ea5t k\u1ef3 \u0111\u00e2u trong d\u1ef1 \u00e1n ho\u1eb7c th\u01b0 vi\u1ec7n.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Protected<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ch\u1ec9 c\u00f3 th\u1ec3 truy c\u1eadp t\u1eeb l\u1edbp hi\u1ec7n t\u1ea1i v\u00e0 l\u1edbp con.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Truy c\u1eadp t\u1eeb l\u1edbp hi\u1ec7n t\u1ea1i v\u00e0 l\u1edbp con (k\u1ebf th\u1eeba).<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Khi b\u1ea1n mu\u1ed1n cho ph\u00e9p l\u1edbp con truy c\u1eadp th\u00e0nh ph\u1ea7n m\u00e0 kh\u00f4ng c\u00f4ng khai ra ngo\u00e0i l\u1edbp cha.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Internal<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ch\u1ec9 c\u00f3 th\u1ec3 truy c\u1eadp trong c\u00f9ng m\u1ed9t module (project ho\u1eb7c th\u01b0 vi\u1ec7n).<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Truy c\u1eadp trong c\u00f9ng m\u1ed9t module.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Khi b\u1ea1n mu\u1ed1n gi\u1edbi h\u1ea1n truy c\u1eadp th\u00e0nh ph\u1ea7n trong module hi\u1ec7n t\u1ea1i.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Private<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ch\u1ec9 c\u00f3 th\u1ec3 truy c\u1eadp trong l\u1edbp ho\u1eb7c file khai b\u00e1o th\u00e0nh ph\u1ea7n \u0111\u00f3.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Truy c\u1eadp trong l\u1edbp ho\u1eb7c file khai b\u00e1o.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Khi b\u1ea1n mu\u1ed1n \u1ea9n th\u00f4ng tin v\u00e0 ch\u1ec9 cho ph\u00e9p truy c\u1eadp trong l\u1edbp ho\u1eb7c file \u0111\u00f3.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><strong>Trong Kotlin, l\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 override m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, \u0111\u1ec3 override (ghi \u0111\u00e8) m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c t\u1eeb l\u1edbp cha, b\u1ea1n c\u1ea7n th\u1ef1c hi\u1ec7n c\u00e1c b\u01b0\u1edbc sau:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u00e1nh d\u1ea5u ph\u01b0\u01a1ng th\u1ee9c trong l\u1edbp cha b\u1eb1ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">open<\/span><span style=\"font-weight: 400;\"> (v\u00ec m\u1eb7c \u0111\u1ecbnh c\u00e1c ph\u01b0\u01a1ng th\u1ee9c trong Kotlin l\u00e0 <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\"> v\u00e0 kh\u00f4ng th\u1ec3 b\u1ecb ghi \u0111\u00e8).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">override<\/span><span style=\"font-weight: 400;\"> trong l\u1edbp con khi ghi \u0111\u00e8 ph\u01b0\u01a1ng th\u1ee9c \u0111\u00f3.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">C\u00e1ch th\u1ef1c hi\u1ec7n:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">L\u1edbp cha c\u1ea7n \u0111\u00e1nh d\u1ea5u ph\u01b0\u01a1ng th\u1ee9c l\u00e0 <\/span><span style=\"font-weight: 400;\">open<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 cho ph\u00e9p l\u1edbp con c\u00f3 th\u1ec3 ghi \u0111\u00e8.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">L\u1edbp con s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">override<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 ghi \u0111\u00e8 ph\u01b0\u01a1ng th\u1ee9c c\u1ee7a l\u1edbp cha.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre>\/\/ L\u1edbp cha v\u1edbi ph\u01b0\u01a1ng th\u1ee9c c\u00f3 th\u1ec3 ghi \u0111\u00e8\r\nopen class Animal {\r\n  open fun sound() {\r\n    println(\"Some sound\")\r\n  }\r\n}\r\n\r\n\/\/ L\u1edbp con ghi \u0111\u00e8 ph\u01b0\u01a1ng th\u1ee9c sound()\r\nclass Dog : Animal() {\r\n  override fun sound() {\r\n    println(\"Woof\")\r\n  }\r\n}\r\n\r\nfun main() {\r\n  val dog = Dog()\r\n  dog.sound() \/\/ Output: Woof\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">C\u00e1c \u0111i\u1ec3m c\u1ea7n l\u01b0u \u00fd:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">L\u1edbp cha: Ph\u01b0\u01a1ng th\u1ee9c ph\u1ea3i \u0111\u01b0\u1ee3c khai b\u00e1o v\u1edbi t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">open<\/span><span style=\"font-weight: 400;\"> (ho\u1eb7c c\u00f3 th\u1ec3 l\u00e0 <\/span><span style=\"font-weight: 400;\">abstract<\/span><span style=\"font-weight: 400;\"> n\u1ebfu l\u1edbp cha l\u00e0 l\u1edbp tr\u1eebu t\u01b0\u1ee3ng).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">L\u1edbp con: Khi ghi \u0111\u00e8, b\u1ea1n ph\u1ea3i s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">override<\/span><span style=\"font-weight: 400;\"> v\u00e0 ph\u01b0\u01a1ng th\u1ee9c ph\u1ea3i c\u00f3 c\u00f9ng t\u00ean v\u00e0 ki\u1ec3u d\u1eef li\u1ec7u tr\u1ea3 v\u1ec1 v\u1edbi ph\u01b0\u01a1ng th\u1ee9c trong l\u1edbp cha.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Kh\u00f4ng th\u1ec3 ghi \u0111\u00e8 n\u1ebfu l\u1edbp cha kh\u00f4ng m\u1edf: N\u1ebfu ph\u01b0\u01a1ng th\u1ee9c trong l\u1edbp cha kh\u00f4ng c\u00f3 t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">open<\/span><span style=\"font-weight: 400;\">, b\u1ea1n kh\u00f4ng th\u1ec3 ghi \u0111\u00e8 n\u00f3 trong l\u1edbp con.<\/span><\/li>\n<\/ol>\n<h3><strong>Ph\u00e2n bi\u1ec7t abstract class v\u00e0 interface trong Kotlin<\/strong><\/h3>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">Ti\u00eau ch\u00ed\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Abstract Class<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Interface<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Kh\u1ea3 n\u0103ng k\u1ebf th\u1eeba<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 k\u1ebf th\u1eeba m\u1ed9t l\u1edbp (ch\u1ec9 m\u1ed9t l\u1edbp duy nh\u1ea5t).<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 k\u1ebf th\u1eeba nhi\u1ec1u interface c\u00f9ng l\u00fac.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">C\u00e1c ph\u01b0\u01a1ng th\u1ee9c c\u00f3 th\u00e2n<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 ch\u1ee9a ph\u01b0\u01a1ng th\u1ee9c c\u00f3 th\u00e2n (c\u00f3 th\u1ec3 c\u00f3 m\u00e3 th\u1ef1c thi).<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 ch\u1ee9a ph\u01b0\u01a1ng th\u1ee9c c\u00f3 th\u00e2n (default methods).<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">C\u00e1c thu\u1ed9c t\u00ednh<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 c\u00f3 thu\u1ed9c t\u00ednh (bi\u1ebfn) v\u1edbi gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh ho\u1eb7c kh\u00f4ng.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 c\u00f3 thu\u1ed9c t\u00ednh, nh\u01b0ng t\u1ea5t c\u1ea3 \u0111\u1ec1u ph\u1ea3i l\u00e0 <\/span><span style=\"font-weight: 400;\">val<\/span><span style=\"font-weight: 400;\"> (ch\u1ec9 \u0111\u1ecdc).<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Kh\u1edfi t\u1ea1o<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 c\u00f3 constructor v\u00e0 kh\u1edfi t\u1ea1o thu\u1ed9c t\u00ednh trong constructor.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng c\u00f3 constructor.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">T\u00ednh k\u1ebf th\u1eeba<\/span><\/td>\n<td><span style=\"font-weight: 400;\">L\u1edbp k\u1ebf th\u1eeba t\u1eeb l\u1edbp tr\u1eebu t\u01b0\u1ee3ng c\u00f3 th\u1ec3 k\u1ebf th\u1eeba c\u00e1c ph\u01b0\u01a1ng th\u1ee9c v\u00e0 thu\u1ed9c t\u00ednh.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Interface ch\u1ec9 c\u00f3 th\u1ec3 k\u1ebf th\u1eeba c\u00e1c ph\u01b0\u01a1ng th\u1ee9c (kh\u00f4ng c\u00f3 thu\u1ed9c t\u00ednh).<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">M\u1ee5c \u0111\u00edch s\u1eed d\u1ee5ng<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Th\u01b0\u1eddng d\u00f9ng khi b\u1ea1n mu\u1ed1n t\u1ea1o m\u1ed9t l\u1edbp c\u01a1 s\u1edf v\u1edbi m\u00e3 th\u1ef1c thi chung cho c\u00e1c l\u1edbp con.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Th\u01b0\u1eddng d\u00f9ng \u0111\u1ec3 \u0111\u1ecbnh ngh\u0129a c\u00e1c h\u00e0nh vi m\u00e0 c\u00e1c l\u1edbp c\u00f3 th\u1ec3 tri\u1ec3n khai.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Ghi \u0111\u00e8 ph\u01b0\u01a1ng th\u1ee9c<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng th\u1ee9c c\u00f3 th\u1ec3 c\u00f3 ho\u1eb7c kh\u00f4ng c\u00f3 th\u00e2n, n\u1ebfu c\u00f3 th\u1ec3 ghi \u0111\u00e8 th\u00ec c\u1ea7n s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">override<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ph\u01b0\u01a1ng th\u1ee9c trong interface m\u1eb7c \u0111\u1ecbnh l\u00e0 <\/span><span style=\"font-weight: 400;\">abstract<\/span><span style=\"font-weight: 400;\"> (kh\u00f4ng c\u00f3 th\u00e2n), c\u00f3 th\u1ec3 cung c\u1ea5p ph\u1ea7n th\u00e2n v\u1edbi t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">default<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Ph\u1ea1m vi truy c\u1eadp<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 s\u1eed d\u1ee5ng b\u1ea5t k\u1ef3 visibility modifier n\u00e0o (<\/span><span style=\"font-weight: 400;\">public<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">protected<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">private<\/span><span style=\"font-weight: 400;\">).<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00e1c ph\u01b0\u01a1ng th\u1ee9c trong interface m\u1eb7c \u0111\u1ecbnh l\u00e0 <\/span><span style=\"font-weight: 400;\">public<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><strong>Gi\u1ea3i th\u00edch v\u1ec1 companion object v\u00e0 c\u00e1ch s\u1eed d\u1ee5ng n\u00f3 trong Kotlin<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, <\/span><span style=\"font-weight: 400;\">companion object<\/span><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m gi\u00fap \u0111\u1ecbnh ngh\u0129a c\u00e1c th\u00e0nh vi\u00ean (ph\u01b0\u01a1ng th\u1ee9c v\u00e0 thu\u1ed9c t\u00ednh) t\u0129nh trong l\u1edbp m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i t\u1ea1o ra m\u1ed9t th\u1ec3 hi\u1ec7n (instance) c\u1ee7a l\u1edbp \u0111\u00f3. \u0110i\u1ec1u n\u00e0y t\u01b0\u01a1ng t\u1ef1 nh\u01b0 c\u00e1c th\u00e0nh vi\u00ean <\/span><span style=\"font-weight: 400;\">static<\/span><span style=\"font-weight: 400;\"> trong Java.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">companion object<\/span><span style=\"font-weight: 400;\">, b\u1ea1n ch\u1ec9 c\u1ea7n khai b\u00e1o t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">companion object<\/span><span style=\"font-weight: 400;\"> trong l\u1edbp. C\u00e1c th\u00e0nh vi\u00ean trong <\/span><span style=\"font-weight: 400;\">companion object<\/span><span style=\"font-weight: 400;\"> c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp tr\u1ef1c ti\u1ebfp qua t\u00ean l\u1edbp, kh\u00f4ng c\u1ea7n t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre>class MyClass {\r\n  companion object {\r\n    const val CONSTANT_VALUE = 100\r\n\r\n    fun myFunction() {\r\n      println(\"This is a function in companion object.\")\r\n    }\r\n  }\r\n}\r\n\r\nfun main() {\r\n  println(MyClass.CONSTANT_VALUE) \/\/ Truy c\u1eadp constant\r\n  MyClass.myFunction() \/\/ G\u1ecdi ph\u01b0\u01a1ng th\u1ee9c\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">\u0110i\u1ec1u \u0111\u1eb7c bi\u1ec7t l\u00e0 b\u1ea1n c\u00f3 th\u1ec3 \u0111\u1eb7t t\u00ean cho <\/span><span style=\"font-weight: 400;\">companion object<\/span><span style=\"font-weight: 400;\"> n\u1ebfu c\u1ea7n, v\u00e0 n\u00f3 c\u0169ng c\u00f3 th\u1ec3 implement c\u00e1c interface, gi\u00fap t\u0103ng t\u00ednh linh ho\u1ea1t. V\u00ed d\u1ee5:<\/span><\/p>\n<pre>class MyClass {\r\n  companion object Factory {\r\n    fun create(): MyClass = MyClass()\r\n  }\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">T\u00f3m l\u1ea1i, <\/span><span style=\"font-weight: 400;\">companion object<\/span><span style=\"font-weight: 400;\"> cho ph\u00e9p \u0111\u1ecbnh ngh\u0129a c\u00e1c th\u00e0nh vi\u00ean t\u0129nh trong Kotlin, gi\u00fap m\u00e3 ngu\u1ed3n g\u1ecdn g\u00e0ng v\u00e0 d\u1ec5 qu\u1ea3n l\u00fd h\u01a1n so v\u1edbi vi\u1ec7c s\u1eed d\u1ee5ng c\u00e1c th\u00e0nh vi\u00ean <\/span><span style=\"font-weight: 400;\">static<\/span><span style=\"font-weight: 400;\"> trong Java.<\/span><\/p>\n<h3><strong>Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng open keyword trong Kotlin?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">open<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng khi b\u1ea1n mu\u1ed1n m\u1ed9t l\u1edbp, ph\u01b0\u01a1ng th\u1ee9c ho\u1eb7c thu\u1ed9c t\u00ednh c\u00f3 th\u1ec3 b\u1ecb k\u1ebf th\u1eeba ho\u1eb7c ghi \u0111\u00e8 trong c\u00e1c l\u1edbp con. M\u1eb7c \u0111\u1ecbnh, Kotlin coi c\u00e1c l\u1edbp v\u00e0 ph\u01b0\u01a1ng th\u1ee9c l\u00e0 <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\">, t\u1ee9c l\u00e0 kh\u00f4ng th\u1ec3 b\u1ecb k\u1ebf th\u1eeba ho\u1eb7c ghi \u0111\u00e8, tr\u1eeb khi b\u1ea1n khai b\u00e1o ch\u00fang l\u00e0 <\/span><span style=\"font-weight: 400;\">open<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">B\u1ea1n n\u00ean s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">open<\/span><span style=\"font-weight: 400;\"> trong c\u00e1c tr\u01b0\u1eddng h\u1ee3p sau:<\/span><\/p>\n<h4><strong>Khi b\u1ea1n mu\u1ed1n l\u1edbp c\u00f3 th\u1ec3 b\u1ecb k\u1ebf th\u1eeba<\/strong><\/h4>\n<p>N\u1ebfu b\u1ea1n c\u00f3 m\u1ed9t l\u1edbp v\u00e0 mu\u1ed1n cho ph\u00e9p c\u00e1c l\u1edbp kh\u00e1c k\u1ebf th\u1eeba t\u1eeb l\u1edbp \u0111\u00f3, b\u1ea1n c\u1ea7n \u0111\u00e1nh d\u1ea5u l\u1edbp v\u1edbi open.<\/p>\n<pre>open class Animal {\r\n  fun eat() {\r\n    println(\"Eating food\")\r\n  }\r\n}<\/pre>\n<h4><strong>Khi b\u1ea1n mu\u1ed1n ph\u01b0\u01a1ng th\u1ee9c c\u00f3 th\u1ec3 b\u1ecb ghi \u0111\u00e8<\/strong><\/h4>\n<p>N\u1ebfu b\u1ea1n c\u00f3 m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c trong l\u1edbp cha v\u00e0 mu\u1ed1n n\u00f3 c\u00f3 th\u1ec3 b\u1ecb l\u1edbp con ghi \u0111\u00e8, b\u1ea1n ph\u1ea3i \u0111\u00e1nh d\u1ea5u ph\u01b0\u01a1ng th\u1ee9c \u0111\u00f3 v\u1edbi open.<\/p>\n<pre>open class Animal {\r\n  open fun sound() {\r\n    println(\"Some sound\")\r\n  }\r\n}<\/pre>\n<h4><strong>Khi b\u1ea1n mu\u1ed1n thu\u1ed9c t\u00ednh c\u00f3 th\u1ec3 b\u1ecb ghi \u0111\u00e8<\/strong><\/h4>\n<p>Gi\u1ed1ng nh\u01b0 ph\u01b0\u01a1ng th\u1ee9c, n\u1ebfu b\u1ea1n mu\u1ed1n m\u1ed9t thu\u1ed9c t\u00ednh trong l\u1edbp cha c\u00f3 th\u1ec3 b\u1ecb l\u1edbp con ghi \u0111\u00e8, b\u1ea1n c\u1ea7n \u0111\u00e1nh d\u1ea5u thu\u1ed9c t\u00ednh \u0111\u00f3 v\u1edbi open.<\/p>\n<pre>open class Animal {\r\n  open val name: String = \"Animal\"\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">T\u00f3m l\u1ea1i, b\u1ea1n s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">open<\/span><span style=\"font-weight: 400;\"> khi c\u1ea7n cho ph\u00e9p k\u1ebf th\u1eeba ho\u1eb7c ghi \u0111\u00e8 trong Kotlin, v\u00ec m\u1eb7c \u0111\u1ecbnh m\u1ecdi th\u1ee9 \u0111\u1ec1u l\u00e0 <\/span><span style=\"font-weight: 400;\">final<\/span><span style=\"font-weight: 400;\"> v\u00e0 kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i.<\/span><\/p>\n<h3><strong>K\u1ec3 t\u00ean c\u00e1c c\u00e1ch \u0111\u1ec3 s\u1eed d\u1ee5ng extension functions trong Kotlin<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, extension functions cho ph\u00e9p m\u1edf r\u1ed9ng c\u00e1c l\u1edbp hi\u1ec7n c\u00f3 m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i thay \u0111\u1ed5i m\u00e3 ngu\u1ed3n c\u1ee7a l\u1edbp \u0111\u00f3. C\u00f3 m\u1ed9t s\u1ed1 c\u00e1ch s\u1eed d\u1ee5ng extension functions trong Kotlin, bao g\u1ed3m:<\/span><\/p>\n<h4><strong>Extension Function cho L\u1edbp Hi\u1ec7n C\u00f3<\/strong><\/h4>\n<p>\u0110\u00e2y l\u00e0 c\u00e1ch s\u1eed d\u1ee5ng ph\u1ed5 bi\u1ebfn nh\u1ea5t, cho ph\u00e9p b\u1ea1n th\u00eam c\u00e1c ph\u01b0\u01a1ng th\u1ee9c v\u00e0o l\u1edbp m\u00e0 kh\u00f4ng c\u1ea7n k\u1ebf th\u1eeba l\u1edbp \u0111\u00f3.<\/p>\n<pre>fun String.reverse(): String {\r\n  return this.reversed()\r\n}\r\nval reversedString = \"Hello\". reverse() \/\/ Output: olleH<\/pre>\n<h4><strong>Extension Function cho \u0110\u1ed1i T\u01b0\u1ee3ng c\u1ee7a L\u1edbp<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng extension function cho c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a l\u1edbp m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i t\u1ea1o l\u1edbp con.<\/span><\/p>\n<pre>fun Int.isEven(): Boolean {\r\n  return this % 2 == 0\r\n}\r\nval number = 4\r\nprintln(number.isEven()) \/\/ Output: true<\/pre>\n<h4><span style=\"font-weight: 400;\"><strong>Extension Function cho Interface<\/strong><\/span><\/h4>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u0169ng c\u00f3 th\u1ec3 \u0111\u1ecbnh ngh\u0129a extension function cho c\u00e1c interface, gi\u00fap th\u00eam ch\u1ee9c n\u0103ng v\u00e0o interface m\u00e0 kh\u00f4ng c\u1ea7n thay \u0111\u1ed5i m\u00e3 c\u1ee7a interface \u0111\u00f3.<\/span><\/p>\n<pre>interface Animal {\r\n  fun sound(): String\r\n}\r\n\r\nfun Animal.isLoud(): Boolean {\r\n  return this.sound().length &gt; 5\r\n}<\/pre>\n<h4><span style=\"font-weight: 400;\"><strong>Extension Property<\/strong><\/span><\/h4>\n<p><span style=\"font-weight: 400;\">Extension functions kh\u00f4ng ch\u1ec9 c\u00f3 th\u1ec3 l\u00e0 ph\u01b0\u01a1ng th\u1ee9c m\u00e0 c\u00f2n c\u00f3 th\u1ec3 l\u00e0 thu\u1ed9c t\u00ednh (property). B\u1ea1n c\u00f3 th\u1ec3 \u0111\u1ecbnh ngh\u0129a c\u00e1c thu\u1ed9c t\u00ednh m\u1edf r\u1ed9ng cho l\u1edbp hi\u1ec7n c\u00f3.<\/span><\/p>\n<pre>val String.firstChar: Char\r\n  get() = this[0]\r\nval name = \"Kotlin\"\r\nprintln(name.firstChar) \/\/ Output: K<\/pre>\n<h4><span style=\"font-weight: 400;\"><strong>Extension Function cho Collection<\/strong><\/span><\/h4>\n<p><span style=\"font-weight: 400;\">Extension functions c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 m\u1edf r\u1ed9ng c\u00e1c l\u1edbp collection 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;\">, ho\u1eb7c <\/span><span style=\"font-weight: 400;\">Map<\/span><span style=\"font-weight: 400;\">, gi\u00fap th\u00eam c\u00e1c ch\u1ee9c n\u0103ng h\u1eefu \u00edch v\u00e0o c\u00e1c collection c\u00f3 s\u1eb5n.<\/span><\/p>\n<pre>fun List&lt;Int&gt;.sumOfSquares(): Int {\r\n  return this.sumOf { it * it }\r\n}\r\nval numbers = listOf(1, 2, 3)\r\nprintln(numbers.sumOfSquares()) \/\/ Output: 14<\/pre>\n<p><span style=\"font-weight: 400;\">Extension functions r\u1ea5t m\u1ea1nh m\u1ebd v\u00e0 gi\u00fap m\u00e3 ngu\u1ed3n tr\u1edf n\u00ean g\u1ecdn g\u00e0ng v\u00e0 d\u1ec5 b\u1ea3o tr\u00ec m\u00e0 kh\u00f4ng c\u1ea7n thay \u0111\u1ed5i c\u00e1c l\u1edbp ban \u0111\u1ea7u.<\/span><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">T\u00ecm hi\u1ec3u th\u00eam v\u1ec1 Extension trong Kotlin: <\/span><a href=\"https:\/\/kotlinlang.org\/docs\/extensions.html\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">https:\/\/kotlinlang.org\/docs\/extensions.html<\/span><\/a><\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Cau_hoi_phong_van_Kotlin_ve_xu_ly_bat_dong_bo_va_luong_du_lieu_Concurrency\"><\/span><strong>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin v\u1ec1 x\u1eed l\u00fd b\u1ea5t \u0111\u1ed3ng b\u1ed9 v\u00e0 lu\u1ed3ng d\u1eef li\u1ec7u (Concurrency)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><strong>Kotlin h\u1ed7 tr\u1ee3 c\u00e1c ph\u01b0\u01a1ng th\u1ee9c x\u1eed l\u00fd b\u1ea5t \u0111\u1ed3ng b\u1ed9 n\u00e0o?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, c\u00f3 ba ph\u01b0\u01a1ng th\u1ee9c ch\u00ednh h\u1ed7 tr\u1ee3 x\u1eed l\u00fd b\u1ea5t \u0111\u1ed3ng b\u1ed9:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Coroutines: \u0110\u00e2y l\u00e0 ph\u01b0\u01a1ng ph\u00e1p \u0111\u01b0\u1ee3c Kotlin h\u1ed7 tr\u1ee3 natively. Coroutines gi\u00fap vi\u1ebft code b\u1ea5t \u0111\u1ed3ng b\u1ed9 theo ki\u1ec3u tu\u1ea7n t\u1ef1, d\u1ec5 hi\u1ec3u v\u00e0 d\u1ec5 b\u1ea3o tr\u00ec. Th\u01b0 vi\u1ec7n <\/span><span style=\"font-weight: 400;\">kotlinx.coroutines<\/span><span style=\"font-weight: 400;\"> cung c\u1ea5p c\u00e1c c\u00f4ng c\u1ee5 nh\u01b0 <\/span><span style=\"font-weight: 400;\">launch<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">async<\/span><span style=\"font-weight: 400;\">, v\u00e0 c\u00e1c h\u00e0m suspend (<\/span><span style=\"font-weight: 400;\">suspend functions<\/span><span style=\"font-weight: 400;\">), gi\u00fap qu\u1ea3n l\u00fd lu\u1ed3ng c\u00f4ng vi\u1ec7c m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3 m\u00e0 kh\u00f4ng c\u1ea7n s\u1eed d\u1ee5ng qu\u00e1 nhi\u1ec1u callback.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Reactive Programming: V\u1edbi c\u00e1c th\u01b0 vi\u1ec7n nh\u01b0 RxJava ho\u1eb7c Flow c\u1ee7a Kotlin, l\u1eadp tr\u00ecnh vi\u00ean c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n x\u1eed l\u00fd b\u1ea5t \u0111\u1ed3ng b\u1ed9 theo m\u00f4 h\u00ecnh ph\u1ea3n \u1ee9ng. Flow, \u0111\u01b0\u1ee3c t\u00edch h\u1ee3p trong Kotlin, s\u1eed d\u1ee5ng c\u00e1c h\u00e0m <\/span><span style=\"font-weight: 400;\">collect<\/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;\">filter<\/span><span style=\"font-weight: 400;\">, cho ph\u00e9p l\u1eadp tr\u00ecnh vi\u00ean x\u1eed l\u00fd lu\u1ed3ng d\u1eef li\u1ec7u kh\u00f4ng \u0111\u1ed3ng b\u1ed9, r\u1ea5t h\u1eefu \u00edch khi l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u th\u1eddi gian th\u1ef1c ho\u1eb7c stream data.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Callback: \u0110\u00e2y l\u00e0 ph\u01b0\u01a1ng ph\u00e1p truy\u1ec1n th\u1ed1ng \u0111\u1ec3 x\u1eed l\u00fd b\u1ea5t \u0111\u1ed3ng b\u1ed9. D\u00f9 Kotlin cung c\u1ea5p c\u00e1c c\u00f4ng c\u1ee5 ti\u00ean ti\u1ebfn h\u01a1n, callback v\u1eabn \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng ph\u1ed5 bi\u1ebfn trong m\u1ed9t s\u1ed1 tr\u01b0\u1eddng h\u1ee3p nh\u01b0 t\u01b0\u01a1ng t\u00e1c v\u1edbi c\u00e1c API c\u0169 ho\u1eb7c trong m\u1ed9t s\u1ed1 th\u01b0 vi\u1ec7n Java ch\u01b0a h\u1ed7 tr\u1ee3 Coroutines hay Reactive.<\/span><\/li>\n<\/ol>\n<h3><strong>Coroutines trong Kotlin l\u00e0 g\u00ec, v\u00e0 t\u1ea1i sao ch\u00fang quan tr\u1ecdng?<\/strong><\/h3>\n<p><a href=\"https:\/\/kotlinlang.org\/docs\/coroutines-overview.html\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">Coroutines<\/span><\/a><span style=\"font-weight: 400;\"> trong Kotlin l\u00e0 m\u1ed9t t\u00ednh n\u0103ng h\u1ed7 tr\u1ee3 l\u1eadp tr\u00ecnh b\u1ea5t \u0111\u1ed3ng b\u1ed9 (asynchronous) v\u00e0 x\u1eed l\u00fd \u0111a lu\u1ed3ng (multi-threading) m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3. Ch\u00fang gi\u00fap t\u1ea1o c\u00e1c t\u00e1c v\u1ee5 ch\u1ea1y n\u1ec1n m\u00e0 kh\u00f4ng l\u00e0m gi\u00e1n \u0111o\u1ea1n lu\u1ed3ng ch\u00ednh (main thread), t\u1eeb \u0111\u00f3 c\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t \u1ee9ng d\u1ee5ng, \u0111\u1eb7c bi\u1ec7t l\u00e0 trong c\u00e1c \u1ee9ng d\u1ee5ng Android n\u01a1i m\u00e0 vi\u1ec7c tr\u00e1nh g\u00e2y &#8220;lag&#8221; cho giao di\u1ec7n ng\u01b0\u1eddi d\u00f9ng l\u00e0 r\u1ea5t quan tr\u1ecdng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Coroutines n\u1ed5i b\u1eadt nh\u1edd t\u00ednh n\u0103ng suspend, gi\u00fap d\u1eebng m\u1ed9t h\u00e0m t\u1ea1i m\u1ed9t \u0111i\u1ec3m x\u00e1c \u0111\u1ecbnh v\u00e0 ti\u1ebfp t\u1ee5c t\u1eeb \u0111i\u1ec3m \u0111\u00f3 khi ho\u00e0n th\u00e0nh m\u1ed9t t\u00e1c v\u1ee5 b\u1ea5t \u0111\u1ed3ng b\u1ed9. \u0110i\u1ec1u n\u00e0y cho ph\u00e9p l\u1eadp tr\u00ecnh vi\u00ean vi\u1ebft m\u00e3 b\u1ea5t \u0111\u1ed3ng b\u1ed9 theo c\u00e1ch tuy\u1ebfn t\u00ednh, d\u1ec5 \u0111\u1ecdc v\u00e0 d\u1ec5 qu\u1ea3n l\u00fd h\u01a1n so v\u1edbi c\u00e1c ph\u01b0\u01a1ng ph\u00e1p truy\u1ec1n th\u1ed1ng nh\u01b0 callbacks ho\u1eb7c RxJava.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T\u1ea1i sao Coroutines quan tr\u1ecdng:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u01a1n gi\u1ea3n h\u00f3a m\u00e3 b\u1ea5t \u0111\u1ed3ng b\u1ed9: Coroutines gi\u00fap vi\u1ebft m\u00e3 x\u1eed l\u00fd b\u1ea5t \u0111\u1ed3ng b\u1ed9 d\u1ec5 hi\u1ec3u, r\u00f5 r\u00e0ng v\u00e0 duy tr\u00ec d\u1ec5 d\u00e0ng, gi\u1ed1ng nh\u01b0 m\u00e3 tu\u1ea7n t\u1ef1.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Hi\u1ec7u su\u1ea5t cao: Ch\u00fang ti\u00eau t\u1ed1n \u00edt t\u00e0i nguy\u00ean h\u01a1n c\u00e1c lu\u1ed3ng (threads) th\u00f4ng th\u01b0\u1eddng, nh\u1edd kh\u1ea3 n\u0103ng \u201c\u0111\u00ecnh ch\u1ec9\u201d t\u1ea1m th\u1eddi thay v\u00ec gi\u1eef cho thread ph\u1ea3i ch\u1ea1y li\u00ean t\u1ee5c.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">An to\u00e0n h\u01a1n cho UI: Coroutines gi\u00fap qu\u1ea3n l\u00fd lu\u1ed3ng ch\u00ednh hi\u1ec7u qu\u1ea3, gi\u1ea3m thi\u1ec3u nguy c\u01a1 g\u00e2y treo ho\u1eb7c &#8220;lag&#8221; giao di\u1ec7n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng t\u1ed1t: Coroutines h\u1ed7 tr\u1ee3 c\u00e1c t\u00e1c v\u1ee5 ph\u1ee9c t\u1ea1p nh\u01b0 c\u00e1c t\u00e1c v\u1ee5 song song ho\u1eb7c c\u00e1c ti\u1ebfn tr\u00ecnh c\u00f3 th\u1ec3 m\u1edf r\u1ed9ng, gi\u00fap \u1ee9ng d\u1ee5ng ho\u1ea1t \u0111\u1ed9ng \u1ed5n \u0111\u1ecbnh ngay c\u1ea3 khi c\u1ea7n x\u1eed l\u00fd c\u00e1c t\u00e1c v\u1ee5 l\u1edbn.<\/span><\/li>\n<\/ul>\n<h3><strong>Ph\u00e2n bi\u1ec7t gi\u1eefa launch v\u00e0 async<\/strong><span style=\"font-weight: 400;\"><strong> trong coroutines<\/strong><\/span><\/h3>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">Ti\u00eau ch\u00ed\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Launch\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Async<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">M\u1ee5c \u0111\u00edch<\/span><\/td>\n<td><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 kh\u1edfi ch\u1ea1y m\u1ed9t coroutine kh\u00f4ng tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 kh\u1edfi ch\u1ea1y m\u1ed9t coroutine c\u00f3 th\u1ec3 tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3 d\u01b0\u1edbi d\u1ea1ng <\/span><span style=\"font-weight: 400;\">Deferred<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Gi\u00e1 tr\u1ecb tr\u1ea3 v\u1ec1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Job<\/span><span style=\"font-weight: 400;\"> (kh\u00f4ng tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb).<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Deferred&lt;T&gt;<\/span><span style=\"font-weight: 400;\"> (tr\u1ea3 v\u1ec1 m\u1ed9t gi\u00e1 tr\u1ecb ki\u1ec3u <\/span><span style=\"font-weight: 400;\">T<\/span><span style=\"font-weight: 400;\"> th\u00f4ng qua <\/span><span style=\"font-weight: 400;\">await()<\/span><span style=\"font-weight: 400;\">).<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">C\u00e1ch s\u1eed d\u1ee5ng<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Th\u00edch h\u1ee3p cho c\u00e1c t\u00e1c v\u1ee5 kh\u00f4ng c\u1ea7n k\u1ebft qu\u1ea3, v\u00ed d\u1ee5 nh\u01b0 g\u1ecdi API m\u00e0 kh\u00f4ng quan t\u00e2m d\u1eef li\u1ec7u tr\u1ea3 v\u1ec1 ngay l\u1eadp t\u1ee9c.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Th\u00edch h\u1ee3p cho c\u00e1c t\u00e1c v\u1ee5 c\u1ea7n k\u1ebft qu\u1ea3, nh\u01b0 t\u1ea3i d\u1eef li\u1ec7u t\u1eeb m\u1ea1ng ho\u1eb7c x\u1eed l\u00fd t\u00ednh to\u00e1n \u0111\u1ec3 l\u1ea5y k\u1ebft qu\u1ea3.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">K\u1ebft th\u00fac coroutine<\/span><\/td>\n<td><span style=\"font-weight: 400;\">K\u1ebft th\u00fac sau khi coroutine ho\u00e0n th\u00e0nh; c\u00f3 th\u1ec3 h\u1ee7y v\u1edbi <\/span><span style=\"font-weight: 400;\">job.cancel()<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ho\u00e0n th\u00e0nh khi g\u1ecdi <\/span><span style=\"font-weight: 400;\">await()<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 l\u1ea5y k\u1ebft qu\u1ea3 ho\u1eb7c khi coroutine t\u1ef1 ho\u00e0n th\u00e0nh.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">X\u1eed l\u00fd ngo\u1ea1i l\u1ec7<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ngo\u1ea1i l\u1ec7 lan truy\u1ec1n ngay khi x\u1ea3y ra.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ngo\u1ea1i l\u1ec7 ch\u1ec9 lan truy\u1ec1n khi g\u1ecdi <\/span><span style=\"font-weight: 400;\">await()<\/span><span style=\"font-weight: 400;\"> (n\u1ebfu kh\u00f4ng g\u1ecdi <\/span><span style=\"font-weight: 400;\">await()<\/span><span style=\"font-weight: 400;\">, ngo\u1ea1i l\u1ec7 s\u1ebd b\u1ecb tr\u00ec ho\u00e3n).<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><strong>Gi\u1ea3i th\u00edch v\u1ec1 c\u00e1c Dispatcher<\/strong><span style=\"font-weight: 400;\"><strong> trong Kotlin Coroutine v\u00e0 c\u00e1c lo\u1ea1i ch\u00ednh c\u1ee7a ch\u00fang<\/strong><\/span><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin Coroutines, c\u00e1c Dispatcher \u0111\u00f3ng vai tr\u00f2 x\u00e1c \u0111\u1ecbnh thread n\u00e0o s\u1ebd ch\u1ea1y coroutine, qua \u0111\u00f3 gi\u00fap t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t v\u00e0 \u0111\u01a1n gi\u1ea3n h\u00f3a vi\u1ec7c qu\u1ea3n l\u00fd lu\u1ed3ng (thread).<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Lo\u1ea1i Dispatcher<\/b><\/td>\n<td><b>M\u00f4 t\u1ea3\u00a0<\/b><\/td>\n<td><b>S\u1eed d\u1ee5ng<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>Dispatchers.Default<\/b><\/td>\n<td><span style=\"font-weight: 400;\">Dispatcher d\u00e0nh cho c\u00e1c t\u00e1c v\u1ee5 CPU-bound, s\u1eed d\u1ee5ng c\u00e1c thread n\u1ec1n d\u1ef1a tr\u00ean s\u1ed1 l\u01b0\u1ee3ng l\u00f5i CPU \u0111\u1ec3 ph\u00e2n b\u1ed5 c\u00f4ng vi\u1ec7c.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">X\u1eed l\u00fd d\u1eef li\u1ec7u l\u1edbn, thu\u1eadt to\u00e1n ph\u1ee9c t\u1ea1p, c\u00e1c t\u00e1c v\u1ee5 y\u00eau c\u1ea7u x\u1eed l\u00fd m\u1ea1nh.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>Dispatchers.IO<\/b><\/td>\n<td><span style=\"font-weight: 400;\">Dispatcher t\u1ed1i \u01b0u cho c\u00e1c t\u00e1c v\u1ee5 I\/O-bound, cho ph\u00e9p t\u1ea1o nhi\u1ec1u thread h\u01a1n s\u1ed1 l\u00f5i CPU, h\u1ed7 tr\u1ee3 x\u1eed l\u00fd nhi\u1ec1u t\u00e1c v\u1ee5 I\/O song song.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\u0110\u1ecdc\/ghi t\u1eeb file, truy v\u1ea5n c\u01a1 s\u1edf d\u1eef li\u1ec7u, g\u1ecdi API.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>Dispatchers.Main<\/b><\/td>\n<td><span style=\"font-weight: 400;\">Dispatcher d\u00e0nh cho c\u00e1c t\u00e1c v\u1ee5 li\u00ean quan \u0111\u1ebfn UI, th\u01b0\u1eddng ch\u1ea1y tr\u00ean thread ch\u00ednh \u0111\u1ec3 c\u1eadp nh\u1eadt giao di\u1ec7n ng\u01b0\u1eddi d\u00f9ng m\u00e0 kh\u00f4ng l\u00e0m gi\u00e1n \u0111o\u1ea1n tr\u1ea3i nghi\u1ec7m c\u1ee7a ng\u01b0\u1eddi d\u00f9ng.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u1eadp nh\u1eadt giao di\u1ec7n ng\u01b0\u1eddi d\u00f9ng trong \u1ee9ng d\u1ee5ng Android.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>Dispatchers.Unconfined<\/b><\/td>\n<td><span style=\"font-weight: 400;\">Dispatcher ch\u1ea1y coroutine tr\u00ean thread hi\u1ec7n t\u1ea1i cho \u0111\u1ebfn khi g\u1eb7p m\u1ed9t suspend function, d\u1ec5 g\u00e2y kh\u00f3 \u0111o\u00e1n trong qu\u1ea3n l\u00fd lu\u1ed3ng v\u00e0 t\u00e0i nguy\u00ean.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Hi\u1ebfm khi s\u1eed d\u1ee5ng trong th\u1ef1c t\u1ebf do kh\u1ea3 n\u0103ng g\u00e2y kh\u00f3 \u0111o\u00e1n trong vi\u1ec7c qu\u1ea3n l\u00fd lu\u1ed3ng.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><strong>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 x\u1eed l\u00fd l\u1ed7i trong Kotlin coroutines?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, vi\u1ec7c x\u1eed l\u00fd l\u1ed7i trong coroutines c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n qua m\u1ed9t s\u1ed1 ph\u01b0\u01a1ng ph\u00e1p, t\u00f9y thu\u1ed9c v\u00e0o c\u00e1ch b\u1ea1n qu\u1ea3n l\u00fd c\u00e1c coroutine v\u00e0 lo\u1ea1i l\u1ed7i b\u1ea1n mu\u1ed1n x\u1eed l\u00fd. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t s\u1ed1 c\u00e1ch ti\u1ebfp c\u1eadn ph\u1ed5 bi\u1ebfn:<\/span><\/p>\n<h4><strong>S\u1eed d\u1ee5ng try-catch trong coroutine \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c l\u1ed7i li\u00ean quan \u0111\u1ebfn x\u1eed l\u00fd ngo\u1ea1i l\u1ec7 v\u00e0 ki\u1ec3m so\u00e1t lu\u1ed3ng x\u1eed l\u00fd<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng kh\u1ed1i <\/span><span style=\"font-weight: 400;\">try-catch<\/span><span style=\"font-weight: 400;\"> truy\u1ec1n th\u1ed1ng \u0111\u1ec3 b\u1eaft v\u00e0 x\u1eed l\u00fd c\u00e1c l\u1ed7i trong coroutine. \u0110i\u1ec1u n\u00e0y h\u1eefu \u00edch khi b\u1ea1n c\u1ea7n x\u1eed l\u00fd c\u00e1c ngo\u1ea1i l\u1ec7 t\u1eeb c\u00e1c t\u00e1c v\u1ee5 b\u1ea5t \u0111\u1ed3ng b\u1ed9 trong coroutine.<\/span><\/p>\n<pre>GlobalScope. launch {\r\n  try {\r\n      \/\/ G\u1ecdi h\u00e0m b\u1ea5t \u0111\u1ed3ng b\u1ed9 c\u00f3 th\u1ec3 g\u00e2y l\u1ed7i\r\n      val result = some Function ThatMightFail()\r\n  } catch (e: Exception) {\r\n      \/\/ X\u1eed l\u00fd l\u1ed7i\r\n      println(\"L\u1ed7i x\u1ea3y ra: ${e.message}\")\r\n  }\r\n}<\/pre>\n<h4><strong>S\u1eed d\u1ee5ng CoroutineExceptionHandler \u0111\u1ec3 x\u1eed l\u00fd ngo\u1ea1i l\u1ec7 trong c\u00e1c coroutine kh\u00f4ng c\u00f3 kh\u1ea3 n\u0103ng b\u1eaft ngo\u1ea1i l\u1ec7 tr\u1ef1c ti\u1ebfp<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">CoroutineExceptionHandler<\/span><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t handler \u0111\u1eb7c bi\u1ec7t d\u00e0nh cho vi\u1ec7c x\u1eed l\u00fd ngo\u1ea1i l\u1ec7 trong coroutine. N\u00f3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng khi b\u1ea1n mu\u1ed1n b\u1eaft l\u1ed7i \u1edf m\u1ee9c to\u00e0n c\u1ee5c cho c\u00e1c coroutine kh\u00f4ng \u0111\u01b0\u1ee3c bao b\u1ecdc b\u1edfi <\/span><span style=\"font-weight: 400;\">try-catch<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<pre>val handler = Coroutine ExceptionHandler { _, exception -&gt;\r\n  println(\"Caught $exception\")\r\n}\r\n\r\nGlobalScope.launch(handler) {\r\n  \/\/ M\u1ed9t coroutine g\u00e2y ra l\u1ed7i\r\n  throw Exception(\"Test Exception\")\r\n}<\/pre>\n<h4><strong>S\u1eed d\u1ee5ng supervisorScope cho ph\u00e9p b\u1ea1n b\u1eaft l\u1ed7i trong t\u1eebng coroutine c\u1ee5 th\u1ec3 m\u00e0 kh\u00f4ng l\u00e0m h\u1ee7y to\u00e0n b\u1ed9 nh\u00f3m coroutine<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">Khi l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c coroutine con, <\/span><span style=\"font-weight: 400;\">supervisorScope<\/span><span style=\"font-weight: 400;\"> c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 ng\u0103n ch\u1eb7n l\u1ed7i trong c\u00e1c coroutine con \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn c\u00e1c coroutine kh\u00e1c trong ph\u1ea1m vi cha. \u0110i\u1ec1u n\u00e0y gi\u00fap b\u1ea1n ki\u1ec3m so\u00e1t l\u1ed7i \u1edf c\u1ea5p \u0111\u1ed9 c\u1ee5 th\u1ec3 m\u00e0 kh\u00f4ng l\u00e0m h\u1ecfng to\u00e0n b\u1ed9 chu\u1ed7i coroutine.<\/span><\/p>\n<pre>supervisorScope {\r\n  launch {\r\n    \/\/ Task 1\r\n  }\r\n  launch {\r\n    \/\/ Task 2, n\u1ebfu g\u1eb7p l\u1ed7i kh\u00f4ng \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn task kh\u00e1c\r\n  }\r\n}<\/pre>\n<h4><strong>S\u1eed d\u1ee5ng withContext \u0111\u1ec3 thay \u0111\u1ed5i context cho ph\u00e9p b\u1ea1n bao b\u1ecdc m\u1ed9t \u0111o\u1ea1n m\u00e3 trong m\u1ed9t coroutine context nh\u1ea5t \u0111\u1ecbnh v\u00e0 x\u1eed l\u00fd l\u1ed7i li\u00ean quan \u0111\u1ebfn x\u1eed l\u00fd ngo\u1ea1i l\u1ec7<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">withContext<\/span><span style=\"font-weight: 400;\"> cho ph\u00e9p thay \u0111\u1ed5i context trong m\u1ed9t coroutine v\u00e0 c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00f9ng v\u1edbi <\/span><span style=\"font-weight: 400;\">try-catch<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c l\u1ed7i x\u1ea3y ra trong m\u1ed9t ph\u1ea1m vi context c\u1ee5 th\u1ec3.<\/span><\/p>\n<pre>try {\r\n  val result withContext (Dispatchers. IO) {\r\n      someFunction ThatMightFail()\r\n  }\r\n} catch (e: Exception) {\r\n  println(\"L\u1ed7i khi th\u1ef1c hi\u1ec7n c\u00f4ng vi\u1ec7c trong IO context: ${e.message}\")\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Trong m\u00f4i tr\u01b0\u1eddng th\u1ef1c t\u1ebf, b\u1ea1n c\u00f3 th\u1ec3 k\u1ebft h\u1ee3p c\u00e1c ph\u01b0\u01a1ng ph\u00e1p n\u00e0y t\u00f9y thu\u1ed9c v\u00e0o c\u00e1ch b\u1ea1n t\u1ed5 ch\u1ee9c v\u00e0 ph\u00e2n t\u00e1ch c\u00e1c coroutine trong \u1ee9ng d\u1ee5ng c\u1ee7a m\u00ecnh.<\/span><\/p>\n<h3><strong>C\u00e1c Channel trong Kotlin l\u00e0 g\u00ec? Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng?<\/strong><\/h3>\n<p><a href=\"https:\/\/kotlinlang.org\/docs\/channels.html\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">Channels trong Kotlin<\/span><\/a><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t th\u00e0nh ph\u1ea7n c\u1ee7a th\u01b0 vi\u1ec7n Kotlin Coroutines, \u0111\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 trao \u0111\u1ed5i d\u1eef li\u1ec7u gi\u1eefa c\u00e1c coroutine m\u1ed9t c\u00e1ch an to\u00e0n v\u00e0 hi\u1ec7u qu\u1ea3, t\u01b0\u01a1ng t\u1ef1 nh\u01b0 c\u00e1c queue hay pipeline. Channels gi\u00fap cho c\u00e1c coroutine c\u00f3 th\u1ec3 g\u1eedi v\u00e0 nh\u1eadn d\u1eef li\u1ec7u t\u1eeb nhau m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i lo l\u1eafng v\u1ec1 \u0111i\u1ec1u ki\u1ec7n \u0111ua (race conditions) hay kh\u00f3a (lock).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Channels r\u1ea5t h\u1eefu \u00edch khi b\u1ea1n c\u00f3 m\u1ed9t lu\u1ed3ng d\u1eef li\u1ec7u c\u1ea7n x\u1eed l\u00fd li\u00ean t\u1ee5c trong c\u00e1c coroutine, ch\u1eb3ng h\u1ea1n nh\u01b0 lu\u1ed3ng s\u1ef1 ki\u1ec7n t\u1eeb ng\u01b0\u1eddi d\u00f9ng, lu\u1ed3ng d\u1eef li\u1ec7u trong m\u1ed9t h\u1ec7 th\u1ed1ng x\u1eed l\u00fd nhi\u1ec1u t\u00e1c v\u1ee5 ho\u1eb7c khi b\u1ea1n mu\u1ed1n t\u1ea1o m\u1ed9t pipeline \u0111\u1ec3 x\u1eed l\u00fd d\u1eef li\u1ec7u theo nhi\u1ec1u b\u01b0\u1edbc.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Channels gi\u00fap c\u00e1c coroutine giao ti\u1ebfp m\u1ed9t c\u00e1ch an to\u00e0n m\u00e0 kh\u00f4ng c\u1ea7n c\u00e1c c\u01a1 ch\u1ebf kh\u00f3a truy\u1ec1n th\u1ed1ng, gi\u00fap m\u00e3 d\u1ec5 b\u1ea3o tr\u00ec v\u00e0 tr\u00e1nh c\u00e1c v\u1ea5n \u0111\u1ec1 nh\u01b0 deadlock hay \u0111i\u1ec1u ki\u1ec7n \u0111ua.<\/span><\/p>\n<h3><strong>Coroutine Scope l\u00e0 g\u00ec, v\u00e0 c\u00f3 nh\u1eefng lo\u1ea1i n\u00e0o?<\/strong><\/h3>\n<p><a href=\"https:\/\/kotlinlang.org\/api\/kotlinx.coroutines\/kotlinx-coroutines-core\/kotlinx.coroutines\/-coroutine-scope\/\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">Coroutine Scope trong Kotlin<\/span><\/a><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t ph\u1ea1m vi (scope) x\u00e1c \u0111\u1ecbnh v\u00f2ng \u0111\u1eddi v\u00e0 b\u1ed1i c\u1ea3nh m\u00e0 c\u00e1c coroutine \u0111\u01b0\u1ee3c kh\u1edfi ch\u1ea1y v\u00e0 qu\u1ea3n l\u00fd. N\u00f3 gi\u00fap \u0111\u1ea3m b\u1ea3o r\u1eb1ng c\u00e1c coroutine ho\u00e0n th\u00e0nh c\u00f4ng vi\u1ec7c trong m\u1ed9t ph\u1ea1m vi nh\u1ea5t \u0111\u1ecbnh ho\u1eb7c h\u1ee7y khi kh\u00f4ng c\u00f2n c\u1ea7n thi\u1ebft, gi\u00fap qu\u1ea3n l\u00fd t\u00e0i nguy\u00ean hi\u1ec7u qu\u1ea3 h\u01a1n.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Lo\u1ea1i Coroutine Scope<\/b><\/td>\n<td><b>M\u00f4 t\u1ea3\u00a0<\/b><\/td>\n<td><b>L\u1ee3i \u00edch v\u00e0 nh\u01b0\u1ee3c \u0111i\u1ec3m<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>GlobalScope<\/b><\/td>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">L\u00e0 ph\u1ea1m vi to\u00e0n c\u1ee5c, gi\u00fap kh\u1edfi ch\u1ea1y coroutine \u1edf m\u1ee9c \u1ee9ng d\u1ee5ng m\u00e0 kh\u00f4ng ph\u1ee5 thu\u1ed9c v\u00e0o v\u00f2ng \u0111\u1eddi c\u1ee7a c\u00e1c th\u00e0nh ph\u1ea7n kh\u00e1c.<\/span><\/td>\n<td>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">&#8211; <\/span><b>L\u1ee3i \u00edch<\/b><span style=\"font-weight: 400;\">: Kh\u1edfi ch\u1ea1y coroutine m\u00e0 kh\u00f4ng c\u1ea7n quan t\u00e2m \u0111\u1ebfn v\u00f2ng \u0111\u1eddi c\u1ee7a c\u00e1c th\u00e0nh ph\u1ea7n kh\u00e1c.<\/span><\/p>\n<p style=\"text-align: left;\"><span style=\"font-weight: 400;\">&#8211; <\/span><b>Nh\u01b0\u1ee3c \u0111i\u1ec3m<\/b><span style=\"font-weight: 400;\">: D\u1ec5 d\u1eabn \u0111\u1ebfn thi\u1ebfu ki\u1ec3m so\u00e1t v\u00f2ng \u0111\u1eddi v\u00e0 r\u00f2 r\u1ec9 t\u00e0i nguy\u00ean n\u1ebfu kh\u00f4ng \u0111\u01b0\u1ee3c qu\u1ea3n l\u00fd c\u1ea9n th\u1eadn.<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td><b>CoroutineScope<\/b><\/td>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">Ph\u1ea1m vi n\u00e0y th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c l\u1edbp nh\u01b0 ViewModel ho\u1eb7c c\u00e1c th\u00e0nh ph\u1ea7n c\u00f3 v\u00f2ng \u0111\u1eddi x\u00e1c \u0111\u1ecbnh, cho ph\u00e9p ki\u1ec3m so\u00e1t v\u00f2ng \u0111\u1eddi coroutine.<\/span><\/td>\n<td style=\"text-align: left;\"><b>L\u1ee3i \u00edch<\/b><span style=\"font-weight: 400;\">: Cho ph\u00e9p t\u1ef1 qu\u1ea3n l\u00fd v\u00f2ng \u0111\u1eddi c\u1ee7a coroutine, \u0111\u1ea3m b\u1ea3o ch\u00fang \u0111\u01b0\u1ee3c h\u1ee7y b\u1ecf khi kh\u00f4ng c\u1ea7n thi\u1ebft.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>SupervisorScope<\/b><\/td>\n<td style=\"text-align: left;\"><span style=\"font-weight: 400;\">T\u01b0\u01a1ng t\u1ef1 nh\u01b0 CoroutineScope, nh\u01b0ng n\u1ebfu m\u1ed9t coroutine con g\u1eb7p l\u1ed7i, n\u00f3 s\u1ebd kh\u00f4ng \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn c\u00e1c coroutine anh em kh\u00e1c trong ph\u1ea1m vi \u0111\u00f3.<\/span><\/td>\n<td style=\"text-align: left;\"><b>L\u1ee3i \u00edch<\/b><span style=\"font-weight: 400;\">: Gi\u00fap c\u00e1c t\u00e1c v\u1ee5 \u0111\u1ed9c l\u1eadp kh\u00f4ng b\u1ecb \u1ea3nh h\u01b0\u1edfng n\u1ebfu c\u00f3 l\u1ed7i trong m\u1ed9t coroutine.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><strong>Flow trong Kotlin l\u00e0 g\u00ec?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Flow l\u00e0 m\u1ed9t API \u0111\u01b0\u1ee3c gi\u1edbi thi\u1ec7u trong <\/span><i><span style=\"font-weight: 400;\">Kotlin Coroutines<\/span><\/i><span style=\"font-weight: 400;\"> \u0111\u1ec3 x\u1eed l\u00fd lu\u1ed3ng d\u1eef li\u1ec7u b\u1ea5t \u0111\u1ed3ng b\u1ed9 theo c\u00e1ch l\u1eadp tr\u00ecnh ph\u1ea3n \u1ee9ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Flow cho ph\u00e9p b\u1ea1n ph\u00e1t ra m\u1ed9t chu\u1ed7i c\u00e1c gi\u00e1 tr\u1ecb qua th\u1eddi gian, \u0111i\u1ec1u n\u00e0y r\u1ea5t h\u1eefu \u00edch khi l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u lu\u1ed3ng (stream) nh\u01b0 d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c ph\u00e1t li\u00ean t\u1ee5c ho\u1eb7c d\u1eef li\u1ec7u c\u00f3 s\u1ef1 thay \u0111\u1ed5i theo th\u1eddi gian (v\u00ed d\u1ee5: d\u1eef li\u1ec7u t\u1eeb m\u1ea1ng, c\u01a1 s\u1edf d\u1eef li\u1ec7u, ho\u1eb7c c\u1ea3m bi\u1ebfn).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Flow c\u00f3 t\u00ednh n\u0103ng k\u1ebft h\u1ee3p cao v\u1edbi c\u00e1c coroutine, gi\u00fap x\u1eed l\u00fd d\u1eef li\u1ec7u kh\u00f4ng \u0111\u1ed3ng b\u1ed9 tr\u1edf n\u00ean d\u1ec5 qu\u1ea3n l\u00fd v\u00e0 ti\u1ebft ki\u1ec7m t\u00e0i nguy\u00ean h\u01a1n.<\/span><\/p>\n<h3><strong>Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng Flow thay v\u00ec LiveData?<\/strong><\/h3>\n<h3><b>N\u00ean s\u1eed d\u1ee5ng Flow khi:<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ph\u1ea1m vi \u0111a n\u1ec1n t\u1ea3ng<\/b><span style=\"font-weight: 400;\">: Flow c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng kh\u00f4ng ch\u1ec9 tr\u00ean Android m\u00e0 c\u00f2n tr\u00ean c\u00e1c n\u1ec1n t\u1ea3ng kh\u00e1c, l\u00e0m cho n\u00f3 tr\u1edf th\u00e0nh l\u1ef1a ch\u1ecdn t\u1ed1t khi \u1ee9ng d\u1ee5ng c\u00f3 ti\u1ec1m n\u0103ng ph\u00e1t tri\u1ec3n \u0111a n\u1ec1n t\u1ea3ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>X\u1eed l\u00fd d\u1eef li\u1ec7u nhi\u1ec1u t\u1ea7ng v\u00e0 kh\u00f4ng \u0111\u1ed3ng b\u1ed9<\/b><span style=\"font-weight: 400;\">: Flow h\u1ed7 tr\u1ee3 c\u00e1c lu\u1ed3ng d\u1eef li\u1ec7u ph\u1ee9c t\u1ea1p v\u00e0 c\u00f3 kh\u1ea3 n\u0103ng th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c nh\u01b0 map, filter, combine, flatMapLatest,&#8230; gi\u00fap t\u1ed1i \u01b0u h\u00f3a m\u00e3 ngu\u1ed3n. LiveData h\u1ea1n ch\u1ebf h\u01a1n trong c\u00e1c t\u00ecnh hu\u1ed1ng n\u00e0y.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Qu\u1ea3n l\u00fd t\u00e0i nguy\u00ean hi\u1ec7u qu\u1ea3 h\u01a1n<\/b><span style=\"font-weight: 400;\">: Flow h\u1ed7 tr\u1ee3 cold stream, ngh\u0129a l\u00e0 ch\u1ec9 ph\u00e1t d\u1eef li\u1ec7u khi c\u00f3 m\u1ed9t collector, gi\u00fap ti\u1ebft ki\u1ec7m t\u00e0i nguy\u00ean so v\u1edbi LiveData, v\u1ed1n lu\u00f4n gi\u1eef d\u1eef li\u1ec7u ho\u1ea1t \u0111\u1ed9ng b\u1ea5t k\u1ec3 c\u00f3 ng\u01b0\u1eddi quan s\u00e1t hay kh\u00f4ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u00edch h\u1ee3p t\u1ed1t v\u1edbi coroutine<\/b><span style=\"font-weight: 400;\">: Flow \u0111\u01b0\u1ee3c x\u00e2y d\u1ef1ng d\u1ef1a tr\u00ean coroutine, gi\u00fap x\u1eed l\u00fd c\u00e1c thao t\u00e1c b\u1ea5t \u0111\u1ed3ng b\u1ed9 m\u1ed9t c\u00e1ch li\u1ec1n m\u1ea1ch v\u00e0 d\u1ec5 d\u00e0ng.<\/span><\/li>\n<\/ul>\n<h4><b>N\u00ean s\u1eed d\u1ee5ng LiveData khi:<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u00edch h\u1ee3p ch\u1eb7t ch\u1ebd v\u1edbi Android<\/b><span style=\"font-weight: 400;\">: LiveData \u0111\u01b0\u1ee3c thi\u1ebft k\u1ebf \u0111\u1eb7c bi\u1ec7t cho \u1ee9ng d\u1ee5ng Android, d\u1ec5 d\u00e0ng s\u1eed d\u1ee5ng trong ViewModel v\u00e0 c\u00f3 kh\u1ea3 n\u0103ng t\u1ef1 \u0111\u1ed9ng c\u1eadp nh\u1eadt giao di\u1ec7n khi d\u1eef li\u1ec7u thay \u0111\u1ed5i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Qu\u1ea3n l\u00fd v\u00f2ng \u0111\u1eddi<\/b><span style=\"font-weight: 400;\">: LiveData t\u1ef1 \u0111\u1ed9ng qu\u1ea3n l\u00fd v\u00f2ng \u0111\u1eddi c\u1ee7a c\u00e1c observer, gi\u00fap tr\u00e1nh c\u00e1c v\u1ea5n \u0111\u1ec1 v\u1ec1 memory leak khi kh\u00f4ng c\u00f2n observer ho\u1eb7c khi ho\u1ea1t \u0111\u1ed9ng trong c\u00e1c t\u00ecnh hu\u1ed1ng thay \u0111\u1ed5i tr\u1ea1ng th\u00e1i v\u00f2ng \u0111\u1eddi.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>D\u1ec5 d\u00e0ng t\u00edch h\u1ee3p v\u1edbi UI<\/b><span style=\"font-weight: 400;\">: LiveData ho\u1ea1t \u0111\u1ed9ng t\u1ed1t v\u1edbi c\u00e1c UI components trong Android, nh\u01b0 Activity, Fragment, v\u00e0 ViewModel, gi\u00fap t\u1ef1 \u0111\u1ed9ng c\u1eadp nh\u1eadt giao di\u1ec7n m\u00e0 kh\u00f4ng c\u1ea7n qu\u1ea3n l\u00fd th\u1ee7 c\u00f4ng qu\u00e1 tr\u00ecnh c\u1eadp nh\u1eadt d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ul>\n<h3><strong>L\u00e0m sao \u0111\u1ec3 h\u1ee7y m\u1ed9t Coroutine trong Kotlin?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 h\u1ee7y m\u1ed9t Coroutine trong Kotlin, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">Job<\/span><span style=\"font-weight: 400;\"> m\u00e0 Coroutine \u0111\u00f3 \u0111\u00e3 t\u1ea1o ra. M\u1ed7i coroutine trong Kotlin \u0111\u1ec1u tr\u1ea3 v\u1ec1 m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng <\/span><span style=\"font-weight: 400;\">Job<\/span><span style=\"font-weight: 400;\">, v\u00e0 b\u1ea1n c\u00f3 th\u1ec3 g\u1ecdi ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">cancel()<\/span><span style=\"font-weight: 400;\"> tr\u00ean \u0111\u1ed1i t\u01b0\u1ee3ng <\/span><span style=\"font-weight: 400;\">Job<\/span><span style=\"font-weight: 400;\"> \u0111\u00f3 \u0111\u1ec3 h\u1ee7y coroutine.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">D\u01b0\u1edbi \u0111\u00e2y l\u00e0 c\u00e1ch th\u1ef1c hi\u1ec7n:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Kh\u1edfi t\u1ea1o m\u1ed9t Coroutine v\u1edbi Job:<\/span><\/li>\n<\/ul>\n<pre>val job = GlobalScope.launch {\r\n  \/\/ Coroutine code here\r\n}<\/pre>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">H\u1ee7y Coroutine: <\/span>\u0110\u1ec3 h\u1ee7y coroutine, b\u1ea1n g\u1ecdi:<\/li>\n<\/ul>\n<pre>job.cancel():<\/pre>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ki\u1ec3m tra tr\u1ea1ng th\u00e1i c\u1ee7a Job: <\/span>B\u1ea1n c\u00f3 th\u1ec3 ki\u1ec3m tra xem coroutine c\u00f3 b\u1ecb h\u1ee7y hay ch\u01b0a b\u1eb1ng c\u00e1ch ki\u1ec3m tra thu\u1ed9c t\u00ednh isCancelled c\u1ee7a Job:<\/li>\n<\/ul>\n<pre>if (job.isCancelled) {\r\n  println(\"Coroutine has been cancelled\")\r\n}<\/pre>\n<h3><strong>S\u1ef1 kh\u00e1c bi\u1ec7t gi\u1eefa suspend function v\u00e0 coroutine builder<\/strong><\/h3>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">Ti\u00eau ch\u00ed<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Suspend Function<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Coroutine Builder<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">\u0110\u1ecbnh ngh\u0129a\u00a0\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">L\u00e0 h\u00e0m c\u00f3 th\u1ec3 t\u1ea1m d\u1eebng v\u00e0 ti\u1ebfp t\u1ee5c sau khi ho\u00e0n th\u00e0nh m\u1ed9t t\u00e1c v\u1ee5 b\u1ea5t \u0111\u1ed3ng b\u1ed9.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">L\u00e0 c\u1ea5u tr\u00fac gi\u00fap t\u1ea1o v\u00e0 qu\u1ea3n l\u00fd coroutine (ch\u1eb3ng h\u1ea1n nh\u01b0 <\/span><span style=\"font-weight: 400;\">launch<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">async<\/span><span style=\"font-weight: 400;\">).<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\u0110\u01b0\u1ee3c s\u1eed d\u1ee5ng trong coroutine \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 b\u1ea5t \u0111\u1ed3ng b\u1ed9 m\u00e0 kh\u00f4ng ch\u1eb7n lu\u1ed3ng ch\u00ednh.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\u0110\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 kh\u1edfi t\u1ea1o v\u00e0 b\u1eaft \u0111\u1ea7u m\u1ed9t coroutine.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Kh\u1ea3 n\u0103ng g\u1ecdi<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ch\u1ec9 c\u00f3 th\u1ec3 g\u1ecdi trong m\u1ed9t coroutine ho\u1eb7c t\u1eeb m\u1ed9t h\u00e0m suspend kh\u00e1c.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c g\u1ecdi tr\u1ef1c ti\u1ebfp t\u1eeb h\u00e0m <\/span><span style=\"font-weight: 400;\">runBlocking<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c m\u1ed9t coroutine.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Ch\u1ea1y \u0111\u1ed9c l\u1eadp<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng th\u1ec3 t\u1ef1 kh\u1edfi \u0111\u1ed9ng coroutine m\u1edbi, n\u00f3 ch\u1ec9 t\u1ea1m d\u1eebng v\u00e0 ti\u1ebfp t\u1ee5c trong coroutine hi\u1ec7n t\u1ea1i.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 t\u1ea1o ra m\u1ed9t coroutine m\u1edbi v\u00e0 qu\u1ea3n l\u00fd ch\u00fang.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">Ch\u1ea1y \u0111\u1ed3ng b\u1ed9\/ b\u1ea5t \u0111\u1ed3ng b\u1ed9<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 b\u1ea5t \u0111\u1ed3ng b\u1ed9 nh\u01b0ng c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c g\u1ecdi trong h\u00e0m \u0111\u1ed3ng b\u1ed9 n\u1ebfu c\u00f3 coroutine scope.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">T\u1ea1o coroutine m\u1edbi \u0111\u1ec3 ch\u1ea1y b\u1ea5t \u0111\u1ed3ng b\u1ed9.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb, nh\u01b0ng c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">Result<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">deferred<\/span><span style=\"font-weight: 400;\"> trong m\u1ed9t coroutine.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 tr\u1ea3 v\u1ec1 <\/span><span style=\"font-weight: 400;\">Deferred<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 l\u1ea5y k\u1ebft qu\u1ea3 trong t\u01b0\u01a1ng lai (v\u1edbi <\/span><span style=\"font-weight: 400;\">async<\/span><span style=\"font-weight: 400;\">).<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"Cau_hoi_phong_van_Kotlin_ve_kien_thuc_lap_trinh_ham\"><\/span><strong>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin v\u1ec1 ki\u1ebfn th\u1ee9c l\u1eadp tr\u00ecnh h\u00e0m<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><strong>Kotlin h\u1ed7 tr\u1ee3 l\u1eadp tr\u00ecnh h\u00e0m (functional programming) nh\u01b0 th\u1ebf n\u00e0o?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Kotlin h\u1ed7 tr\u1ee3 l\u1eadp tr\u00ecnh h\u00e0m (<\/span><span style=\"font-weight: 400;\">functional programming<\/span><span style=\"font-weight: 400;\">) m\u1ed9t c\u00e1ch to\u00e0n di\u1ec7n. Kotlin k\u1ebft h\u1ee3p c\u00e1c kh\u00e1i ni\u1ec7m l\u1eadp tr\u00ecnh h\u00e0m v\u1edbi l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng, gi\u00fap t\u1eadn d\u1ee5ng \u0111\u01b0\u1ee3c \u0111i\u1ec3m m\u1ea1nh c\u1ee7a c\u1ea3 hai c\u00e1ch ti\u1ebfp c\u1eadn.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00e1c t\u00ednh n\u0103ng l\u1eadp tr\u00ecnh h\u00e0m n\u1ed5i b\u1eadt c\u1ee7a Kotlin bao g\u1ed3m:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Higher-order functions (H\u00e0m b\u1eadc cao): Kotlin cho ph\u00e9p truy\u1ec1n h\u00e0m d\u01b0\u1edbi d\u1ea1ng tham s\u1ed1 v\u00e0 tr\u1ea3 v\u1ec1 h\u00e0m t\u1eeb m\u1ed9t h\u00e0m kh\u00e1c, gi\u00fap t\u1ea1o ra c\u00e1c h\u00e0m c\u00f3 t\u00ednh linh ho\u1ea1t v\u00e0 t\u00e1i s\u1eed d\u1ee5ng cao.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Lambda expressions (Bi\u1ec3u th\u1ee9c Lambda): Kotlin h\u1ed7 tr\u1ee3 bi\u1ec3u th\u1ee9c Lambda, gi\u00fap vi\u1ebft m\u00e3 ng\u1eafn g\u1ecdn v\u00e0 tr\u1ef1c quan h\u01a1n, th\u01b0\u1eddng d\u00f9ng trong c\u00e1c t\u00ecnh hu\u1ed1ng nh\u01b0 x\u1eed l\u00fd danh s\u00e1ch v\u00e0 thao t\u00e1c d\u1eef li\u1ec7u.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Immutability (T\u00ednh b\u1ea5t bi\u1ebfn): Kotlin khuy\u1ebfn kh\u00edch s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">val<\/span><span style=\"font-weight: 400;\"> thay v\u00ec <\/span><span style=\"font-weight: 400;\">var<\/span><span style=\"font-weight: 400;\">, t\u1ee9c l\u00e0 s\u1eed d\u1ee5ng c\u00e1c bi\u1ebfn kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i, gi\u00fap code an to\u00e0n h\u01a1n v\u00e0 tr\u00e1nh c\u00e1c l\u1ed7i li\u00ean quan \u0111\u1ebfn vi\u1ec7c thay \u0111\u1ed5i tr\u1ea1ng th\u00e1i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Collection functions: Kotlin cung c\u1ea5p c\u00e1c h\u00e0m ti\u1ec7n \u00edch tr\u00ean collections (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 thao t\u00e1c v\u1edbi d\u1eef li\u1ec7u m\u1ed9t c\u00e1ch r\u00f5 r\u00e0ng v\u00e0 hi\u1ec7u qu\u1ea3.<\/span><\/li>\n<\/ol>\n<h3><strong>Higher-order function l\u00e0 g\u00ec? Kotlin c\u00f3 h\u1ed7 tr\u1ee3 kh\u00f4ng?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Higher-order function (h\u00e0m b\u1eadc cao) l\u00e0 h\u00e0m c\u00f3 kh\u1ea3 n\u0103ng nh\u1eadn h\u00e0m kh\u00e1c l\u00e0m \u0111\u1ed1i s\u1ed1 ho\u1eb7c tr\u1ea3 v\u1ec1 m\u1ed9t h\u00e0m kh\u00e1c. \u0110\u00e2y l\u00e0 m\u1ed9t t\u00ednh n\u0103ng quan tr\u1ecdng trong l\u1eadp tr\u00ecnh h\u00e0m (functional programming), gi\u00fap vi\u1ebft m\u00e3 d\u1ec5 b\u1ea3o tr\u00ec v\u00e0 linh ho\u1ea1t h\u01a1n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, higher-order function \u0111\u01b0\u1ee3c h\u1ed7 tr\u1ee3 v\u00e0 l\u00e0 m\u1ed9t ph\u1ea7n m\u1ea1nh m\u1ebd c\u1ee7a ng\u00f4n ng\u1eef. B\u1ea1n c\u00f3 th\u1ec3 truy\u1ec1n m\u1ed9t h\u00e0m d\u01b0\u1edbi d\u1ea1ng lambda ho\u1eb7c reference, ho\u1eb7c tr\u1ea3 v\u1ec1 m\u1ed9t h\u00e0m t\u1eeb m\u1ed9t higher-order function. \u0110i\u1ec1u n\u00e0y r\u1ea5t h\u1eefu \u00edch khi c\u1ea7n x\u1eed l\u00fd d\u1eef li\u1ec7u theo c\u00e1ch linh ho\u1ea1t, v\u00ed d\u1ee5 trong vi\u1ec7c t\u1ea1o c\u00e1c h\u00e0m callback ho\u1eb7c s\u1eed d\u1ee5ng c\u00e1c h\u00e0m 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;\">, v\u00e0 <\/span><span style=\"font-weight: 400;\">reduce<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 thao t\u00e1c tr\u00ean danh s\u00e1ch.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 ng\u1eafn g\u1ecdn v\u1ec1 higher-order function trong Kotlin:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">fun higherOrderFunction(action: (Int) -&gt; Int): Int {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0return action(5)<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">fun main() {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0val result = higherOrderFunction { it * 2 }<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0println(result) \/\/ Output: 10<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Nh\u01b0 v\u1eady, khi h\u1ecfi v\u1ec1 higher-order function, nh\u00e0 tuy\u1ec3n d\u1ee5ng mu\u1ed1n th\u1ea5y b\u1ea1n hi\u1ec3u r\u00f5 v\u1ec1 kh\u00e1i ni\u1ec7m l\u1eadp tr\u00ecnh h\u00e0m v\u00e0 c\u00e1ch \u00e1p d\u1ee5ng n\u00f3 trong Kotlin, gi\u00fap vi\u1ebft m\u00e3 linh ho\u1ea1t v\u00e0 d\u1ec5 b\u1ea3o tr\u00ec h\u01a1n.<\/span><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">T\u00ecm hi\u1ec3u th\u00eam t\u1ea1i: <\/span><a href=\"https:\/\/kotlinlang.org\/docs\/lambdas.html\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">https:\/\/kotlinlang.org\/docs\/lambdas.html<\/span><\/a><\/p><\/blockquote>\n<h3><strong>lambda expressions trong Kotlin l\u00e0 g\u00ec? Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Lambda expressions trong Kotlin l\u00e0 nh\u1eefng h\u00e0m \u1ea9n danh (anonymous functions) gi\u00fap t\u1ea1o ra c\u00e1c bi\u1ec3u th\u1ee9c ng\u1eafn g\u1ecdn \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c t\u00e1c v\u1ee5 m\u00e0 kh\u00f4ng c\u1ea7n khai b\u00e1o h\u00e0m \u0111\u1ea7y \u0111\u1ee7. C\u00fa ph\u00e1p c\u1ee7a lambda trong Kotlin r\u1ea5t ng\u1eafn g\u1ecdn, th\u01b0\u1eddng gi\u00fap code d\u1ec5 \u0111\u1ecdc v\u00e0 gi\u1ea3m thi\u1ec3u boilerplate.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 v\u1ec1 lambda trong Kotlin: <\/span><span style=\"font-weight: 400;\">val sum: (Int, Int) -&gt; Int = { a, b -&gt; a + b }<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng lambda expressions?<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi c\u1ea7n truy\u1ec1n m\u1ed9t h\u00e0m l\u00e0m \u0111\u1ed1i s\u1ed1 v\u00e0o h\u00e0m kh\u00e1c, nh\u01b0 trong c\u00e1c h\u00e0m x\u1eed l\u00fd danh s\u00e1ch (<\/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;\">forEach<\/span><span style=\"font-weight: 400;\">), lambda expressions gi\u00fap m\u00e3 ngu\u1ed3n ng\u1eafn g\u1ecdn v\u00e0 d\u1ec5 \u0111\u1ecdc.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi c\u1ea7n bi\u1ec3u \u0111\u1ea1t c\u00e1c t\u00e1c v\u1ee5 \u0111\u01a1n gi\u1ea3n, c\u00e1c h\u00e0m callback ho\u1eb7c x\u1eed l\u00fd s\u1ef1 ki\u1ec7n m\u00e0 kh\u00f4ng c\u1ea7n t\u1ea1o h\u00e0m ri\u00eang.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c x\u1eed l\u00fd d\u1eef li\u1ec7u, lambda gi\u00fap x\u1eed l\u00fd d\u1eef li\u1ec7u theo c\u00e1ch ng\u1eafn g\u1ecdn v\u00e0 hi\u1ec7u qu\u1ea3 h\u01a1n.<\/span><\/li>\n<\/ul>\n<h3><strong>C\u00e1c h\u00e0m let, apply, also, run, v\u00e0 with kh\u00e1c nhau nh\u01b0 th\u1ebf n\u00e0o?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">C\u00e1c h\u00e0m <\/span><b>let<\/b><span style=\"font-weight: 400;\">, <\/span><b>apply<\/b><span style=\"font-weight: 400;\">, <\/span><b>also<\/b><span style=\"font-weight: 400;\">, <\/span><b>run<\/b><span style=\"font-weight: 400;\">, v\u00e0 <\/span><b>with<\/b><span style=\"font-weight: 400;\"> trong Kotlin th\u01b0\u1eddng \u0111\u01b0\u1ee3c so s\u00e1nh v\u1edbi nhau v\u00ec ch\u00fang \u0111\u1ec1u l\u00e0 c\u00e1c h\u00e0m m\u1edf r\u1ed9ng (extension functions) ho\u1ea1t \u0111\u1ed9ng tr\u00ean \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 h\u1ed7 tr\u1ee3 c\u00e1c thao t\u00e1c nh\u01b0 chuy\u1ec3n \u0111\u1ed5i, thay \u0111\u1ed5i tr\u1ea1ng th\u00e1i ho\u1eb7c th\u1ef1c thi c\u00e1c h\u00e0nh \u0111\u1ed9ng m\u00e0 kh\u00f4ng c\u1ea7n t\u1ea1o ra m\u1ed9t bi\u1ebfn t\u1ea1m.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ch\u00fang c\u00f3 \u0111i\u1ec3m chung l\u00e0 cho ph\u00e9p truy c\u1eadp v\u00e0o \u0111\u1ed1i t\u01b0\u1ee3ng b\u00ean trong kh\u1ed1i l\u1ec7nh v\u00e0 gi\u00fap c\u1ea3i thi\u1ec7n m\u00e3 ngu\u1ed3n b\u1eb1ng c\u00e1ch gi\u1ea3m thi\u1ec3u vi\u1ec7c l\u1eb7p l\u1ea1i t\u00ean \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 l\u00e0m m\u00e3 ng\u1eafn g\u1ecdn, d\u1ec5 \u0111\u1ecdc h\u01a1n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">M\u1eb7c d\u00f9 c\u00f3 c\u00fa ph\u00e1p t\u01b0\u01a1ng t\u1ef1, m\u1ed7i h\u00e0m c\u00f3 nh\u1eefng \u0111\u1eb7c \u0111i\u1ec3m ri\u00eang bi\u1ec7t v\u1ec1 c\u00e1ch tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3 v\u00e0 ph\u1ea1m vi truy c\u1eadp, do \u0111\u00f3 ch\u00fang \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong c\u00e1c t\u00ecnh hu\u1ed1ng kh\u00e1c nhau.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400;\">H\u00e0m\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng v\u1edbi<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Tr\u1ea3 v\u1ec1\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">\u0110\u1ed1i t\u01b0\u1ee3ng tham chi\u1ebfu<\/span><\/td>\n<td><span style=\"font-weight: 400;\">M\u00f4 t\u1ea3 ng\u1eafn g\u1ecdn\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">let\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Object<\/span><\/td>\n<td><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 c\u1ee7a lambda<\/span><\/td>\n<td><span style=\"font-weight: 400;\">it\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c tr\u00ean \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3 c\u1ee7a lambda. Th\u01b0\u1eddng d\u00f9ng \u0111\u1ec3 tr\u00e1nh <\/span><span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c th\u1ef1c hi\u1ec7n chu\u1ed7i c\u00e1c thao t\u00e1c ng\u1eafn g\u1ecdn.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">apply<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Object<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Object<\/span><\/td>\n<td><span style=\"font-weight: 400;\">this<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">D\u00f9ng khi c\u1ea7n kh\u1edfi t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 thay \u0111\u1ed5i thu\u1ed9c t\u00ednh c\u1ee7a n\u00f3. Tr\u1ea3 v\u1ec1 ch\u00ednh \u0111\u1ed1i t\u01b0\u1ee3ng ban \u0111\u1ea7u sau khi \u00e1p d\u1ee5ng c\u00e1c thay \u0111\u1ed5i.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">also<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Object<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Object<\/span><\/td>\n<td><span style=\"font-weight: 400;\">it<\/span><\/td>\n<td><span style=\"font-weight: 400;\">D\u00f9ng \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 ph\u1ee5 tr\u00ean \u0111\u1ed1i t\u01b0\u1ee3ng, nh\u01b0 logging ho\u1eb7c ki\u1ec3m tra, m\u00e0 kh\u00f4ng thay \u0111\u1ed5i \u0111\u1ed1i t\u01b0\u1ee3ng. Tr\u1ea3 v\u1ec1 ch\u00ednh \u0111\u1ed1i t\u01b0\u1ee3ng ban \u0111\u1ea7u.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">run<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Object<\/span><\/td>\n<td><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 c\u1ee7a lambda<\/span><\/td>\n<td><span style=\"font-weight: 400;\">this<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400;\">D\u00f9ng khi c\u1ea7n th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c tr\u00ean \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb c\u1ee7a lambda. Th\u01b0\u1eddng k\u1ebft h\u1ee3p \u0111\u1ec3 tr\u00e1nh <\/span><span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c th\u1ef1c hi\u1ec7n m\u1ed9t chu\u1ed7i c\u00e1c thao t\u00e1c.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400;\">with<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Non-null Object<\/span><\/td>\n<td><span style=\"font-weight: 400;\">K\u1ebft qu\u1ea3 c\u1ee7a lambda<\/span><\/td>\n<td><span style=\"font-weight: 400;\">this<\/span><span style=\"font-weight: 400;\"> (m\u1eb7c \u0111\u1ecbnh)<\/span><\/td>\n<td><span style=\"font-weight: 400;\">D\u00f9ng khi c\u1ea7n thao t\u00e1c nhi\u1ec1u v\u1edbi m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng kh\u00f4ng <\/span><span style=\"font-weight: 400;\">null<\/span><span style=\"font-weight: 400;\">. Th\u01b0\u1eddng s\u1eed d\u1ee5ng nh\u01b0 m\u1ed9t c\u00f4ng c\u1ee5 ng\u1eef ph\u00e1p \u0111\u1ec3 thao t\u00e1c v\u1edbi m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee5 th\u1ec3 m\u00e0 kh\u00f4ng tr\u1ea3 v\u1ec1 n\u00f3.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><strong>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 s\u1eed d\u1ee5ng map, filter, v\u00e0 reduce trong Kotlin?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, c\u00e1c h\u00e0m <\/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;\">, v\u00e0 <\/span><span style=\"font-weight: 400;\">reduce<\/span><span style=\"font-weight: 400;\"> l\u00e0 c\u00e1c h\u00e0m cao c\u1ea5p gi\u00fap x\u1eed l\u00fd v\u00e0 thao t\u00e1c d\u1eef li\u1ec7u v\u1edbi c\u00e1c t\u1eadp h\u1ee3p nh\u01b0 danh s\u00e1ch ho\u1eb7c chu\u1ed7i.\u00a0<\/span><\/p>\n<h4><strong>map<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">H\u00e0m <\/span><span style=\"font-weight: 400;\">map<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 bi\u1ebfn \u0111\u1ed5i t\u1eebng ph\u1ea7n t\u1eed trong m\u1ed9t danh s\u00e1ch th\u00e0nh m\u1ed9t gi\u00e1 tr\u1ecb kh\u00e1c, t\u1ea1o ra m\u1ed9t danh s\u00e1ch m\u1edbi. V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">val numbers = listOf(1, 2, 3, 4)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">val squaredNumbers = numbers.map { it * it }<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ K\u1ebft qu\u1ea3: [1, 4, 9, 16]<\/span><\/pre>\n<h4><strong>filter<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">H\u00e0m <\/span><span style=\"font-weight: 400;\">filter<\/span><span style=\"font-weight: 400;\"> gi\u00fap l\u1ecdc ra c\u00e1c ph\u1ea7n t\u1eed th\u1ecfa m\u00e3n \u0111i\u1ec1u ki\u1ec7n \u0111\u00e3 cho. V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">val numbers = listOf(1, 2, 3, 4)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">val evenNumbers = numbers.filter { it % 2 == 0 }<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ K\u1ebft qu\u1ea3: [2, 4]<\/span><\/pre>\n<h4><strong>reduce<\/strong><\/h4>\n<p><span style=\"font-weight: 400;\">H\u00e0m <\/span><span style=\"font-weight: 400;\">reduce<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 t\u1ed5ng h\u1ee3p ho\u1eb7c g\u1ed9p c\u00e1c ph\u1ea7n t\u1eed th\u00e0nh m\u1ed9t gi\u00e1 tr\u1ecb duy nh\u1ea5t b\u1eb1ng c\u00e1ch \u00e1p d\u1ee5ng m\u1ed9t ph\u00e9p to\u00e1n li\u00ean ti\u1ebfp. V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">val numbers = listOf(1, 2, 3, 4)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">val sum = numbers.reduce { acc, number -&gt; acc + number }<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ K\u1ebft qu\u1ea3: 10<\/span><\/pre>\n<h3><strong>Gi\u1ea3i th\u00edch v\u1ec1 infix function trong Kotlin v\u00e0 c\u00e1ch khai b\u00e1o ch\u00fang<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, <\/span><a href=\"https:\/\/kotlinlang.org\/docs\/functions.html\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">infix function<\/span><\/a><span style=\"font-weight: 400;\"> l\u00e0 m\u1ed9t h\u00e0m \u0111\u1eb7c bi\u1ec7t cho ph\u00e9p g\u1ecdi m\u00e0 kh\u00f4ng c\u1ea7n d\u1ea5u ch\u1ea5m ho\u1eb7c d\u1ea5u ngo\u1eb7c \u0111\u01a1n, l\u00e0m cho m\u00e3 ng\u1eafn g\u1ecdn v\u00e0 d\u1ec5 \u0111\u1ecdc h\u01a1n, \u0111\u1eb7c bi\u1ec7t l\u00e0 khi m\u00f4 t\u1ea3 c\u00e1c quan h\u1ec7 ho\u1eb7c h\u00e0nh \u0111\u1ed9ng gi\u1eefa c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">M\u1ed9t h\u00e0m c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c khai b\u00e1o l\u00e0 infix khi \u0111\u00e1p \u1ee9ng c\u00e1c \u0111i\u1ec1u ki\u1ec7n sau:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">L\u00e0 h\u00e0m th\u00e0nh vi\u00ean (member function) ho\u1eb7c h\u00e0m m\u1edf r\u1ed9ng (extension function).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00f3 m\u1ed9t tham s\u1ed1 \u0111\u1ea7u v\u00e0o duy nh\u1ea5t.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u01b0\u1ee3c khai b\u00e1o v\u1edbi t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">infix<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">infix fun Int.times(x: Int): Int = this * x<\/span><\/pre>\n<h3><strong>Kotlin h\u1ed7 tr\u1ee3 tail recursion nh\u01b0 th\u1ebf n\u00e0o? Khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Kotlin h\u1ed7 tr\u1ee3 <\/span><b>tail recursion<\/b><span style=\"font-weight: 400;\"> th\u00f4ng qua t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">tailrec<\/span><span style=\"font-weight: 400;\">. Khi m\u1ed9t h\u00e0m \u0111\u1ec7 quy c\u00f3 th\u1ec3 g\u1ecdi l\u1ea1i ch\u00ednh n\u00f3 m\u00e0 kh\u00f4ng c\u1ea7n l\u01b0u tr\u1eef b\u1ea5t k\u1ef3 tr\u1ea1ng th\u00e1i trung gian n\u00e0o, Kotlin s\u1ebd t\u1ed1i \u01b0u h\u00f3a h\u00e0m \u0111\u00f3 th\u00e0nh m\u1ed9t v\u00f2ng l\u1eb7p, gi\u00fap tr\u00e1nh l\u1ed7i <\/span><b>StackOverflow<\/b><span style=\"font-weight: 400;\"> khi \u0111\u1ec7 quy s\u00e2u.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00e1ch s\u1eed d\u1ee5ng kh\u00e1 \u0111\u01a1n gi\u1ea3n: b\u1ea1n ch\u1ec9 c\u1ea7n th\u00eam t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">tailrec<\/span><span style=\"font-weight: 400;\"> tr\u01b0\u1edbc \u0111\u1ecbnh ngh\u0129a c\u1ee7a h\u00e0m \u0111\u1ec7 quy, v\u00e0 Kotlin s\u1ebd t\u1ef1 \u0111\u1ed9ng t\u1ed1i \u01b0u h\u00f3a n\u1ebfu c\u00f3 th\u1ec3. \u0110\u1ec3 t\u1ed1i \u01b0u h\u00f3a th\u00e0nh c\u00f4ng, l\u1eddi g\u1ecdi \u0111\u1ec7 quy ph\u1ea3i l\u00e0 l\u1ec7nh cu\u1ed1i c\u00f9ng trong th\u00e2n h\u00e0m.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">tailrec fun factorial(n: Int, accumulator: Int = 1): Int {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0return if (n &lt;= 1) accumulator else factorial(n - 1, accumulator * n)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p>N\u00ean s\u1eed d\u1ee5ng khi:<\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng tail recursion khi b\u1ea1n c\u1ea7n t\u00ednh to\u00e1n l\u1eb7p l\u1ea1i v\u1edbi c\u00e1c h\u00e0m \u0111\u1ec7 quy \u0111\u1ec3 t\u1ed1i \u01b0u hi\u1ec7u su\u1ea5t, \u0111\u1eb7c bi\u1ec7t khi x\u1eed l\u00fd c\u00e1c d\u1eef li\u1ec7u l\u1edbn v\u00e0 tr\u00e1nh l\u1ed7i ng\u0103n x\u1ebfp.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Ph\u00f9 h\u1ee3p khi b\u1ea1n c\u00f3 th\u1ec3 c\u1ea5u tr\u00fac thu\u1eadt to\u00e1n sao cho l\u1eddi g\u1ecdi \u0111\u1ec7 quy l\u00e0 l\u1ec7nh cu\u1ed1i c\u00f9ng trong h\u00e0m.<\/span><\/li>\n<\/ul>\n<h3><strong>Gi\u1ea3i th\u00edch v\u1ec1 operator overloading trong Kotlin v\u00e0 v\u00ed d\u1ee5 s\u1eed d\u1ee5ng<\/strong><\/h3>\n<p><a href=\"https:\/\/kotlinlang.org\/docs\/operator-overloading.html\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">Operator overloading<\/span><\/a><span style=\"font-weight: 400;\"> trong Kotlin cho ph\u00e9p ch\u00fang ta \u0111\u1ecbnh ngh\u0129a l\u1ea1i c\u00e1c ph\u00e9p to\u00e1n cho c\u00e1c l\u1edbp t\u00f9y ch\u1ec9nh, gi\u00fap m\u00e3 ngu\u1ed3n tr\u1edf n\u00ean ng\u1eafn g\u1ecdn v\u00e0 d\u1ec5 \u0111\u1ecdc h\u01a1n khi th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p t\u00ednh ho\u1eb7c thao t\u00e1c v\u1edbi \u0111\u1ed1i t\u01b0\u1ee3ng. \u0110\u00e2y l\u00e0 m\u1ed9t trong nh\u1eefng t\u00ednh n\u0103ng linh ho\u1ea1t c\u1ee7a Kotlin, t\u1ea1o \u0111i\u1ec1u ki\u1ec7n cho vi\u1ec7c s\u1eed d\u1ee5ng c\u00e1c to\u00e1n t\u1eed th\u00f4ng qua c\u00e1c h\u00e0m c\u00f3 t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">operator<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p>V\u00ed d\u1ee5:<\/p>\n<p><span style=\"font-weight: 400;\">Gi\u1ea3 s\u1eed b\u1ea1n c\u00f3 m\u1ed9t l\u1edbp <\/span><span style=\"font-weight: 400;\">Vector<\/span><span style=\"font-weight: 400;\"> \u0111\u1ea1i di\u1ec7n cho m\u1ed9t vector trong kh\u00f4ng gian 2D v\u00e0 b\u1ea1n mu\u1ed1n s\u1eed d\u1ee5ng to\u00e1n t\u1eed <\/span><span style=\"font-weight: 400;\">+<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 c\u1ed9ng hai vector v\u1edbi nhau. B\u1ea1n c\u00f3 th\u1ec3 l\u00e0m \u0111i\u1ec1u n\u00e0y b\u1eb1ng c\u00e1ch \u0111\u1ecbnh ngh\u0129a h\u00e0m <\/span><span style=\"font-weight: 400;\">plus<\/span><span style=\"font-weight: 400;\"> v\u1edbi t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">operator<\/span><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">data class Vector(val x: Int, val y: Int) {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0operator fun plus(other: Vector): Vector {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return Vector(x + other.x, y + other.y)<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0}<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">fun main() {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0val v1 = Vector(3, 4)<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0val v2 = Vector(2, 7)<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0val result = v1 + v2\u00a0 \/\/ S\u1eed d\u1ee5ng operator overloading cho ph\u00e9p +<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0println(result)\u00a0 \/\/ K\u1ebft qu\u1ea3: Vector(x=5, y=11)<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">\u1ede \u0111\u00e2y, to\u00e1n t\u1eed <\/span><span style=\"font-weight: 400;\">+<\/span><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng v\u1edbi hai \u0111\u1ed1i t\u01b0\u1ee3ng <\/span><span style=\"font-weight: 400;\">Vector<\/span><span style=\"font-weight: 400;\">, v\u00e0 ph\u01b0\u01a1ng th\u1ee9c <\/span><span style=\"font-weight: 400;\">plus<\/span><span style=\"font-weight: 400;\"> s\u1ebd \u0111\u01b0\u1ee3c g\u1ecdi ng\u1ea7m. \u0110\u00e2y l\u00e0 c\u00e1ch operator overloading l\u00e0m cho m\u00e3 d\u1ec5 hi\u1ec3u v\u00e0 ng\u1eafn g\u1ecdn h\u01a1n khi l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c l\u1edbp t\u00f9y ch\u1ec9nh.<\/span><\/p>\n<h3><strong>Kotlin c\u00f3 h\u1ed7 tr\u1ee3 partial application kh\u00f4ng? N\u1ebfu c\u00f3, l\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 tri\u1ec3n khai?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Kotlin kh\u00f4ng h\u1ed7 tr\u1ee3 partial application m\u1ed9t c\u00e1ch tr\u1ef1c ti\u1ebfp nh\u01b0 m\u1ed9t s\u1ed1 ng\u00f4n ng\u1eef h\u00e0m kh\u00e1c (v\u00ed d\u1ee5 Haskell) do kh\u00f4ng c\u00f3 c\u00fa ph\u00e1p t\u00edch h\u1ee3p s\u1eb5n cho vi\u1ec7c n\u00e0y.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Tuy nhi\u00ean, c\u00f3 th\u1ec3 \u0111\u1ea1t \u0111\u01b0\u1ee3c hi\u1ec7u \u1ee9ng t\u01b0\u01a1ng t\u1ef1 b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng c\u00e1c c\u00e1ch ti\u1ebfp c\u1eadn nh\u01b0 t\u1ea1o h\u00e0m m\u1edbi v\u1edbi \u0111\u1ed1i s\u1ed1 c\u1ed1 \u0111\u1ecbnh (closure) ho\u1eb7c s\u1eed d\u1ee5ng c\u00e1c h\u00e0m lambda.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Cau_hoi_phong_van_Kotlin_ve_hieu_suat_va_toi_uu_hoa\"><\/span><strong>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin v\u1ec1 hi\u1ec7u su\u1ea5t v\u00e0 t\u1ed1i \u01b0u h\u00f3a<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><strong>B\u1ea1n l\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 t\u1ed1i \u01b0u hi\u1ec7u su\u1ea5t cho m\u1ed9t \u1ee9ng d\u1ee5ng Android l\u1edbn vi\u1ebft b\u1eb1ng Kotlin?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 t\u1ed1i \u01b0u hi\u1ec7u su\u1ea5t cho m\u1ed9t \u1ee9ng d\u1ee5ng Android l\u1edbn vi\u1ebft b\u1eb1ng Kotlin, t\u00f4i s\u1ebd \u00e1p d\u1ee5ng m\u1ed9t s\u1ed1 chi\u1ebfn l\u01b0\u1ee3c sau \u0111\u00e2y:<\/span><\/p>\n<p><b>T\u1ed1i \u01b0u h\u00f3a m\u00e3 ngu\u1ed3n v\u00e0 logic x\u1eed l\u00fd:<\/b><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng c\u00e1c c\u1ea5u tr\u00fac d\u1eef li\u1ec7u v\u00e0 thu\u1eadt to\u00e1n ph\u00f9 h\u1ee3p \u0111\u1ec3 gi\u1ea3m \u0111\u1ed9 ph\u1ee9c t\u1ea1p t\u00ednh to\u00e1n.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Tr\u00e1nh vi\u1ec7c t\u1ea1o ra c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng kh\u00f4ng c\u1ea7n thi\u1ebft, t\u1ed1i \u01b0u vi\u1ec7c qu\u1ea3n l\u00fd b\u1ed9 nh\u1edb, v\u00e0 s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">lazy<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 kh\u1edfi t\u1ea1o c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng ch\u1ec9 khi c\u1ea7n thi\u1ebft.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">T\u1eadn d\u1ee5ng c\u00e1c t\u00ednh n\u0103ng c\u1ee7a Kotlin nh\u01b0 extension functions v\u00e0 higher-order functions \u0111\u1ec3 l\u00e0m s\u1ea1ch m\u00e3 ngu\u1ed3n, gi\u00fap d\u1ec5 duy tr\u00ec m\u00e0 kh\u00f4ng l\u00e0m gi\u1ea3m hi\u1ec7u su\u1ea5t.<\/span><\/li>\n<\/ul>\n<p><b>Qu\u1ea3n l\u00fd b\u1ed9 nh\u1edb hi\u1ec7u qu\u1ea3:<\/b><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">WeakReference<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">SoftReference<\/span><span style=\"font-weight: 400;\"> khi c\u1ea7n tr\u00e1nh vi\u1ec7c gi\u1eef tham chi\u1ebfu m\u1ea1nh \u0111\u1ebfn c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c d\u1ecdn d\u1eb9p \u0111\u1ec3 gi\u1ea3i ph\u00f3ng b\u1ed9 nh\u1edb.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Ki\u1ec3m tra v\u00e0 gi\u1ea3m thi\u1ec3u c\u00e1c t\u00ecnh hu\u1ed1ng r\u00f2 r\u1ec9 b\u1ed9 nh\u1edb, \u0111\u1eb7c bi\u1ec7t l\u00e0 trong c\u00e1c Activity\/Fragment ho\u1eb7c khi s\u1eed d\u1ee5ng c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng b\u1ea5t \u0111\u1ed3ng b\u1ed9.<\/span><\/li>\n<\/ul>\n<p><b>X\u1eed l\u00fd b\u1ea5t \u0111\u1ed3ng b\u1ed9 v\u00e0 \u0111a lu\u1ed3ng:<\/b><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">T\u1eadn d\u1ee5ng <\/span><span style=\"font-weight: 400;\">Coroutines<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c t\u00e1c v\u1ee5 b\u1ea5t \u0111\u1ed3ng b\u1ed9 m\u00e0 kh\u00f4ng g\u00e2y ra v\u1ea5n \u0111\u1ec1 v\u1edbi lu\u1ed3ng ch\u00ednh, gi\u00fap c\u1ea3i thi\u1ec7n kh\u1ea3 n\u0103ng ph\u1ea3n h\u1ed3i c\u1ee7a \u1ee9ng d\u1ee5ng.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">D\u00f9ng <\/span><span style=\"font-weight: 400;\">Dispatchers.IO<\/span><span style=\"font-weight: 400;\"> cho c\u00e1c t\u00e1c v\u1ee5 I\/O v\u00e0 <\/span><span style=\"font-weight: 400;\">Dispatchers.Main<\/span><span style=\"font-weight: 400;\"> cho vi\u1ec7c c\u1eadp nh\u1eadt UI, gi\u1ea3m thi\u1ec3u vi\u1ec7c ch\u1eb7n lu\u1ed3ng ch\u00ednh.<\/span><\/li>\n<\/ul>\n<p><b>T\u1ed1i \u01b0u h\u00f3a giao di\u1ec7n ng\u01b0\u1eddi d\u00f9ng (UI):<\/b><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">RecyclerView<\/span><span style=\"font-weight: 400;\"> v\u1edbi <\/span><span style=\"font-weight: 400;\">ViewHolder<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 t\u1ed1i \u01b0u h\u00f3a vi\u1ec7c hi\u1ec3n th\u1ecb danh s\u00e1ch l\u1edbn.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Gi\u1ea3m thi\u1ec3u vi\u1ec7c t\u00e1i t\u1ea1o v\u00e0 v\u1ebd l\u1ea1i giao di\u1ec7n kh\u00f4ng c\u1ea7n thi\u1ebft b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">DiffUtil<\/span><span style=\"font-weight: 400;\"> trong <\/span><span style=\"font-weight: 400;\">RecyclerView<\/span><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">\u00c1p d\u1ee5ng c\u00e1c k\u1ef9 thu\u1eadt nh\u01b0 lazy loading \u0111\u1ec3 t\u1ea3i d\u1eef li\u1ec7u ch\u1ec9 khi c\u1ea7n thi\u1ebft.<\/span><\/li>\n<\/ul>\n<p><b>Gi\u1ea3m thi\u1ec3u th\u1eddi gian kh\u1edfi \u0111\u1ed9ng \u1ee9ng d\u1ee5ng (App Startup):<\/b><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">T\u1ed1i \u01b0u h\u00f3a m\u00e3 ngu\u1ed3n trong <\/span><span style=\"font-weight: 400;\">onCreate()<\/span><span style=\"font-weight: 400;\"> c\u1ee7a <\/span><span style=\"font-weight: 400;\">Application<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">Activity<\/span><span style=\"font-weight: 400;\">, tr\u00e1nh th\u1ef1c hi\u1ec7n c\u00e1c c\u00f4ng vi\u1ec7c n\u1eb7ng trong giai \u0111o\u1ea1n kh\u1edfi t\u1ea1o.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">WorkManager<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">JobScheduler<\/span><span style=\"font-weight: 400;\"> cho c\u00e1c c\u00f4ng vi\u1ec7c n\u1ec1n thay v\u00ec th\u1ef1c hi\u1ec7n ngay l\u1eadp t\u1ee9c khi \u1ee9ng d\u1ee5ng kh\u1edfi \u0111\u1ed9ng.<\/span><\/li>\n<\/ul>\n<p><b>S\u1eed d\u1ee5ng Profiling v\u00e0 Monitoring:<\/b><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">D\u00f9ng Android Profiler v\u00e0 c\u00e1c c\u00f4ng c\u1ee5 profiling kh\u00e1c \u0111\u1ec3 theo d\u00f5i hi\u1ec7u su\u1ea5t c\u1ee7a \u1ee9ng d\u1ee5ng trong qu\u00e1 tr\u00ecnh ph\u00e1t tri\u1ec3n, t\u1eeb \u0111\u00f3 ph\u00e1t hi\u1ec7n c\u00e1c v\u1ea5n \u0111\u1ec1 v\u1ec1 b\u1ed9 nh\u1edb, CPU ho\u1eb7c th\u1eddi gian kh\u1edfi \u0111\u1ed9ng.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Ph\u00e2n t\u00edch logs \u0111\u1ec3 ph\u00e1t hi\u1ec7n c\u00e1c bottleneck v\u00e0 t\u1ed1i \u01b0u l\u1ea1i nh\u1eefng khu v\u1ef1c n\u00e0y.<\/span><\/li>\n<\/ul>\n<p><b>C\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t c\u1ee7a m\u1ea1ng:<\/b><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">D\u00f9ng th\u01b0 vi\u1ec7n nh\u01b0 <\/span><span style=\"font-weight: 400;\">Retrofit<\/span><span style=\"font-weight: 400;\"> k\u1ebft h\u1ee3p v\u1edbi <\/span><span style=\"font-weight: 400;\">OkHttp<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 t\u1ed1i \u01b0u h\u00f3a vi\u1ec7c giao ti\u1ebfp m\u1ea1ng, h\u1ea1n ch\u1ebf s\u1ed1 l\u01b0\u1ee3ng request v\u00e0 s\u1eed d\u1ee5ng cache hi\u1ec7u qu\u1ea3.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">LiveData<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">StateFlow<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c d\u1eef li\u1ec7u b\u1ea5t \u0111\u1ed3ng b\u1ed9 v\u00e0 gi\u1ea3m thi\u1ec3u vi\u1ec7c c\u1eadp nh\u1eadt UI kh\u00f4ng c\u1ea7n thi\u1ebft.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Nh\u1eefng chi\u1ebfn l\u01b0\u1ee3c n\u00e0y kh\u00f4ng ch\u1ec9 gi\u00fap \u1ee9ng d\u1ee5ng ho\u1ea1t \u0111\u1ed9ng nhanh ch\u00f3ng v\u00e0 hi\u1ec7u qu\u1ea3, m\u00e0 c\u00f2n \u0111\u1ea3m b\u1ea3o r\u1eb1ng m\u00e3 ngu\u1ed3n d\u1ec5 b\u1ea3o tr\u00ec v\u00e0 c\u00f3 th\u1ec3 m\u1edf r\u1ed9ng khi \u1ee9ng d\u1ee5ng ph\u00e1t tri\u1ec3n.<\/span><\/li>\n<\/ul>\n<h3><strong>Khi t\u1ed1i \u01b0u h\u00f3a \u1ee9ng d\u1ee5ng, b\u1ea1n s\u1eed d\u1ee5ng c\u00f4ng c\u1ee5 n\u00e0o \u0111\u1ec3 ph\u00e2n t\u00edch hi\u1ec7u su\u1ea5t code Kotlin?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t c\u1ee7a \u1ee9ng d\u1ee5ng Kotlin, t\u00f4i th\u01b0\u1eddng s\u1eed d\u1ee5ng m\u1ed9t s\u1ed1 c\u00f4ng c\u1ee5 ph\u00e2n t\u00edch hi\u1ec7u su\u1ea5t v\u00e0 t\u1ed1i \u01b0u m\u00e3 ngu\u1ed3n, bao g\u1ed3m:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><a href=\"https:\/\/developer.android.com\/studio\/profile?hl=vi\" target=\"_blank\" rel=\"noopener\"><b>Android Profiler<\/b><\/a><span style=\"font-weight: 400;\">: \u0110\u00e2y l\u00e0 c\u00f4ng c\u1ee5 t\u00edch h\u1ee3p trong Android Studio, cho ph\u00e9p t\u00f4i theo d\u00f5i hi\u1ec7u su\u1ea5t c\u1ee7a \u1ee9ng d\u1ee5ng trong th\u1eddi gian th\u1ef1c, t\u1eeb vi\u1ec7c s\u1eed d\u1ee5ng CPU, b\u1ed9 nh\u1edb \u0111\u1ebfn c\u00e1c ho\u1ea1t \u0111\u1ed9ng m\u1ea1ng. T\u00f4i s\u1eed d\u1ee5ng c\u00f4ng c\u1ee5 n\u00e0y \u0111\u1ec3 t\u00ecm ra nh\u1eefng \u0111o\u1ea1n m\u00e3 t\u1ed1n k\u00e9m v\u1ec1 t\u00e0i nguy\u00ean v\u00e0 t\u1ed1i \u01b0u h\u00f3a ch\u00fang.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Lint v\u00e0 KtLint<\/b><span style=\"font-weight: 400;\">: C\u00e1c c\u00f4ng c\u1ee5 n\u00e0y gi\u00fap t\u00f4i ph\u00e2n t\u00edch m\u00e3 ngu\u1ed3n Kotlin v\u00e0 \u0111\u1ea3m b\u1ea3o tu\u00e2n th\u1ee7 c\u00e1c quy t\u1eafc v\u1ec1 code style v\u00e0 hi\u1ec7u su\u1ea5t. Vi\u1ec7c tu\u00e2n th\u1ee7 c\u00e1c quy t\u1eafc gi\u00fap gi\u1ea3m thi\u1ec3u c\u00e1c l\u1ed7i kh\u00f4ng c\u1ea7n thi\u1ebft v\u00e0 c\u1ea3i thi\u1ec7n ch\u1ea5t l\u01b0\u1ee3ng m\u00e3 ngu\u1ed3n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><a href=\"https:\/\/firebase.google.com\/docs\/perf-mon\" target=\"_blank\" rel=\"noopener\"><b>Firebase Performance Monitoring<\/b><\/a><span style=\"font-weight: 400;\">: \u0110\u00e2y l\u00e0 c\u00f4ng c\u1ee5 tuy\u1ec7t v\u1eddi khi tri\u1ec3n khai c\u00e1c \u1ee9ng d\u1ee5ng tr\u00ean m\u00f4i tr\u01b0\u1eddng th\u1ef1c t\u1ebf. Firebase gi\u00fap theo d\u00f5i hi\u1ec7u su\u1ea5t c\u1ee7a \u1ee9ng d\u1ee5ng tr\u00ean c\u00e1c thi\u1ebft b\u1ecb c\u1ee7a ng\u01b0\u1eddi d\u00f9ng, t\u1eeb \u0111\u00f3 t\u00f4i c\u00f3 th\u1ec3 ph\u00e1t hi\u1ec7n c\u00e1c v\u1ea5n \u0111\u1ec1 hi\u1ec7u su\u1ea5t c\u00f3 th\u1ec3 xu\u1ea5t hi\u1ec7n trong qu\u00e1 tr\u00ecnh s\u1eed d\u1ee5ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><a href=\"https:\/\/square.github.io\/leakcanary\/\" target=\"_blank\" rel=\"noopener\"><b>LeakCanary<\/b><\/a><span style=\"font-weight: 400;\">: \u0110\u00e2y l\u00e0 th\u01b0 vi\u1ec7n gi\u00fap t\u00f4i ph\u00e1t hi\u1ec7n v\u00e0 s\u1eeda c\u00e1c v\u1ea5n \u0111\u1ec1 r\u00f2 r\u1ec9 b\u1ed9 nh\u1edb trong \u1ee9ng d\u1ee5ng Kotlin, \u0111i\u1ec1u n\u00e0y r\u1ea5t quan tr\u1ecdng \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o \u1ee9ng d\u1ee5ng kh\u00f4ng g\u1eb7p ph\u1ea3i t\u00ecnh tr\u1ea1ng ti\u00eau t\u1ed1n b\u1ed9 nh\u1edb kh\u00f4ng c\u1ea7n thi\u1ebft.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><a href=\"https:\/\/www.googleadservices.com\/pagead\/aclk\" target=\"_blank\" rel=\"noopener\"><b>Profiler c\u1ee7a JVM<\/b><\/a><span style=\"font-weight: 400;\">: N\u1ebfu \u1ee9ng d\u1ee5ng Kotlin c\u1ee7a t\u00f4i ch\u1ea1y tr\u00ean n\u1ec1n t\u1ea3ng JVM, t\u00f4i s\u1ebd s\u1eed d\u1ee5ng c\u00e1c c\u00f4ng c\u1ee5 nh\u01b0 VisualVM ho\u1eb7c YourKit \u0111\u1ec3 ph\u00e2n t\u00edch hi\u1ec7u su\u1ea5t c\u1ee7a m\u00e3 ngu\u1ed3n, t\u1eeb vi\u1ec7c ph\u00e2n t\u00edch c\u00e1c b\u1ed9 nh\u1edb heap \u0111\u1ebfn c\u00e1c thread v\u00e0 CPU usage.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">B\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng k\u1ebft h\u1ee3p c\u00e1c c\u00f4ng c\u1ee5 n\u00e0y, t\u00f4i c\u00f3 th\u1ec3 nh\u1eadn di\u1ec7n v\u00e0 t\u1ed1i \u01b0u h\u00f3a c\u00e1c v\u1ea5n \u0111\u1ec1 hi\u1ec7u su\u1ea5t trong \u1ee9ng d\u1ee5ng Kotlin, gi\u00fap n\u00e2ng cao tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng v\u00e0 ti\u1ebft ki\u1ec7m t\u00e0i nguy\u00ean h\u1ec7 th\u1ed1ng.<\/span><\/p>\n<h3><strong>Gi\u1ea3i th\u00edch v\u1ec1 boxed v\u00e0 unboxed type trong Kotlin<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, <\/span><b>boxed type<\/b><span style=\"font-weight: 400;\">\u00a0v\u00e0 <\/span><b>unboxed type<\/b><span style=\"font-weight: 400;\"> l\u00e0 c\u00e1c kh\u00e1i ni\u1ec7m li\u00ean quan \u0111\u1ebfn c\u00e1ch m\u00e0 c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef v\u00e0 x\u1eed l\u00fd trong b\u1ed9 nh\u1edb. C\u1ee5 th\u1ec3:<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Lo\u1ea1i\u00a0<\/b><\/td>\n<td><b>\u0110\u1ecbnh ngh\u0129a<\/b><\/td>\n<td><b>\u01afu \u0111i\u1ec3m\u00a0<\/b><\/td>\n<td><b>Nh\u01b0\u1ee3c \u0111i\u1ec3m\u00a0<\/b><\/td>\n<td><b>V\u00ed d\u1ee5\u00a0<\/b><\/td>\n<\/tr>\n<tr>\n<td><b>Unboxed type<\/b><\/td>\n<td><span style=\"font-weight: 400;\">L\u00e0 c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y (primitive types) nh\u01b0 Int, Double, Char, v.v., l\u01b0u tr\u1eef tr\u1ef1c ti\u1ebfp trong b\u1ed9 nh\u1edb m\u00e0 kh\u00f4ng c\u1ea7n l\u1edbp b\u1ecdc.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Ti\u1ebft ki\u1ec7m b\u1ed9 nh\u1edb v\u00e0 t\u1ed1c \u0111\u1ed9.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Kh\u00f4ng th\u1ec3 l\u01b0u tr\u1eef trong c\u00e1c c\u1ea5u tr\u00fac d\u1eef li\u1ec7u y\u00eau c\u1ea7u \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Int<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">Double<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">Char<\/span><span style=\"font-weight: 400;\">, v.v.<\/span><\/td>\n<\/tr>\n<tr>\n<td><b>Boxed type<\/b><\/td>\n<td><span style=\"font-weight: 400;\">L\u00e0 c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y \u0111\u01b0\u1ee3c b\u1ecdc trong m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng, v\u00ed d\u1ee5: <\/span><span style=\"font-weight: 400;\">Int<\/span><span style=\"font-weight: 400;\"> b\u1ecdc trong <\/span><span style=\"font-weight: 400;\">Integer<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">Double<\/span><span style=\"font-weight: 400;\"> b\u1ecdc trong <\/span><span style=\"font-weight: 400;\">Double<\/span><span style=\"font-weight: 400;\">.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 l\u01b0u tr\u1eef trong c\u00e1c c\u1ea5u tr\u00fac d\u1eef li\u1ec7u nh\u01b0 List, Set.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">T\u1ed1n b\u1ed9 nh\u1edb v\u00e0 hi\u1ec7u su\u1ea5t th\u1ea5p h\u01a1n do overhead t\u1eeb vi\u1ec7c t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 garbage collection.<\/span><\/td>\n<td><span style=\"font-weight: 400;\">Integer<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">Double<\/span><span style=\"font-weight: 400;\">, v.v.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><b>T\u1ea1i sao boxed v\u00e0 unboxed type quan tr\u1ecdng \u0111\u1ed1i v\u1edbi hi\u1ec7u su\u1ea5t?<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Hi\u1ec7u su\u1ea5t b\u1ed9 nh\u1edb<\/b><span style=\"font-weight: 400;\">: Khi s\u1eed d\u1ee5ng boxed types, m\u1ed7i gi\u00e1 tr\u1ecb y\u00eau c\u1ea7u m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng m\u1edbi \u0111\u1ec3 l\u01b0u tr\u1eef, g\u00e2y t\u1ed1n th\u00eam b\u1ed9 nh\u1edb. \u0110i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 g\u00e2y ra overhead \u0111\u00e1ng k\u1ec3 n\u1ebfu c\u00f3 qu\u00e1 nhi\u1ec1u gi\u00e1 tr\u1ecb c\u1ea7n \u0111\u01b0\u1ee3c x\u1eed l\u00fd. Ng\u01b0\u1ee3c l\u1ea1i, unboxed types ch\u1ec9 chi\u1ebfm m\u1ed9t l\u01b0\u1ee3ng b\u1ed9 nh\u1edb c\u1ed1 \u0111\u1ecbnh v\u00e0 hi\u1ec7u qu\u1ea3 h\u01a1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Hi\u1ec7u su\u1ea5t x\u1eed l\u00fd<\/b><span style=\"font-weight: 400;\">: Khi l\u00e0m vi\u1ec7c v\u1edbi boxed types, Kotlin ho\u1eb7c JVM ph\u1ea3i x\u1eed l\u00fd c\u00e1c thao t\u00e1c th\u00eam ph\u1ee9c t\u1ea1p nh\u01b0 t\u1ea1o v\u00e0 qu\u1ea3n l\u00fd c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng. \u0110i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 l\u00e0m gi\u1ea3m t\u1ed1c \u0111\u1ed9 th\u1ef1c thi, \u0111\u1eb7c bi\u1ec7t khi th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p to\u00e1n s\u1ed1 h\u1ecdc ho\u1eb7c x\u1eed l\u00fd v\u1edbi m\u1ed9t l\u01b0\u1ee3ng l\u1edbn d\u1eef li\u1ec7u.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Trong c\u00e1c t\u00ecnh hu\u1ed1ng c\u1ea7n t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t, vi\u1ec7c s\u1eed d\u1ee5ng unboxed types s\u1ebd l\u00e0 l\u1ef1a ch\u1ecdn t\u1ed1t h\u01a1n, v\u00ec ch\u00fang cho ph\u00e9p x\u1eed l\u00fd tr\u1ef1c ti\u1ebfp v\u00e0 nhanh h\u01a1n m\u00e0 kh\u00f4ng c\u00f3 overhead t\u1eeb vi\u1ec7c t\u1ea1o c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng b\u1ecdc.<\/span><\/p>\n<p><b>V\u00ed d\u1ee5:<\/b><\/p>\n<pre><span style=\"font-weight: 400;\">val unboxed: Int = 42\u00a0 \/\/ Int l\u00e0 unboxed type<\/span>\r\n<span style=\"font-weight: 400;\">val boxed: Int? = 42 \u00a0 \/\/ Int? l\u00e0 boxed type, v\u00ec n\u00f3 c\u00f3 th\u1ec3 l\u00e0 null v\u00e0 \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef nh\u01b0 \u0111\u1ed1i t\u01b0\u1ee3ng<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\/\/ Boxed type c\u00f3 th\u1ec3 g\u00e2y overhead h\u01a1n khi s\u1eed d\u1ee5ng trong c\u00e1c thao t\u00e1c v\u1edbi b\u1ed9 s\u01b0u t\u1eadp<\/span>\r\n<span style=\"font-weight: 400;\">val list: List&lt;Int?&gt; = listOf(42)\u00a0 \/\/ \u0110\u00e2y l\u00e0 m\u1ed9t boxed type v\u00ec Int? \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef d\u01b0\u1edbi d\u1ea1ng \u0111\u1ed1i t\u01b0\u1ee3ng<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">T\u00f3m l\u1ea1i, hi\u1ec3u v\u00e0 s\u1eed d\u1ee5ng \u0111\u00fang c\u00e1c boxed v\u00e0 unboxed types gi\u00fap b\u1ea1n t\u1ed1i \u01b0u hi\u1ec7u su\u1ea5t trong c\u00e1c \u1ee9ng d\u1ee5ng Kotlin, \u0111\u1eb7c bi\u1ec7t khi l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c ph\u00e9p to\u00e1n ho\u1eb7c c\u00e1c d\u1eef li\u1ec7u quy m\u00f4 l\u1edbn.<\/span><\/p>\n<h3><strong>C\u00e1c c\u00e1ch t\u1ed1i \u01b0u h\u00f3a khi s\u1eed d\u1ee5ng Collections trong Kotlin l\u00e0 g\u00ec?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Trong Kotlin, <\/span><b>Collections<\/b><span style=\"font-weight: 400;\"> l\u00e0 c\u00e1c c\u1ea5u tr\u00fac d\u1eef li\u1ec7u d\u00f9ng \u0111\u1ec3 l\u01b0u tr\u1eef v\u00e0 qu\u1ea3n l\u00fd nh\u00f3m c\u00e1c ph\u1ea7n t\u1eed, bao g\u1ed3m danh s\u00e1ch (List), t\u1eadp h\u1ee3p (Set) v\u00e0 b\u1ea3n \u0111\u1ed3 (Map). Kotlin cung c\u1ea5p m\u1ed9t lo\u1ea1t c\u00e1c API m\u1ea1nh m\u1ebd \u0111\u1ec3 thao t\u00e1c v\u1edbi Collections, h\u1ed7 tr\u1ee3 c\u00e1c thao t\u00e1c nh\u01b0 t\u00ecm ki\u1ebfm, l\u1ecdc, s\u1eafp x\u1ebfp v\u00e0 bi\u1ebfn \u0111\u1ed5i d\u1eef li\u1ec7u m\u1ed9t c\u00e1ch linh ho\u1ea1t v\u00e0 hi\u1ec7u qu\u1ea3.<\/span><\/p>\n<h4><b> S\u1eed d\u1ee5ng c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u ph\u00f9 h\u1ee3p<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>List<\/b><span style=\"font-weight: 400;\">: S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">ArrayList<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">mutableListOf()<\/span><span style=\"font-weight: 400;\"> khi c\u1ea7n thay \u0111\u1ed5i danh s\u00e1ch, ho\u1eb7c <\/span><span style=\"font-weight: 400;\">listOf()<\/span><span style=\"font-weight: 400;\"> n\u1ebfu kh\u00f4ng c\u1ea7n thay \u0111\u1ed5i (immutable).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Set<\/b><span style=\"font-weight: 400;\">: Ch\u1ecdn <\/span><span style=\"font-weight: 400;\">HashSet<\/span><span style=\"font-weight: 400;\"> khi c\u1ea7n t\u00ecm ki\u1ebfm nhanh, tr\u00e1nh vi\u1ec7c tr\u00f9ng l\u1eb7p ph\u1ea7n t\u1eed.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Map<\/b><span style=\"font-weight: 400;\">: D\u00f9ng <\/span><span style=\"font-weight: 400;\">HashMap<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">mutableMapOf()<\/span><span style=\"font-weight: 400;\"> khi c\u1ea7n l\u01b0u tr\u1eef c\u00e1c c\u1eb7p kh\u00f3a-gi\u00e1 tr\u1ecb, gi\u00fap truy c\u1eadp nhanh h\u01a1n th\u00f4ng qua kh\u00f3a.<\/span><\/li>\n<\/ul>\n<h4><b> Tr\u00e1nh s\u1eed d\u1ee5ng Collection kh\u00f4ng c\u1ea7n thi\u1ebft<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Tr\u00e1nh t\u1ea1o ra c\u00e1c Collection t\u1ea1m th\u1eddi trong khi x\u1eed l\u00fd d\u1eef li\u1ec7u. V\u00ed d\u1ee5, kh\u00f4ng c\u1ea7n ph\u1ea3i t\u1ea1o m\u1ed9t <\/span><span style=\"font-weight: 400;\">List<\/span><span style=\"font-weight: 400;\"> m\u1edbi khi b\u1ea1n c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n thao t\u00e1c tr\u00ean Collection ban \u0111\u1ea7u (d\u00f9ng tr\u1ef1c ti\u1ebfp c\u00e1c h\u00e0m 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;\"> thay v\u00ec <\/span><span style=\"font-weight: 400;\">toList()<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n<\/ul>\n<h4><b> S\u1eed d\u1ee5ng c\u00e1c ph\u01b0\u01a1ng th\u1ee9c m\u1edf r\u1ed9ng c\u1ee7a Collection hi\u1ec7u qu\u1ea3<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">D\u00f9ng c\u00e1c h\u00e0m 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;\">flatMap<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">fold<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">reduce<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 x\u1eed l\u00fd d\u1eef li\u1ec7u thay v\u00ec s\u1eed d\u1ee5ng v\u00f2ng l\u1eb7p truy\u1ec1n th\u1ed1ng. \u0110i\u1ec1u n\u00e0y gi\u00fap code ng\u1eafn g\u1ecdn, d\u1ec5 hi\u1ec3u v\u00e0 t\u1eadn d\u1ee5ng t\u1ed1i \u0111a kh\u1ea3 n\u0103ng c\u1ee7a Kotlin.<\/span><\/li>\n<\/ul>\n<h4><b> Tr\u00e1nh l\u1eb7p l\u1ea1i (redundant) vi\u1ec7c t\u00ednh to\u00e1n<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">N\u1ebfu b\u1ea1n c\u1ea7n s\u1eed d\u1ee5ng m\u1ed9t Collection nhi\u1ec1u l\u1ea7n, h\u00e3y xem x\u00e9t vi\u1ec7c t\u00ednh to\u00e1n k\u1ebft qu\u1ea3 m\u1ed9t l\u1ea7n v\u00e0 l\u01b0u tr\u1eef gi\u00e1 tr\u1ecb thay v\u00ec t\u00ednh l\u1ea1i nhi\u1ec1u l\u1ea7n, v\u00ed d\u1ee5: l\u01b0u tr\u1eef k\u1ebft qu\u1ea3 c\u1ee7a <\/span><span style=\"font-weight: 400;\">map<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">filter<\/span><span style=\"font-weight: 400;\"> trong m\u1ed9t bi\u1ebfn.<\/span><\/li>\n<\/ul>\n<h4><b> D\u00f9ng <\/b><b>sequence<\/b><b> thay v\u00ec <\/b><b>List<\/b><b> khi x\u1eed l\u00fd d\u1eef li\u1ec7u l\u1edbn<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Khi l\u00e0m vi\u1ec7c v\u1edbi d\u1eef li\u1ec7u l\u1edbn, thay v\u00ec s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">List<\/span><span style=\"font-weight: 400;\">, h\u00e3y s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">Sequence<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 gi\u1ea3m thi\u1ec3u vi\u1ec7c t\u1ea1o ra c\u00e1c Collection trung gian v\u00e0 gi\u1ea3m vi\u1ec7c t\u00ednh to\u00e1n l\u1ea1i. <\/span><span style=\"font-weight: 400;\">Sequence<\/span><span style=\"font-weight: 400;\"> ch\u1ec9 th\u1ef1c thi c\u00e1c thao t\u00e1c khi c\u1ea7n thi\u1ebft (lazy evaluation), gi\u00fap ti\u1ebft ki\u1ec7m b\u1ed9 nh\u1edb v\u00e0 t\u0103ng hi\u1ec7u su\u1ea5t.<\/span><\/li>\n<\/ul>\n<h4><b> T\u1ed1i \u01b0u h\u00f3a b\u1ed9 nh\u1edb<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Tr\u00e1nh vi\u1ec7c s\u1eed d\u1ee5ng qu\u00e1 nhi\u1ec1u \u0111\u1ed1i t\u01b0\u1ee3ng Collection, \u0111\u1eb7c bi\u1ec7t l\u00e0 trong c\u00e1c v\u00f2ng l\u1eb7p ho\u1eb7c khi kh\u00f4ng c\u1ea7n ph\u1ea3i thay \u0111\u1ed5i d\u1eef li\u1ec7u. H\u00e3y t\u1eadn d\u1ee5ng c\u00e1c ph\u01b0\u01a1ng th\u1ee9c trong Kotlin nh\u01b0 <\/span><span style=\"font-weight: 400;\">mutableListOf<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">mutableSetOf<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 thay \u0111\u1ed5i d\u1eef li\u1ec7u tr\u1ef1c ti\u1ebfp m\u00e0 kh\u00f4ng c\u1ea7n t\u1ea1o m\u1edbi Collection.<\/span><\/li>\n<\/ul>\n<h4><b> S\u1eed d\u1ee5ng <\/b><b>to<\/b><b> (Pairs) cho Map n\u1ebfu c\u00f3 \u00edt c\u1eb7p kh\u00f3a-gi\u00e1 tr\u1ecb<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">N\u1ebfu b\u1ea1n c\u1ea7n t\u1ea1o Map nh\u1ecf, s\u1eed d\u1ee5ng c\u00fa ph\u00e1p <\/span><span style=\"font-weight: 400;\">to<\/span><span style=\"font-weight: 400;\"> \u0111\u1ec3 t\u1ea1o ra c\u00e1c c\u1eb7p kh\u00f3a-gi\u00e1 tr\u1ecb m\u1ed9t c\u00e1ch ng\u1eafn g\u1ecdn v\u00e0 d\u1ec5 hi\u1ec3u (v\u00ed d\u1ee5: <\/span><span style=\"font-weight: 400;\">val map = listOf(&#8220;key1&#8221; to &#8220;value1&#8221;, &#8220;key2&#8221; to &#8220;value2&#8221;)<\/span><span style=\"font-weight: 400;\">).<\/span><\/li>\n<\/ul>\n<h4><b> Ph\u00e2n bi\u1ec7t gi\u1eefa <\/b><b>Immutable<\/b><b> v\u00e0 <\/b><b>Mutable<\/b><b> Collection<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u1ea3m b\u1ea3o r\u1eb1ng b\u1ea1n s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">Immutable<\/span><span style=\"font-weight: 400;\"> Collections (nh\u01b0 <\/span><span style=\"font-weight: 400;\">listOf()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">setOf()<\/span><span style=\"font-weight: 400;\">) khi kh\u00f4ng c\u1ea7n thay \u0111\u1ed5i d\u1eef li\u1ec7u, v\u00ec ch\u00fang gi\u00fap gi\u1ea3m r\u1ee7i ro v\u00e0 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c t\u1ed1i \u01b0u h\u00f3a t\u1ed1t h\u01a1n trong b\u1ed9 bi\u00ean d\u1ecbch.<\/span><\/li>\n<\/ul>\n<h4><b> C\u1ea9n th\u1eadn khi s\u1eed d\u1ee5ng Collection trong \u0111a lu\u1ed3ng (Concurrency)<\/b><\/h4>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">N\u1ebfu l\u00e0m vi\u1ec7c trong m\u00f4i tr\u01b0\u1eddng \u0111a lu\u1ed3ng, b\u1ea1n n\u00ean c\u1ea9n th\u1eadn khi truy c\u1eadp ho\u1eb7c thay \u0111\u1ed5i Collection. S\u1eed d\u1ee5ng c\u00e1c c\u1ea5u tr\u00fac d\u1eef li\u1ec7u \u0111\u1ed3ng b\u1ed9 nh\u01b0 <\/span><span style=\"font-weight: 400;\">CopyOnWriteArrayList<\/span><span style=\"font-weight: 400;\"> ho\u1eb7c <\/span><span style=\"font-weight: 400;\">SynchronizedList<\/span><span style=\"font-weight: 400;\"> n\u1ebfu c\u1ea7n.<\/span><\/li>\n<\/ul>\n<h3><strong>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 gi\u1ea3m chi ph\u00ed ph\u00e2n b\u1ed5 b\u1ed9 nh\u1edb khi s\u1eed d\u1ee5ng lambda expressions?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Khi s\u1eed d\u1ee5ng <\/span><b>lambda expressions<\/b><span style=\"font-weight: 400;\">, c\u00f3 m\u1ed9t s\u1ed1 nguy\u00ean nh\u00e2n d\u1eabn \u0111\u1ebfn t\u0103ng chi ph\u00ed ph\u00e2n b\u1ed5 b\u1ed9 nh\u1edb. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 c\u00e1c nguy\u00ean nh\u00e2n ch\u00ednh:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng \u1ea9n<\/b><span style=\"font-weight: 400;\">: Lambda expressions trong nhi\u1ec1u tr\u01b0\u1eddng h\u1ee3p s\u1ebd t\u1ea1o ra m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng \u1ea9n (anonymous class) \u0111\u1ec3 th\u1ef1c thi m\u00e3. \u0110i\u1ec1u n\u00e0y l\u00e0m t\u0103ng chi ph\u00ed b\u1ed9 nh\u1edb v\u00ec c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng n\u00e0y c\u1ea7n \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef trong heap.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Capture c\u1ee7a c\u00e1c bi\u1ebfn b\u00ean ngo\u00e0i (closure)<\/b><span style=\"font-weight: 400;\">: N\u1ebfu lambda expression s\u1eed d\u1ee5ng c\u00e1c bi\u1ebfn t\u1eeb ngo\u00e0i ph\u1ea1m vi c\u1ee7a n\u00f3 (closure), c\u00e1c bi\u1ebfn n\u00e0y s\u1ebd \u0111\u01b0\u1ee3c sao ch\u00e9p v\u00e0o m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng l\u01b0u tr\u1eef ri\u00eang bi\u1ec7t, \u0111i\u1ec1u n\u00e0y s\u1ebd l\u00e0m t\u0103ng m\u1ee9c \u0111\u1ed9 ph\u00e2n b\u1ed5 b\u1ed9 nh\u1edb.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Kh\u00f4ng t\u1ed1i \u01b0u h\u00f3a b\u1ed9 bi\u00ean d\u1ecbch (compiler optimizations)<\/b><span style=\"font-weight: 400;\">: M\u1ed9t s\u1ed1 tr\u00ecnh bi\u00ean d\u1ecbch kh\u00f4ng th\u1ec3 t\u1ed1i \u01b0u h\u00f3a lambda expressions hi\u1ec7u qu\u1ea3 nh\u01b0 c\u00e1c ph\u01b0\u01a1ng th\u1ee9c th\u00f4ng th\u01b0\u1eddng, d\u1eabn \u0111\u1ebfn overhead b\u1ed9 nh\u1edb cao h\u01a1n khi s\u1eed d\u1ee5ng ch\u00fang trong c\u00e1c v\u00f2ng l\u1eb7p ho\u1eb7c ph\u00e9p to\u00e1n th\u01b0\u1eddng xuy\u00ean.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>B\u1ea3n sao c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng l\u1edbn<\/b><span style=\"font-weight: 400;\">: Khi lambda expression s\u1eed d\u1ee5ng c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng l\u1edbn (v\u00ed d\u1ee5: Collections ho\u1eb7c c\u00e1c l\u1edbp d\u1eef li\u1ec7u ph\u1ee9c t\u1ea1p), ch\u00fang c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c sao ch\u00e9p v\u00e0 l\u01b0u tr\u1eef trong b\u1ed9 nh\u1edb, g\u00e2y ra chi ph\u00ed b\u1ed9 nh\u1edb cao.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Overhead c\u1ee7a Garbage Collection<\/b><span style=\"font-weight: 400;\">: Vi\u1ec7c t\u1ea1o ra c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng \u1ea9n ho\u1eb7c \u0111\u1ed1i t\u01b0\u1ee3ng closure cho m\u1ed7i lambda expression c\u00f3 th\u1ec3 l\u00e0m t\u0103ng s\u1ed1 l\u01b0\u1ee3ng \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ea7n \u0111\u01b0\u1ee3c qu\u1ea3n l\u00fd b\u1edfi garbage collector, d\u1eabn \u0111\u1ebfn chi ph\u00ed t\u00e0i nguy\u00ean b\u1ed9 nh\u1edb v\u00e0 th\u1eddi gian d\u1ecdn d\u1eb9p b\u1ed9 nh\u1edb.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Kh\u00f4ng t\u1eadn d\u1ee5ng \u0111\u01b0\u1ee3c inlining (n\u1ebfu kh\u00f4ng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng)<\/b><span style=\"font-weight: 400;\">: Trong Kotlin, n\u1ebfu lambda kh\u00f4ng th\u1ec3 \u0111\u01b0\u1ee3c inline (v\u00ed d\u1ee5: khi lambda l\u00e0 tham s\u1ed1 c\u1ee7a m\u1ed9t h\u00e0m kh\u00f4ng h\u1ed7 tr\u1ee3 inlining), m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng s\u1ebd \u0111\u01b0\u1ee3c t\u1ea1o ra thay v\u00ec ch\u1ec9 s\u1eed d\u1ee5ng m\u00e3, d\u1eabn \u0111\u1ebfn vi\u1ec7c ph\u00e2n b\u1ed5 b\u1ed9 nh\u1edb l\u1edbn h\u01a1n.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Nh\u1eefng nguy\u00ean nh\u00e2n tr\u00ean g\u00f3p ph\u1ea7n l\u00e0m t\u0103ng chi ph\u00ed b\u1ed9 nh\u1edb khi s\u1eed d\u1ee5ng lambda expressions trong l\u1eadp tr\u00ecnh, v\u00e0 vi\u1ec7c t\u1ed1i \u01b0u h\u00f3a c\u00e1ch s\u1eed d\u1ee5ng ch\u00fang l\u00e0 r\u1ea5t quan tr\u1ecdng \u0111\u1ec3 gi\u1ea3m thi\u1ec3u c\u00e1c v\u1ea5n \u0111\u1ec1 v\u1ec1 hi\u1ec7u su\u1ea5t.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 gi\u1ea3m chi ph\u00ed ph\u00e2n b\u1ed5 b\u1ed9 nh\u1edb khi s\u1eed d\u1ee5ng lambda expressions trong Kotlin, b\u1ea1n c\u00f3 th\u1ec3 \u00e1p d\u1ee5ng m\u1ed9t s\u1ed1 k\u1ef9 thu\u1eadt sau:<\/span><\/p>\n<h4><b>Tr\u00e1nh s\u1eed d\u1ee5ng lambda kh\u00f4ng c\u1ea7n thi\u1ebft<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">N\u1ebfu b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00e1c h\u00e0m th\u00f4ng th\u01b0\u1eddng ho\u1eb7c c\u00e1c ph\u01b0\u01a1ng th\u1ee9c \u0111\u00e3 \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a thay v\u00ec lambda, h\u00e3y s\u1eed d\u1ee5ng ch\u00fang \u0111\u1ec3 tr\u00e1nh chi ph\u00ed ph\u00e2n b\u1ed5 b\u1ed9 nh\u1edb. Lambda t\u1ea1o ra \u0111\u1ed1i t\u01b0\u1ee3ng m\u1edbi m\u1ed7i khi \u0111\u01b0\u1ee3c g\u1ecdi, \u0111i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 g\u00e2y t\u0103ng chi ph\u00ed b\u1ed9 nh\u1edb n\u1ebfu s\u1eed d\u1ee5ng kh\u00f4ng c\u1ea9n th\u1eadn.<\/span><\/p>\n<h4><b>S\u1eed d\u1ee5ng \u0111\u1ed1i t\u01b0\u1ee3ng <\/b><b>inline<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Kotlin h\u1ed7 tr\u1ee3 t\u1eeb kh\u00f3a <\/span><span style=\"font-weight: 400;\">inline<\/span><span style=\"font-weight: 400;\"> cho c\u00e1c h\u00e0m, \u0111i\u1ec1u n\u00e0y gi\u00fap b\u1ea1n tr\u00e1nh ph\u1ea3i t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng lambda trong b\u1ed9 nh\u1edb. Khi m\u1ed9t h\u00e0m \u0111\u01b0\u1ee3c \u0111\u00e1nh d\u1ea5u l\u00e0 <\/span><span style=\"font-weight: 400;\">inline<\/span><span style=\"font-weight: 400;\">, c\u00e1c lambda \u0111\u01b0\u1ee3c truy\u1ec1n v\u00e0o h\u00e0m \u0111\u00f3 s\u1ebd \u0111\u01b0\u1ee3c nh\u00fang tr\u1ef1c ti\u1ebfp v\u00e0o code c\u1ee7a h\u00e0m thay v\u00ec t\u1ea1o ra \u0111\u1ed1i t\u01b0\u1ee3ng m\u1edbi, gi\u00fap gi\u1ea3m thi\u1ec3u chi ph\u00ed ph\u00e2n b\u1ed5 b\u1ed9 nh\u1edb.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">inline fun doSomething(action: () -&gt; Unit) {<\/span>\r\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0action() \/\/ Lambda s\u1ebd \u0111\u01b0\u1ee3c nh\u00fang tr\u1ef1c ti\u1ebfp v\u00e0o \u0111\u00e2y, kh\u00f4ng t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng m\u1edbi<\/span>\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h4><b>Tr\u00e1nh d\u00f9ng <\/b><b>object<\/b><b> trong lambda<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">object<\/span><span style=\"font-weight: 400;\"> trong lambda \u0111\u1ec3 t\u1ea1o c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng s\u1ebd l\u00e0m t\u0103ng chi ph\u00ed b\u1ed9 nh\u1edb do m\u1ed7i l\u1ea7n g\u1ecdi lambda s\u1ebd t\u1ea1o ra m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng m\u1edbi. N\u1ebfu kh\u00f4ng c\u1ea7n thi\u1ebft, h\u00e3y tr\u00e1nh s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">object<\/span><span style=\"font-weight: 400;\"> trong lambda.<\/span><\/p>\n<h4><b>S\u1eed d\u1ee5ng t\u1ed1i \u0111a c\u00e1c h\u00e0m chu\u1ea9n (higher-order functions)<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Thay v\u00ec t\u1ea1o ra c\u00e1c lambda kh\u00f4ng c\u1ea7n thi\u1ebft, b\u1ea1n c\u00f3 th\u1ec3 t\u1eadn d\u1ee5ng c\u00e1c h\u00e0m chu\u1ea9n c\u1ee7a Kotlin \u0111\u1ec3 t\u1ed1i \u01b0u h\u00f3a. Kotlin c\u00f3 r\u1ea5t nhi\u1ec1u h\u00e0m chu\u1ea9n \u0111\u01b0\u1ee3c t\u1ed1i \u01b0u v\u1ec1 hi\u1ec7u n\u0103ng, gi\u00fap gi\u1ea3m chi ph\u00ed ph\u00e2n b\u1ed5 b\u1ed9 nh\u1edb.<\/span><\/p>\n<h4><b>Ch\u00fa \u00fd \u0111\u1ebfn ph\u1ea1m vi s\u1eed d\u1ee5ng lambda<\/b><\/h4>\n<p><span style=\"font-weight: 400;\">Lambda c\u00f3 th\u1ec3 gi\u1eef c\u00e1c bi\u1ebfn b\u00ean ngo\u00e0i ph\u1ea1m vi c\u1ee7a n\u00f3 (capturing variables), \u0111i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 g\u00e2y ra vi\u1ec7c gi\u1eef c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng kh\u00f4ng c\u1ea7n thi\u1ebft trong b\u1ed9 nh\u1edb. B\u1ea1n c\u1ea7n ch\u00fa \u00fd \u0111\u1ec3 gi\u1ea3m thi\u1ec3u vi\u1ec7c gi\u1eef l\u1ea1i c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng kh\u00f4ng c\u1ea7n thi\u1ebft b\u1eb1ng c\u00e1ch gi\u1eef ph\u1ea1m vi c\u1ee7a lambda c\u00e0ng h\u1eb9p c\u00e0ng t\u1ed1t.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T\u00f3m l\u1ea1i, \u0111\u1ec3 gi\u1ea3m chi ph\u00ed ph\u00e2n b\u1ed5 b\u1ed9 nh\u1edb khi s\u1eed d\u1ee5ng lambda expressions trong Kotlin, b\u1ea1n n\u00ean s\u1eed d\u1ee5ng c\u00e1c h\u00e0m <\/span><span style=\"font-weight: 400;\">inline<\/span><span style=\"font-weight: 400;\">, tr\u00e1nh s\u1eed d\u1ee5ng lambda kh\u00f4ng c\u1ea7n thi\u1ebft v\u00e0 h\u1ea1n ch\u1ebf vi\u1ec7c gi\u1eef c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng ngo\u00e0i ph\u1ea1m vi c\u1ee7a lambda.<\/span><\/p>\n<h3><strong>B\u1ea1n \u0111\u00e3 t\u1eebng l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c th\u01b0 vi\u1ec7n ho\u1eb7c c\u00f4ng c\u1ee5 nh\u01b0 R8 hay ProGuard ch\u01b0a? Kotlin c\u00f3 nh\u1eefng \u0111i\u1ec3m \u0111\u1eb7c bi\u1ec7t n\u00e0o c\u1ea7n l\u01b0u \u00fd khi s\u1eed d\u1ee5ng ch\u00fang?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">C\u00e1c c\u00f4ng c\u1ee5 k\u1ec3 tr\u00ean \u0111\u01b0\u1ee3c d\u00f9ng kh\u00e1 ph\u1ed5 bi\u1ebfn trong vi\u1ec7c t\u1ed1i \u01b0u h\u00f3a \u1ee9ng d\u1ee5ng v\u00e0 gi\u1ea3m k\u00edch th\u01b0\u1edbc APK. R8 v\u00e0 ProGuard \u0111\u1ec1u l\u00e0 c\u00f4ng c\u1ee5 gi\u00fap thu nh\u1ecf, t\u1ed1i \u01b0u m\u00e3 ngu\u1ed3n v\u00e0 lo\u1ea1i b\u1ecf c\u00e1c \u0111o\u1ea1n m\u00e3 kh\u00f4ng s\u1eed d\u1ee5ng, nh\u01b0ng R8 \u0111\u00e3 thay th\u1ebf ProGuard v\u00e0 cung c\u1ea5p hi\u1ec7u su\u1ea5t t\u1ed1i \u01b0u h\u01a1n, \u0111\u1ed3ng th\u1eddi t\u00edch h\u1ee3p t\u1ed1t h\u01a1n v\u1edbi Android Gradle Plugin.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Khi s\u1eed d\u1ee5ng <\/span><a href=\"https:\/\/developer.android.com\/build\/kotlin-support\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">R8 <\/span><\/a><span style=\"font-weight: 400;\">hay <\/span><a href=\"https:\/\/www.guardsquare.com\/manual\/languages\/kotlin\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">ProGuard<\/span><\/a> <span style=\"font-weight: 400;\">trong d\u1ef1 \u00e1n Kotlin, c\u00f3 m\u1ed9t s\u1ed1 \u0111i\u1ec3m \u0111\u1eb7c bi\u1ec7t c\u1ea7n l\u01b0u \u00fd:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>S\u1ef1 kh\u00e1c bi\u1ec7t v\u1ec1 reflection v\u00e0 code obfuscation:<\/b><span style=\"font-weight: 400;\"> Kotlin s\u1eed d\u1ee5ng nhi\u1ec1u t\u00ednh n\u0103ng ph\u1ea3n chi\u1ebfu (reflection) v\u00e0 c\u0169ng c\u00f3 c\u00e1c lo\u1ea1i l\u1edbp d\u1eef li\u1ec7u (data classes) m\u00e0 khi b\u1ecb t\u1ed1i \u01b0u h\u00f3a c\u00f3 th\u1ec3 g\u00e2y ra l\u1ed7i n\u1ebfu kh\u00f4ng c\u1ea5u h\u00ecnh \u0111\u00fang. C\u1ea7n ph\u1ea3i khai b\u00e1o r\u00f5 r\u00e0ng c\u00e1c l\u1edbp, h\u00e0m ho\u1eb7c thu\u1ed9c t\u00ednh c\u1ea7n gi\u1eef l\u1ea1i trong qu\u00e1 tr\u00ecnh obfuscation b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">-keep<\/span><span style=\"font-weight: 400;\"> trong c\u1ea5u h\u00ecnh ProGuard ho\u1eb7c R8.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Extension functions:<\/b><span style=\"font-weight: 400;\"> C\u00e1c extension functions trong Kotlin c\u00f3 th\u1ec3 b\u1ecb lo\u1ea1i b\u1ecf n\u1ebfu kh\u00f4ng \u0111\u01b0\u1ee3c gi\u1eef l\u1ea1i trong c\u1ea5u h\u00ecnh ProGuard\/R8, v\u00ec ch\u00fang kh\u00f4ng ph\u1ea3i l\u00e0 ph\u1ea7n c\u1ee7a l\u1edbp g\u1ed1c. B\u1ea1n c\u1ea7n \u0111\u1ea3m b\u1ea3o c\u00e1c extension functions quan tr\u1ecdng \u0111\u01b0\u1ee3c gi\u1eef l\u1ea1i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Null safety v\u00e0 t\u00ednh t\u01b0\u01a1ng th\u00edch:<\/b><span style=\"font-weight: 400;\"> Kotlin c\u00f3 h\u1ec7 th\u1ed1ng null safety, v\u00e0 khi s\u1eed d\u1ee5ng ProGuard hay R8, c\u00e1c thay \u0111\u1ed5i c\u00f3 th\u1ec3 \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn c\u00e1ch x\u1eed l\u00fd null trong m\u00e3 c\u1ee7a b\u1ea1n. \u0110\u1ea3m b\u1ea3o c\u1ea5u h\u00ecnh c\u1ee7a b\u1ea1n kh\u00f4ng x\u00f3a ho\u1eb7c t\u1ed1i \u01b0u h\u00f3a c\u00e1c \u0111o\u1ea1n m\u00e3 li\u00ean quan \u0111\u1ebfn vi\u1ec7c ki\u1ec3m tra null.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Kotlin synthetic properties v\u00e0 View Binding:<\/b><span style=\"font-weight: 400;\"> N\u1ebfu b\u1ea1n \u0111ang s\u1eed d\u1ee5ng c\u00e1c t\u00ednh n\u0103ng nh\u01b0 synthetic properties (trong tr\u01b0\u1edbc \u0111\u00e2y) ho\u1eb7c View Binding, c\u1ea7n ph\u1ea3i c\u1ea5u h\u00ecnh ProGuard\/R8 \u0111\u1ec3 gi\u1eef l\u1ea1i c\u00e1c ph\u01b0\u01a1ng th\u1ee9c v\u00e0 tr\u01b0\u1eddng h\u1ee3p li\u00ean quan, tr\u00e1nh b\u1ecb x\u00f3a nh\u1ea7m trong qu\u00e1 tr\u00ecnh obfuscation.<\/span><\/li>\n<\/ol>\n<h3><strong>Kotlin Native c\u00f3 c\u00e1c h\u1ea1n ch\u1ebf n\u00e0o v\u1ec1 hi\u1ec7u su\u1ea5t so v\u1edbi Kotlin JVM? L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 t\u1ed1i \u01b0u h\u00f3a khi ph\u00e1t tri\u1ec3n \u0111a n\u1ec1n t\u1ea3ng?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">Kotlin Native c\u00f3 m\u1ed9t s\u1ed1 h\u1ea1n ch\u1ebf v\u1ec1 hi\u1ec7u su\u1ea5t khi so v\u1edbi Kotlin JVM, do s\u1ef1 kh\u00e1c bi\u1ec7t trong c\u00e1ch ho\u1ea1t \u0111\u1ed9ng c\u1ee7a ch\u00fang. C\u00e1c h\u1ea1n ch\u1ebf ch\u00ednh bao g\u1ed3m:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Qu\u1ea3n l\u00fd b\u1ed9 nh\u1edb<\/b><span style=\"font-weight: 400;\">: Kotlin JVM s\u1eed d\u1ee5ng Garbage Collector (GC), gi\u00fap t\u1ef1 \u0111\u1ed9ng qu\u1ea3n l\u00fd b\u1ed9 nh\u1edb, trong khi Kotlin Native kh\u00f4ng c\u00f3 GC m\u00e0 s\u1eed d\u1ee5ng c\u01a1 ch\u1ebf qu\u1ea3n l\u00fd b\u1ed9 nh\u1edb th\u1ee7 c\u00f4ng. \u0110i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 l\u00e0m cho vi\u1ec7c ph\u00e1t tri\u1ec3n trong Kotlin Native tr\u1edf n\u00ean ph\u1ee9c t\u1ea1p h\u01a1n v\u00e0 c\u1ea7n ph\u1ea3i ch\u00fa \u00fd \u0111\u1ebfn vi\u1ec7c gi\u1ea3i ph\u00f3ng b\u1ed9 nh\u1edb \u0111\u00fang c\u00e1ch \u0111\u1ec3 tr\u00e1nh r\u00f2 r\u1ec9 b\u1ed9 nh\u1edb.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Kh\u1ea3 n\u0103ng t\u1ed1i \u01b0u h\u00f3a<\/b><span style=\"font-weight: 400;\">: Kotlin JVM c\u00f3 th\u1ec3 t\u1eadn d\u1ee5ng c\u00e1c t\u1ed1i \u01b0u h\u00f3a m\u1ea1nh m\u1ebd c\u1ee7a JVM, nh\u01b0 JIT (Just-In-Time) compiler, gi\u00fap c\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t khi ch\u1ea1y m\u00e3. Kotlin Native, m\u1eb7c d\u00f9 c\u00f3 AOT (Ahead-Of-Time) compilation, nh\u01b0ng c\u00f3 th\u1ec3 g\u1eb7p kh\u00f3 kh\u0103n h\u01a1n trong vi\u1ec7c t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t nh\u01b0 JVM.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u01b0\u01a1ng th\u00edch th\u01b0 vi\u1ec7n<\/b><span style=\"font-weight: 400;\">: Kotlin JVM c\u00f3 th\u1ec3 t\u1eadn d\u1ee5ng m\u1ed9t h\u1ec7 sinh th\u00e1i th\u01b0 vi\u1ec7n phong ph\u00fa t\u1eeb Java, gi\u00fap t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t trong nhi\u1ec1u t\u00ecnh hu\u1ed1ng. Kotlin Native, ng\u01b0\u1ee3c l\u1ea1i, c\u1ea7n ph\u1ea3i k\u1ebft n\u1ed1i v\u1edbi c\u00e1c th\u01b0 vi\u1ec7n C ho\u1eb7c s\u1eed d\u1ee5ng c\u00e1c th\u01b0 vi\u1ec7n chuy\u00ean bi\u1ec7t cho n\u1ec1n t\u1ea3ng c\u1ee7a n\u00f3, \u0111i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 gi\u1edbi h\u1ea1n kh\u1ea3 n\u0103ng t\u1ed1i \u01b0u h\u00f3a.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Khi ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng \u0111a n\u1ec1n t\u1ea3ng v\u1edbi Kotlin (Kotlin Multiplatform), c\u00f3 m\u1ed9t s\u1ed1 c\u00e1ch \u0111\u1ec3 t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Chia s\u1ebb m\u00e3 logic gi\u1eefa c\u00e1c n\u1ec1n t\u1ea3ng<\/b><span style=\"font-weight: 400;\">: Th\u1ef1c hi\u1ec7n vi\u1ec7c chia s\u1ebb logic m\u00e3 gi\u1eefa c\u00e1c n\u1ec1n t\u1ea3ng (iOS, Android, desktop, backend) m\u1ed9t c\u00e1ch h\u1ee3p l\u00fd \u0111\u1ec3 tr\u00e1nh s\u1ef1 tr\u00f9ng l\u1eb7p v\u00e0 gi\u1ea3m thi\u1ec3u kh\u1ed1i l\u01b0\u1ee3ng m\u00e3 c\u1ea7n t\u1ed1i \u01b0u h\u00f3a cho t\u1eebng n\u1ec1n t\u1ea3ng ri\u00eang bi\u1ec7t.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>S\u1eed d\u1ee5ng expect\/actual<\/b><span style=\"font-weight: 400;\">: Kotlin Multiplatform cung c\u1ea5p c\u01a1 ch\u1ebf <\/span><span style=\"font-weight: 400;\">expect\/actual<\/span><span style=\"font-weight: 400;\">, cho ph\u00e9p b\u1ea1n t\u1ed1i \u01b0u h\u00f3a m\u00e3 cho t\u1eebng n\u1ec1n t\u1ea3ng ri\u00eang bi\u1ec7t m\u00e0 kh\u00f4ng \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn ph\u1ea7n c\u00f2n l\u1ea1i c\u1ee7a \u1ee9ng d\u1ee5ng. B\u1ea1n c\u00f3 th\u1ec3 vi\u1ebft m\u00e3 \u0111\u1eb7c th\u00f9 cho n\u1ec1n t\u1ea3ng khi c\u1ea7n thi\u1ebft, v\u00ed d\u1ee5, t\u1ed1i \u01b0u h\u00f3a vi\u1ec7c x\u1eed l\u00fd h\u00ecnh \u1ea3nh hay m\u1ea1ng tr\u00ean m\u1ecdi n\u1ec1n t\u1ea3ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Tr\u00e1nh s\u1eed d\u1ee5ng t\u00ednh n\u0103ng kh\u00f4ng c\u1ea7n thi\u1ebft<\/b><span style=\"font-weight: 400;\">: C\u1ed1 g\u1eafng tr\u00e1nh s\u1eed d\u1ee5ng c\u00e1c t\u00ednh n\u0103ng ho\u1eb7c th\u01b0 vi\u1ec7n kh\u00f4ng c\u1ea7n thi\u1ebft trong d\u1ef1 \u00e1n, \u0111\u1eb7c bi\u1ec7t l\u00e0 khi l\u00e0m vi\u1ec7c v\u1edbi Kotlin Native, v\u00ec ch\u00fang c\u00f3 th\u1ec3 l\u00e0m t\u0103ng k\u00edch th\u01b0\u1edbc m\u00e3 v\u00e0 \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn hi\u1ec7u su\u1ea5t.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Profiling v\u00e0 Benchmarking<\/b><span style=\"font-weight: 400;\">: S\u1eed d\u1ee5ng c\u00e1c c\u00f4ng c\u1ee5 profiling v\u00e0 benchmarking \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh c\u00e1c ph\u1ea7n m\u00e3 c\u00f3 th\u1ec3 g\u00e2y ra v\u1ea5n \u0111\u1ec1 v\u1ec1 hi\u1ec7u su\u1ea5t. Th\u1ef1c hi\u1ec7n ki\u1ec3m tra hi\u1ec7u su\u1ea5t \u0111\u1ecbnh k\u1ef3 v\u00e0 t\u1ed1i \u01b0u h\u00f3a c\u00e1c ph\u1ea7n m\u00e3 b\u1ecb ch\u1eadm ho\u1eb7c g\u00e2y t\u1ed1n t\u00e0i nguy\u00ean.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>C\u00e2n nh\u1eafc vi\u1ec7c s\u1eed d\u1ee5ng Kotlin\/Native<\/b><span style=\"font-weight: 400;\">: Trong c\u00e1c t\u00ecnh hu\u1ed1ng y\u00eau c\u1ea7u t\u1ed1i \u01b0u h\u00f3a t\u1ed1i \u0111a v\u1ec1 hi\u1ec7u su\u1ea5t v\u00e0 khi ph\u00e1t tri\u1ec3n tr\u00ean c\u00e1c n\u1ec1n t\u1ea3ng kh\u00f4ng ph\u1ea3i JVM (nh\u01b0 iOS), s\u1eed d\u1ee5ng Kotlin\/Native c\u00f3 th\u1ec3 l\u00e0 s\u1ef1 l\u1ef1a ch\u1ecdn t\u1ed1t h\u01a1n. Tuy nhi\u00ean, h\u00e3y c\u00e2n nh\u1eafc k\u1ef9 tr\u01b0\u1edbc khi s\u1eed d\u1ee5ng v\u00ec n\u00f3 \u0111\u00f2i h\u1ecfi th\u00eam c\u00f4ng s\u1ee9c t\u1ed1i \u01b0u h\u00f3a.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">B\u1eb1ng c\u00e1ch \u00e1p d\u1ee5ng nh\u1eefng k\u1ef9 thu\u1eadt n\u00e0y, b\u1ea1n c\u00f3 th\u1ec3 t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t v\u00e0 t\u1eadn d\u1ee5ng t\u1ed1t nh\u1ea5t Kotlin trong ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng \u0111a n\u1ec1n t\u1ea3ng.<\/span><\/p>\n<h3><strong>B\u1ea1n c\u00f3 kinh nghi\u1ec7m l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c k\u1ef9 thu\u1eadt t\u1ed1i \u01b0u h\u00f3a code Kotlin d\u00e0nh cho m\u00e1y \u1ea3o (JVM) kh\u00f4ng?<\/strong><\/h3>\n<p><span style=\"font-weight: 400;\">M\u1ed9t s\u1ed1 k\u1ef9 thu\u1eadt th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng bao g\u1ed3m:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u1ed1i \u01b0u h\u00f3a qu\u1ea3n l\u00fd b\u1ed9 nh\u1edb:<\/b><span style=\"font-weight: 400;\"> T\u00f4i ch\u00fa tr\u1ecdng \u0111\u1ebfn vi\u1ec7c gi\u1ea3m thi\u1ec3u s\u1eed d\u1ee5ng b\u1ed9 nh\u1edb th\u00f4ng qua vi\u1ec7c h\u1ea1n ch\u1ebf vi\u1ec7c t\u1ea1o c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng kh\u00f4ng c\u1ea7n thi\u1ebft v\u00e0 s\u1eed d\u1ee5ng c\u00e1c k\u1ef9 thu\u1eadt nh\u01b0 <\/span><i><span style=\"font-weight: 400;\">object pooling<\/span><\/i><span style=\"font-weight: 400;\"> khi c\u1ea7n thi\u1ebft. Th\u00eam v\u00e0o \u0111\u00f3, t\u00f4i c\u0169ng s\u1eed d\u1ee5ng c\u00e1c c\u01a1 ch\u1ebf nh\u01b0 <\/span><i><span style=\"font-weight: 400;\">lazy initialization<\/span><\/i><span style=\"font-weight: 400;\"> \u0111\u1ec3 ch\u1ec9 t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng khi th\u1ef1c s\u1ef1 c\u1ea7n thi\u1ebft.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>S\u1eed d\u1ee5ng c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u b\u1ea5t bi\u1ebfn (Immutable data types):<\/b><span style=\"font-weight: 400;\"> Vi\u1ec7c s\u1eed d\u1ee5ng c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u b\u1ea5t bi\u1ebfn gi\u00fap gi\u1ea3m thi\u1ec3u l\u1ed7i li\u00ean quan \u0111\u1ebfn thay \u0111\u1ed5i tr\u1ea1ng th\u00e1i kh\u00f4ng mong mu\u1ed1n v\u00e0 \u0111\u1ed3ng th\u1eddi c\u1ea3i thi\u1ec7n hi\u1ec7u n\u0103ng khi JVM t\u1ed1i \u01b0u h\u00f3a cho c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng b\u1ea5t bi\u1ebfn.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u1ed1i \u01b0u h\u00f3a v\u00f2ng l\u1eb7p v\u00e0 x\u1eed l\u00fd t\u1eadp h\u1ee3p:<\/b><span style=\"font-weight: 400;\"> T\u00f4i s\u1eed d\u1ee5ng c\u00e1c ph\u01b0\u01a1ng th\u1ee9c hi\u1ec7u qu\u1ea3 h\u01a1n \u0111\u1ec3 x\u1eed l\u00fd t\u1eadp h\u1ee3p d\u1eef li\u1ec7u, ch\u1eb3ng h\u1ea1n nh\u01b0 s\u1eed d\u1ee5ng <\/span><span style=\"font-weight: 400;\">map<\/span><span style=\"font-weight: 400;\"> v\u00e0 <\/span><span style=\"font-weight: 400;\">filter<\/span><span style=\"font-weight: 400;\"> thay v\u00ec c\u00e1c v\u00f2ng l\u1eb7p th\u1ee7 c\u00f4ng, \u0111\u1ec3 JVM c\u00f3 th\u1ec3 t\u1ed1i \u01b0u h\u00f3a v\u00e0 gi\u1ea3m thi\u1ec3u chi ph\u00ed t\u00e0i nguy\u00ean.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>S\u1eed d\u1ee5ng <\/b><b><i>inline functions<\/i><\/b><b> v\u00e0 <\/b><b><i>reified types<\/i><\/b><b> khi c\u1ea7n:<\/b><span style=\"font-weight: 400;\"> T\u00f4i t\u1eadn d\u1ee5ng c\u00e1c h\u00e0m inline \u0111\u1ec3 tr\u00e1nh overhead do g\u1ecdi h\u00e0m, \u0111\u1eb7c bi\u1ec7t l\u00e0 khi l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c h\u00e0m nh\u1ecf ho\u1eb7c c\u00e1c h\u00e0m th\u01b0\u1eddng xuy\u00ean \u0111\u01b0\u1ee3c g\u1ecdi trong \u1ee9ng d\u1ee5ng.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>T\u1ed1i \u01b0u h\u00f3a s\u1eed d\u1ee5ng c\u00e1c t\u00ednh n\u0103ng c\u1ee7a Kotlin v\u00e0 JVM:<\/b><span style=\"font-weight: 400;\"> T\u00f4i lu\u00f4n theo d\u00f5i v\u00e0 \u00e1p d\u1ee5ng c\u00e1c b\u1ea3n c\u1eadp nh\u1eadt m\u1edbi t\u1eeb Kotlin v\u00e0 JVM \u0111\u1ec3 t\u1eadn d\u1ee5ng c\u00e1c c\u1ea3i ti\u1ebfn hi\u1ec7u su\u1ea5t, v\u00ed d\u1ee5 nh\u01b0 c\u00e1c t\u1ed1i \u01b0u h\u00f3a trong vi\u1ec7c x\u1eed l\u00fd <\/span><span style=\"font-weight: 400;\">coroutines<\/span> <span style=\"font-weight: 400;\">hay gi\u1ea3m \u0111\u1ed9 tr\u1ec5 khi s\u1eed d\u1ee5ng c\u00e1c t\u00ednh n\u0103ng nh\u01b0 <\/span><a href=\"https:\/\/kotlinlang.org\/docs\/composing-suspending-functions.html\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">suspend functions<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Ch\u1ea1y v\u00e0 ph\u00e2n t\u00edch m\u00e3 v\u1edbi c\u00f4ng c\u1ee5 profiling:<\/b><span style=\"font-weight: 400;\"> T\u00f4i s\u1eed d\u1ee5ng c\u00e1c c\u00f4ng c\u1ee5 nh\u01b0 <\/span><i><span style=\"font-weight: 400;\">JVM Profiler<\/span><\/i><span style=\"font-weight: 400;\"> v\u00e0 <\/span><i><span style=\"font-weight: 400;\">VisualVM<\/span><\/i><span style=\"font-weight: 400;\"> \u0111\u1ec3 theo d\u00f5i hi\u1ec7u n\u0103ng c\u1ee7a \u1ee9ng d\u1ee5ng v\u00e0 x\u00e1c \u0111\u1ecbnh c\u00e1c \u0111i\u1ec3m ngh\u1ebdn ti\u1ec1m \u1ea9n trong m\u00e3, sau \u0111\u00f3 t\u00f4i s\u1ebd t\u1ed1i \u01b0u h\u00f3a nh\u1eefng khu v\u1ef1c n\u00e0y.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">T\u00f4i hi\u1ec3u r\u1eb1ng vi\u1ec7c t\u1ed1i \u01b0u h\u00f3a \u1ee9ng d\u1ee5ng Kotlin tr\u00ean JVM kh\u00f4ng ch\u1ec9 d\u1eebng l\u1ea1i \u1edf vi\u1ec7c t\u1ed1i \u01b0u h\u00f3a m\u00e3 ngu\u1ed3n m\u00e0 c\u00f2n li\u00ean quan \u0111\u1ebfn vi\u1ec7c hi\u1ec3u r\u00f5 c\u00e1c t\u00ednh n\u0103ng c\u1ee7a JVM, c\u00e1ch b\u1ed9 thu gom r\u00e1c (<\/span><a href=\"https:\/\/kotlinlang.org\/api\/latest\/jvm\/stdlib\/kotlin.native.runtime\/-g-c\/\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">garbage collector<\/span><\/a><span style=\"font-weight: 400;\">) ho\u1ea1t \u0111\u1ed9ng, v\u00e0 c\u00e1ch \u1ee9ng d\u1ee5ng c\u00f3 th\u1ec3 t\u1eadn d\u1ee5ng t\u1ed1i \u0111a c\u00e1c t\u00e0i nguy\u00ean h\u1ec7 th\u1ed1ng.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Tong_ket_cau_hoi_phong_van_Kotlin\"><\/span><strong>T\u1ed5ng k\u1ebft c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Trong b\u00e0i vi\u1ebft n\u00e0y, ch\u00fang ta \u0111\u00e3 c\u00f9ng nhau kh\u00e1m ph\u00e1 m\u1ed9t s\u1ed1 c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin ph\u1ed5 bi\u1ebfn d\u00e0nh cho c\u00e1c l\u1eadp tr\u00ecnh vi\u00ean Kotlin. Nh\u1eefng c\u00e2u h\u1ecfi n\u00e0y kh\u00f4ng ch\u1ec9 gi\u00fap nh\u00e0 tuy\u1ec3n d\u1ee5ng \u0111\u00e1nh gi\u00e1 kh\u1ea3 n\u0103ng k\u1ef9 thu\u1eadt c\u1ee7a \u1ee9ng vi\u00ean m\u00e0 c\u00f2n ph\u1ea3n \u00e1nh c\u00e1ch \u1ee9ng vi\u00ean ti\u1ebfp c\u1eadn v\u00e0 gi\u1ea3i quy\u1ebft v\u1ea5n \u0111\u1ec1 trong l\u1eadp tr\u00ecnh.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Tuy nhi\u00ean, kh\u00f4ng ch\u1ec9 d\u1eebng l\u1ea1i \u1edf l\u00fd thuy\u1ebft, vi\u1ec7c chu\u1ea9n b\u1ecb tr\u1ea3 l\u1eddi nh\u1eefng c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin n\u00e0y c\u00f2n gi\u00fap b\u1ea1n n\u00e2ng cao k\u1ef9 n\u0103ng t\u01b0 duy logic v\u00e0 ph\u1ea3n x\u1ea1 nhanh ch\u00f3ng trong nh\u1eefng t\u00ecnh hu\u1ed1ng th\u1ef1c t\u1ebf. Quan tr\u1ecdng h\u01a1n, nh\u00e0 tuy\u1ec3n d\u1ee5ng kh\u00f4ng ch\u1ec9 mu\u1ed1n th\u1ea5y ki\u1ebfn th\u1ee9c v\u1eefng v\u00e0ng m\u00e0 c\u00f2n mu\u1ed1n hi\u1ec3u r\u00f5 c\u00e1ch b\u1ea1n l\u00e0m vi\u1ec7c, c\u00e1ch b\u1ea1n ph\u00e1t tri\u1ec3n c\u00e1c gi\u1ea3i ph\u00e1p s\u00e1ng t\u1ea1o v\u00e0 kh\u1ea3 n\u0103ng l\u00e0m vi\u1ec7c nh\u00f3m hi\u1ec7u qu\u1ea3.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Hy v\u1ecdng qua b\u00e0i vi\u1ebft n\u00e0y, b\u1ea1n s\u1ebd c\u00f3 c\u00e1i nh\u00ecn r\u00f5 r\u00e0ng h\u01a1n v\u1ec1 nh\u1eefng g\u00ec c\u1ea7n chu\u1ea9n b\u1ecb khi \u1ee9ng tuy\u1ec3n v\u00e0o v\u1ecb tr\u00ed Kotlin Developer v\u00e0 t\u1ef1 tin \u0111\u1ed1i m\u1eb7t v\u1edbi c\u00e1c c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>B\u00e0i vi\u1ebft t\u1ed5ng h\u1ee3p 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin c\u00f9ng c\u00e2u tr\u1ea3 l\u1eddi c\u1ef1c k\u1ef3 chi ti\u1ebft. Th\u00f4ng qua c\u00e1c c\u00e2u h\u1ecfi v\u1ec1 c\u00fa ph\u00e1p, th\u01b0 vi\u1ec7n, framework, v\u00e0 c\u00e1c t\u00ednh n\u0103ng \u0111\u1eb7c tr\u01b0ng c\u1ee7a Kotlin, \u1ee9ng vi\u00ean c\u00f3 th\u1ec3 th\u1ec3 hi\u1ec7n s\u1ef1 hi\u1ec3u bi\u1ebft s\u00e2u s\u1eafc v\u1ec1 ng\u00f4n ng\u1eef n\u00e0y v\u00e0 kh\u1ea3 n\u0103ng [&hellip;]<\/p>\n","protected":false},"author":203,"featured_media":82283,"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-82079","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.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Top 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin s\u1ebd g\u1eb7p trong bu\u1ed5i ph\u1ecfng v\u1ea5n - ITviec Blog<\/title>\n<meta name=\"description\" content=\"B\u00e0i vi\u1ebft t\u1ed5ng h\u1ee3p 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin k\u00e8m tr\u1ea3 l\u1eddi chi ti\u1ebft, v\u1ec1 c\u00fa ph\u00e1p, th\u01b0 vi\u1ec7n, framework, v\u00e0 c\u00e1c t\u00ednh n\u0103ng \u0111\u1eb7c tr\u01b0ng c\u1ee7a Kotlin.\" \/>\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-kotlin\/\" \/>\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 Kotlin s\u1ebd g\u1eb7p trong bu\u1ed5i ph\u1ecfng v\u1ea5n\" \/>\n<meta property=\"og:description\" content=\"B\u00e0i vi\u1ebft t\u1ed5ng h\u1ee3p 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin c\u00f9ng c\u00e2u tr\u1ea3 l\u1eddi c\u1ef1c k\u1ef3 chi ti\u1ebft. Th\u00f4ng qua c\u00e1c c\u00e2u h\u1ecfi v\u1ec1 c\u00fa ph\u00e1p, th\u01b0 vi\u1ec7n, framework, v\u00e0 c\u00e1c t\u00ednh n\u0103ng\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-kotlin\/\" \/>\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-29T03:44:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-29T04:09:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Kotlin-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=\"71 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Top 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin s\u1ebd g\u1eb7p trong bu\u1ed5i ph\u1ecfng v\u1ea5n - ITviec Blog","description":"B\u00e0i vi\u1ebft t\u1ed5ng h\u1ee3p 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin k\u00e8m tr\u1ea3 l\u1eddi chi ti\u1ebft, v\u1ec1 c\u00fa ph\u00e1p, th\u01b0 vi\u1ec7n, framework, v\u00e0 c\u00e1c t\u00ednh n\u0103ng \u0111\u1eb7c tr\u01b0ng c\u1ee7a Kotlin.","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-kotlin\/","og_locale":"vi_VN","og_type":"article","og_title":"Top 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin s\u1ebd g\u1eb7p trong bu\u1ed5i ph\u1ecfng v\u1ea5n","og_description":"B\u00e0i vi\u1ebft t\u1ed5ng h\u1ee3p 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin c\u00f9ng c\u00e2u tr\u1ea3 l\u1eddi c\u1ef1c k\u1ef3 chi ti\u1ebft. Th\u00f4ng qua c\u00e1c c\u00e2u h\u1ecfi v\u1ec1 c\u00fa ph\u00e1p, th\u01b0 vi\u1ec7n, framework, v\u00e0 c\u00e1c t\u00ednh n\u0103ng","og_url":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-kotlin\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2024-11-29T03:44:51+00:00","article_modified_time":"2024-11-29T04:09:30+00:00","og_image":[{"width":1500,"height":790,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Kotlin-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":"71 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-kotlin\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-kotlin\/"},"author":{"name":"Tien Tran","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/1595d671c49cfa2a48cd3c0a047a1298"},"headline":"Top 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin s\u1ebd g\u1eb7p trong bu\u1ed5i ph\u1ecfng v\u1ea5n","datePublished":"2024-11-29T03:44:51+00:00","dateModified":"2024-11-29T04:09:30+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-kotlin\/"},"wordCount":19334,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-kotlin\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Kotlin-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-kotlin\/","url":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-kotlin\/","name":"Top 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin s\u1ebd g\u1eb7p 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-kotlin\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-kotlin\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Kotlin-vippro.jpg","datePublished":"2024-11-29T03:44:51+00:00","dateModified":"2024-11-29T04:09:30+00:00","description":"B\u00e0i vi\u1ebft t\u1ed5ng h\u1ee3p 40+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n Kotlin k\u00e8m tr\u1ea3 l\u1eddi chi ti\u1ebft, v\u1ec1 c\u00fa ph\u00e1p, th\u01b0 vi\u1ec7n, framework, v\u00e0 c\u00e1c t\u00ednh n\u0103ng \u0111\u1eb7c tr\u01b0ng c\u1ee7a Kotlin.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-kotlin\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/cau-hoi-phong-van-kotlin\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-kotlin\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Kotlin-vippro.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/11\/cau-hoi-phong-van-Kotlin-vippro.jpg","width":1500,"height":790,"caption":"ca\u0302u ho\u0309i pho\u0309ng va\u0302\u0301n Kotlin - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-kotlin\/#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 Kotlin s\u1ebd g\u1eb7p 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\/82079","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=82079"}],"version-history":[{"count":0,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/82079\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/82283"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=82079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=82079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=82079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}