14.07.2013 Views

Contents - Cultural View

Contents - Cultural View

Contents - Cultural View

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Comparison of Java and C++ 89<br />

• Due to its unconstrained expressiveness, low level C++ language features (e.g. unchecked array access, raw<br />

pointers, type punning) cannot be reliably checked at compile-time or without overhead at run-time. Related<br />

programming errors can lead to low-level buffer overflows and segmentation faults. The Standard Template<br />

Library provides higher-level abstractions (like vector, list and map) to help avoid such errors. In Java, low level<br />

errors either cannot occur or are detected by the JVM and reported to the application in the form of an exception.<br />

• The Java language requires specific behavior in the case of an out-of-bounds array access, which generally<br />

requires bounds checking of array accesses. This eliminates a possible source of instability but usually at the cost<br />

of slowing down execution. In some cases, compiler analysis can prove a bounds check unnecessary and<br />

eliminate it. C++ has no required behavior for out-of-bounds access of native arrays, thus requiring no bounds<br />

checking for native arrays. C++ standard library collections like std::vector, however, offer optional bounds<br />

checking. In summary, Java arrays are "always safe; severely constrained; always have overhead" while C++<br />

native arrays "have optional overhead; are completely unconstrained; are potentially unsafe."<br />

Templates vs. Generics<br />

Both C++ and Java provide facilities for generic programming, templates and generics, respectively. Although they<br />

were created to solve similar kinds of problems, and have similar syntax, they are actually quite different.<br />

C++ Templates Java Generics<br />

Classes and functions can be templated. Classes and methods can be genericized.<br />

Parameters can be any type or integral value. Parameters can only be reference types (not primitive types).<br />

Separate copies of the class or function are likely to be generated for each type<br />

parameter when compiled.<br />

One version of the class or function is compiled, works for all<br />

type parameters.<br />

Objects of a class with different type parameters are different types at run time. Type parameters are erased when compiled; objects of a class<br />

Implementation source code of the templated class or function must be included<br />

in order to use it (declaration insufficient).<br />

Templates can be specialized -- a separate implementation could be provided for<br />

a particular template parameter.<br />

Template parameters can have default arguments (only for template classes, not<br />

functions).<br />

Does not support wildcards. Instead, return types are often available as nested<br />

typedefs.<br />

Does not directly support bounding of type parameters, but metaprogramming<br />

provides this [6]<br />

with different type parameters are the same type at run time.<br />

Signature of the class or function from a compiled class file is<br />

sufficient to use it.<br />

Generics cannot be specialized.<br />

Generic type parameters cannot have default arguments.<br />

Supports wildcard as type parameter if it is only used once.<br />

Supports bounding of type parameters with "extends" and<br />

"super" for upper and lower bounds, respectively; allows<br />

enforcement of relationships between type parameters.<br />

Allows instantiation of class of type parameter type. Does not allow instantiation of class of type parameter type<br />

(except through reflection).<br />

Type parameter of templated class can be used for static methods and variables. Type parameter of templated class cannot be used for static<br />

methods and variables.<br />

Static variables are not shared between classes of different type parameters. Static variables are shared between instances of a classes of<br />

Templated classes and functions do not enforce type relations for type parameters<br />

in their declaration. Use of a wrong type parameter results in the template code<br />

"not working", usually generating an error message at a place in the template<br />

code where an operation is not allowed for that type and not in the user's code.<br />

Proper use of templated classes and functions is dependent on proper<br />

documentation. Metaprogramming provides these features at the cost of<br />

additional effort.<br />

different type parameters.<br />

Generic classes and functions can enforce type relationships for<br />

type parameters in their declaration. Use of a wrong type<br />

parameter results in a type error at the code that uses it.<br />

Operations on parametrized types in generic code are only<br />

allowed in ways that can be guaranteed to be safe by the<br />

declaration. This results in greater type safety at the cost of<br />

flexibility.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!