Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Comparison of Java and C++
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Miscellaneous === * Java and C++ use different means to divide code into multiple source files. ** Java uses a [[Java package|package system]] that dictates the file name and path for all program definitions. Its compiler imports the executable [[class (file format)|class files]]. ** Prior to [[C++20]], C++ used a [[header file]] [[source code]] inclusion system to share declarations between source files. Since C++20, however, [[precompiled header#Modules|modules]] were introduced offering similar functionality to Java packages, however C++ modules do not have the same granularity of Java packages which allowing for importing individual classes - rather, in C++, all symbols marked <code>export</code> are accessible after importing a module, making it akin to a wildcard import in Java. ** Since [[C++23]], the C++ standard library can now be imported as a module, but must be imported in its entirety rather than importing specific packages of the library like in Java, with <syntaxhighlight lang="C++" inline>import std;</syntaxhighlight>. This may change in the future, with proposals to separate the standard library into more modules such as <code>std.core</code>, <code>std.math</code>, and <code>std.io</code>.<ref name="P0581R1">C++ Standards Committee. (2018). ''P0581R1 - Modules for C++''. Retrieved from [https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0581r1.pdf https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0581r1.pdf]</ref><ref name="P2412R0">C++ Standards Committee. (2021). ''P2412R0 - Further refinements to the C++ Modules Design''. Retrieved from [https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2412r0.pdf https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2412r0.pdf]</ref> * The term "[[modular programming|module]]" refers to different things. In Java, a [[Java package#Modules|module]] is used to group several packages together, meanwhile in C++ a [[precompiled header#Modules|module]] represents a single [[translation unit]]. ** <code>import</code> in C++ imports a module by linking it at compilation, however in C++, modules do not dictate the namespace which a symbol belongs to. Meanwhile, <code>import</code> in Java does not actually "import" any code into a file, and is used to alias classes to avoid fully qualifying them. This is because all classes are handled as needed during runtime by the [[Java class loader]] on demand, and can be invoked even without "importing", simply by fully qualifying the class. * A Java source file must match the namespace which of the public class it declares (it may be named anything if there are no public classes), and the package it belongs to must match the path it is located in. A package may only declare at most one public class (but may have multiple non-public classes). * A C++ source file (whether a header or module) may have any arbitrary name, and may contain as many classes as the programmer desires. Modules have no requirement to match the path of its location. * Compiled Java code files are generally smaller than code files in C++ as [[Java bytecode]] is usually more compact than native [[machine code]] and Java programs are never statically linked. * C++ compiling features an added textual [[preprocessor|preprocessing]] phase, while Java does not. Thus some users add a preprocessing phase to their build process for better support of conditional compiling. * Java's division and modulus operators are well defined to truncate to zero. C++ (pre-[[C++11]]) does not specify whether or not these operators truncate to zero or "truncate to -infinity". -3/2 will always be -1 in Java and C++11, but a [[C++03]] compiler may return either -1 or -2, depending on the platform. [[C99]] defines division in the same fashion as Java and C++11. Both languages guarantee (where a and b are integer types) that <code>(a/b)*b + (a%b) == a</code> for all a and b (b != 0). The [[C++03]] version will sometimes be faster, as it is allowed to pick whichever truncation mode is native to the processor. * The sizes of integer types are defined in Java (int is 32-bit, long is 64-bit), while in C++ the size of integers and pointers is compiler and [[application binary interface]] (ABI) dependent within given constraints. Thus a Java program will have consistent behavior across platforms, whereas a C++ program may require adapting for some platforms, but may run faster with more natural integer sizes for the local platform. An example comparing [[Wikibooks:C++ Programming/Code/IO#.E2.80.8ERounding number example|C++]] and [[Wikibooks:Java Programming/Mathematical functions#Rounding number example|Java]] exists in [[Wikibooks]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)