Contents - Cultural View
Contents - Cultural View Contents - Cultural View
Comparison of Java and C++ 90 Templates are Turing-complete (see template metaprogramming). Generics are not Turing-complete. Miscellaneous • Java and C++ use different techniques for splitting up code in multiple source files. Java uses a package system that dictates the file name and path for all program definitions. In Java, the compiler imports the executable class files. C++ uses a header file source code inclusion system for sharing declarations between source files. (See Comparison of imports and includes.) • 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++ compilation features an additional textual preprocessing phase, while Java does not. Thus some users add a preprocessing phase to their build process for better support of conditional compilation. • In both languages, arrays have a fixed size. In Java, arrays are first-class objects, while in C++ they are merely a continuous run of their base objects, often referred to using a pointer to their first element and an optional length. In Java, arrays are bounds-checked and know their length, while in C++ you can treat any subsequence as an array in its own right. Both C++ and Java provide container classes (std::vector and java.util.ArrayList respectively) which are resizable and store their size. • Java's division and modulus operators are well defined to truncate to zero. C++ does not specify whether or not these operators truncate to zero or "truncate to -infinity". -3/2 will always be -1 in Java, but a C++ compiler may return either -1 or -2, depending on the platform. C99 defines division in the same fashion as Java. Both languages guarantee (where a and b are integer types) that (a/b)*b + (a%b) == a for all a and b (b != 0). The C++ version will sometimes be faster, as it is allowed to pick whichever truncation mode is native to the processor. • The sizes of integer types is defined in Java (int is 32-bit, long is 64-bit), while in C++ the size of integers and pointers is compiler and ABI dependent within given constraints. Thus, carefully-written C++ code can take advantage of the 64-bit processor's capabilities while still functioning properly on 32-bit processors. However, care must be taken to write the C++ program in a portable manner. In contrast, Java's fixed integer sizes mean that programmer error in this regard shouldn't be possible. This may incur a performance penalty since Java code cannot run using an arbitrary processor's word size. Performance In addition to running a compiled Java program, computers running Java applications must also run the Java Virtual Machine JVM, while compiled C++ programs can be run without external applications. Early versions of Java were significantly outperformed by statically compiled languages such as C++. This is because the program statements of these two closely related languages may compile to a few machine instructions with C++, while compiling into several byte codes involving several machine instructions each when interpreted by a JVM. For example: Java/C++ statement C++ generated code (x86) Java generated byte vector[i]++; mov edx,[ebp+4h] mov eax,[ebp+1Ch] inc dword ptr [edx+eax*4] aload_1 iload_2 dup2 iaload iconst_1 While this may still be the case for embedded systems because of the requirement for a small footprint, it is frequently argued that advances in just in time (JIT) compiler technology for long-running server and desktop Java processes will close the performance gap. iadd iastore code
Comparison of Java and C++ 91 Several studies of mostly numerical benchmarks argue that Java could potentially be faster than C++ in some circumstances, [7] [8] [9] . However, it was shown that numerical (micro-)benchmarks are not appropriate for evaluation of languages as compiler is able to optimize both cases equally, or eliminate benchmarked code entirely. [10] [11] [12] If referring to a real world program, Java would suffer because of number of reasons: [13] [14] [15] • All objects are allocated on the heap. For functions using small objects this can result in huge performance degradation as stack allocation costs essentially zero. • Methods are by-default virtual. This increases memory usage as much as several times for small objects because of virtual function tables. Also, it induces performance penalty, because JIT compiler has to do additional optimization passes even for de-virtualization of small functions. • A lot of casting required even using standard containers induces performance penalty as it is needed to walk through entire inheritance hierarchy. • Virtual Java Machine Increases memory usage even further, thus reducing memory locality and increasing chances of cache misses and therefore slowdown of the program. • Lack of access to low level details does not allow developer to better optimize the program where the compiler is unable to do so. [16] It is argued, however, that compared to Java, C++ also has a number of downsides: • Pointers make optimization difficult since they may point to arbitrary data. However this is obsoleted as new compilers introduced strict-aliasing rule [17] and because of support of the C99 keyword restrict. [18] • Java garbage collection may have better cache coherence than usual usage of malloc/new for memory allocation as its allocations are generally made sequentially. Nevertheless, arguments exist that both allocators equally fragment the heap and no one exhibits better cache locality. • Run-time compilation can potentially use additional information available at run-time to optimise code more effectively, such as knowing what processor the code will be executed on. However this claim is effectively made obsolete as most state-of-the-art C++ compilers generate multiple code paths to employ full computational abilities of the given system [19] . Proprietary Control C++ is not a trademark of any company or organization and is not owned by any individual. [20] Java is a trademark of Sun Microsystems, which is now owned by Oracle. [21] The C++ language is defined by ISO/IEC 14882, an ISO standard, which is published by the ISO/IEC JTC1/SC22/WG21 committee. The Java language is defined by the Java Language Specification[36], a book which is published by Sun (now Oracle). External references • Java and C++ Memory Management [22] — an exhaustive publication about object-oriented memory management that compares Java and C++ in terms of Memory Modeling. • How Java Differs from C [23] — excerpt from Java in a Nutshell by David Flanagan • Java vs. C++ resource management comparison [24] - Comprehensive paper with examples • Java vs C performance... again... [25] - In-depth discussion of differences between Java and C / C++ with regard to performance
- Page 43 and 44: Java applet 39 [18] W3.org (http:/
- Page 45 and 46: Associació d'Usuaris de Java de Ca
- Page 47 and 48: Associació d'Usuaris de Java de Ca
- Page 49 and 50: Associació d'Usuaris de Java de Ca
- Page 51 and 52: Associació d'Usuaris de Java de Ca
- Page 53 and 54: Bean Scripting Framework 49 Bean Sc
- Page 55 and 56: CJAN 51 Ostensibly, CJAN.org failed
- Page 57 and 58: Chainsaw (log file viewer) 53 Chain
- Page 59 and 60: clone (Java method) 55 Alternatives
- Page 61 and 62: clone (Java method) 57 } } abstract
- Page 63 and 64: Comparison of Java and C Sharp 59 A
- Page 65 and 66: Comparison of Java and C Sharp 61 R
- Page 67 and 68: Comparison of Java and C Sharp 63 T
- Page 69 and 70: Comparison of Java and C Sharp 65 f
- Page 71 and 72: e="6.73"> = 42; = null; Comparison
- Page 73 and 74: Comparison of Java and C Sharp 69 L
- Page 75 and 76: Comparison of Java and C Sharp 71 I
- Page 77 and 78: Comparison of Java and C Sharp 73 t
- Page 79 and 80: Comparison of Java and C Sharp 75 N
- Page 81 and 82: Comparison of Java and C Sharp 77 L
- Page 83 and 84: Comparison of Java and C Sharp 79 T
- Page 85 and 86: Comparison of Java and C Sharp 81 C
- Page 87 and 88: Comparison of Java and C++ 83 C++ J
- Page 89 and 90: Comparison of Java and C++ 85 e="7.
- Page 91 and 92: Comparison of Java and C++ 87 the c
- Page 93: Comparison of Java and C++ 89 • D
- Page 97 and 98: Comparison of the Java and .NET pla
- Page 99 and 100: Comparison of the Java and .NET pla
- Page 101 and 102: Comparison of the Java and .NET pla
- Page 103 and 104: Comparison of the Java and .NET pla
- Page 105 and 106: Constant interface 101 } public sta
- Page 107 and 108: Java 4K Game Programming Contest 10
- Page 109 and 110: Java 4K Game Programming Contest 10
- Page 111 and 112: CookXml 107 Creators, setters, adde
- Page 113 and 114: Cougaar 109 Cougaar Cougaar is a Ja
- Page 115 and 116: Devoxx 111 Related links • Devoxx
- Page 117 and 118: Elastic Path 113 References [1] htt
- Page 119 and 120: Electronic Disturbance Theater 115
- Page 121 and 122: Event dispatching thread 117 • Sw
- Page 123 and 124: Facelets 119 The above code can be
- Page 125 and 126: final (Java) 121 } Sphere(double x,
- Page 127 and 128: FormEngine 123 FormEngine Developer
- Page 129 and 130: Generics in Java 125 Motivation for
- Page 131 and 132: Generics in Java 127 Pair grade440
- Page 133 and 134: G-java 129 G-java G-java 2.x was a
- Page 135 and 136: GlassFish Metro 131 Features Metro
- Page 137 and 138: James Gosling 133 Contributions He
- Page 139 and 140: Todd Greanier 135 Todd Greanier Tod
- Page 141 and 142: Head First (book series) 137 Head F
- Page 143 and 144: Inner class 139 • Local class - T
Comparison of Java and C++ 90<br />
Templates are Turing-complete (see template metaprogramming). Generics are not Turing-complete.<br />
Miscellaneous<br />
• Java and C++ use different techniques for splitting up code in multiple source files. Java uses a package system<br />
that dictates the file name and path for all program definitions. In Java, the compiler imports the executable class<br />
files. C++ uses a header file source code inclusion system for sharing declarations between source files. (See<br />
Comparison of imports and includes.)<br />
• Compiled Java code files are generally smaller than code files in C++ as Java bytecode is usually more compact<br />
than native machine code and Java programs are never statically linked.<br />
• C++ compilation features an additional textual preprocessing phase, while Java does not. Thus some users add a<br />
preprocessing phase to their build process for better support of conditional compilation.<br />
• In both languages, arrays have a fixed size. In Java, arrays are first-class objects, while in C++ they are merely a<br />
continuous run of their base objects, often referred to using a pointer to their first element and an optional length.<br />
In Java, arrays are bounds-checked and know their length, while in C++ you can treat any subsequence as an<br />
array in its own right. Both C++ and Java provide container classes (std::vector and java.util.ArrayList<br />
respectively) which are resizable and store their size.<br />
• Java's division and modulus operators are well defined to truncate to zero. C++ does not specify whether or not<br />
these operators truncate to zero or "truncate to -infinity". -3/2 will always be -1 in Java, but a C++ compiler may<br />
return either -1 or -2, depending on the platform. C99 defines division in the same fashion as Java. Both<br />
languages guarantee (where a and b are integer types) that (a/b)*b + (a%b) == a for all a and b (b != 0). The C++<br />
version will sometimes be faster, as it is allowed to pick whichever truncation mode is native to the processor.<br />
• The sizes of integer types is defined in Java (int is 32-bit, long is 64-bit), while in C++ the size of integers and<br />
pointers is compiler and ABI dependent within given constraints. Thus, carefully-written C++ code can take<br />
advantage of the 64-bit processor's capabilities while still functioning properly on 32-bit processors. However,<br />
care must be taken to write the C++ program in a portable manner. In contrast, Java's fixed integer sizes mean that<br />
programmer error in this regard shouldn't be possible. This may incur a performance penalty since Java code<br />
cannot run using an arbitrary processor's word size.<br />
Performance<br />
In addition to running a compiled Java program, computers running Java applications must also run the Java Virtual<br />
Machine JVM, while compiled C++ programs can be run without external applications. Early versions of Java were<br />
significantly outperformed by statically compiled languages such as C++. This is because the program statements of<br />
these two closely related languages may compile to a few machine instructions with C++, while compiling into<br />
several byte codes involving several machine instructions each when interpreted by a JVM. For example:<br />
Java/C++ statement C++ generated code (x86) Java generated byte<br />
vector[i]++; mov edx,[ebp+4h]<br />
mov eax,[ebp+1Ch]<br />
inc dword ptr [edx+eax*4]<br />
aload_1<br />
iload_2<br />
dup2<br />
iaload<br />
iconst_1<br />
While this may still be the case for embedded systems because of the requirement for a small footprint, it is<br />
frequently argued that advances in just in time (JIT) compiler technology for long-running server and desktop Java<br />
processes will close the performance gap.<br />
iadd<br />
iastore<br />
code