Java được thiết kế đơn giản, an toàn, managed hơn C++. Nhiều tính năng C++ mạnh nhưng phức tạp/rủi ro bị loại bỏ hoặc thay thế.
Danh sách chi tiết
| Tính năng C++ | Java Equivalent / Thay thế | Lý do loại bỏ |
|---|---|---|
| Unsigned integer types (unsigned int, uint32_t...) | Không có. Dùng long (64-bit) hoặc int + bitwise ops. Java 8+: Integer.toUnsignedString(), Integer.compareUnsigned(). | Đơn giản hóa type system, tránh bug signed/unsigned mix. |
Destructor & delete | Garbage Collector (GC) tự động. try-with-resources, AutoCloseable, Cleaner (Java 9+) cho resource management. | Memory safety, tránh memory leak/dangling pointer/double free. |
| Friend class / friend function | Package-private (default access), nested class, module system (Java 9+). | Encapsulation, module boundary. Friend phá vỡ encapsulation. |
| Pointers (con trỏ trực tiếp) | Reference (an toàn, không arithmetic). JNI cho native interop. VarHandle, MemorySegment (Foreign Function & Memory API - Java 22+). | Memory safety, type safety, GC mobility. Pointer arithmetic là nguồn bug lớn. |
| typedef / type alias | Không có. Dùng using trong generics? Không. Chỉ dùng class/interface wrapper hoặc type inference var. | Type system nominal, không structural. Tránh confusion. |
| Global variables/functions | Không có. Mọi thứ phải trong class. Static fields/methods trong class đóng vai trò tương tự. | OOP thuần túy, namespace management, tránh name collision. |
| Templates | Generics (Java 5+). Type erasure (không phải template specialization). | Generics đơn giản hơn, type safety compile-time. Template = code generation (compile-time duck typing). |
Scope resolution :: | Không cần. Package/class naming. Static import cho static members. | Không có global namespace. |
| Multiple inheritance (class) | Interface multiple implementation (Java 8+ default methods). Single class inheritance. | Diamond problem, complexity. Interface default method cho code reuse. |
| Operator overloading | Không có (trừ + cho String). | Đọc code dễ hiểu, tránh abuse (operator semantics không rõ ràng). |
| Default arguments | Method overloading, Builder pattern, Optional, varargs. | Đơn giản, rõ ràng tại call site. |
| Inline functions | JIT compiler tự động inline hot methods. @ForceInline (JDK internal), -XX:CompileCommand=inline. | Compiler/runtime quyết định optimization tốt hơn programmer. |
Preprocessor & Macros (#define, #ifdef, #include) | Không có. Dùng constants, enums, annotations, build tools (Maven/Gradle profiles), conditional compilation qua build system. | Textual substitution unsafe, hard to debug, break tooling. Java dùng structured metadata. |
| sizeof operator | Không có. Object size không xác định (GC, layout tùy JVM). Instrumentation.getObjectSize() (estimate), JOL (Java Object Layout). | Memory layout managed by JVM, không exposed. |
| Union | Không có. Dùng class composition hoặc sealed interface (Java 17+). | Type safety, memory safety. |
| goto | Từ khóa reserved nhưng không dùng. Dùng labeled break/continue, exception, structured control flow. | Spaghetti code. Structured programming. |
Const correctness (const qualifier) | final (variable), immutable class design, @Immutable annotation. | final chỉ áp dụng cho reference, không deep const. Immutable pattern thay thế. |
| RAII (Resource Acquisition Is Initialization) | try-with-resources (AutoCloseable), Cleaner, Scope (Project Loom). | GC non-deterministic → destructor không deterministic. try-with-resources deterministic scope. |
Tính năng C++ hiện đại Java đang追赶 (Catch up)
- Value types / Primitive classes (Project Valhalla) – giống struct C++, no identity, flat memory layout.
- Pattern Matching (Java 16+) – switch expressions, record patterns, type patterns.
- Records (Java 16+) – immutable data carrier, như struct có method.
- Sealed Classes (Java 17+) – kiểm soát hierarchy, như sum type.
- Foreign Function & Memory API (Java 22+) – truy cập native memory, gọi C/C++ an toàn, thay thế JNI.
- Virtual Threads (Java 21+) – lightweight threads, structured concurrency.
Tóm tắt triết lý
C++: "Trust the programmer", power, control, zero-overhead abstraction.
Java: "Safety first", managed runtime, simplicity, readability, enterprise productivity. Trade-off: control → safety, performance → productivity (với JIT narrowing gap).