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++ 86<br />

C++ Java<br />

const Foo *a; // you cannot modify the object<br />

//pointed to by a through a<br />

private final Foo a;//Use getA() instead of a<br />

public Foo getA(){<br />

return a.clone()<br />

}<br />

a = new Foo(); a=new Foo();//Only in constructor<br />

a->x = 5;<br />

// ILLEGAL<br />

Foo *const b = new Foo();<br />

// you can declare a "const" pointer<br />

b = new Foo();<br />

//ILLEGAL, you can't re-bind it<br />

b->x = 5;<br />

// LEGAL, you can still modify the object<br />

getA().x=5;<br />

//Only modifies a clone of a's x, not a.x<br />

final Foo b = new Foo();<br />

// you can declare a "final" reference<br />

b = new Foo();<br />

// ILLEGAL, you can't re-bind it<br />

b.x = 5;<br />

// LEGAL, you can still modify the object<br />

• C++ supports goto statements; Java enforces structured control flow, and relies on labelled break and labelled<br />

continue statements to provide some goto-like functionality. Some commenters point out that these labelled flow<br />

control statements break the single point-of-exit property of structured programming. [3]<br />

• C++ provides low-level features which Java lacks. In C++, pointers can be used to manipulate specific memory<br />

locations, a task necessary for writing low-level operating system components. Similarly, many C++ compilers<br />

support inline assembler. In Java, such code has to reside in external libraries, and can only be accessed through<br />

the Java Native Interface with a significant overhead for each call.<br />

Semantics<br />

• C++ allows default values for arguments of a function/method, Java does not. However, method overloading can<br />

be used to obtain similar results in Java.<br />

• The minimal compilation unit in C++ is a function; the compilation unit in Java is a class. In C++, functions can<br />

be compiled separately. In Java, to compile and maintain methods separately requires moving them into super and<br />

extended classes or using some other code refactoring technique.<br />

• C++ allows a range of implicit conversions between native types, and also allows the programmer to define<br />

implicit conversions involving user-defined types. In Java, only widening conversions between native types are<br />

implicit; other conversions require explicit cast syntax.<br />

• A consequence of this is that although loop conditions (if, while and the exit condition in for) in Java and C++<br />

both expect a boolean expression, code such as if(a = 5) will cause a compile error in Java because there is no<br />

implicit narrowing conversion from int to boolean. This is handy if the code was a typo for if(a == 5). Yet<br />

current C++ compilers usually generate a warning when such an assignment is performed within a conditional<br />

expression.<br />

• For passing parameters to functions, C++ supports both pass-by-reference and pass-by-value. In Java, parameters<br />

are always passed by value. [4] However, in Java all non-primitive values are references to objects (in C++ terms,<br />

they are (smart)-pointers). Objects are not values in Java and only their references can be manipulated; C++<br />

developers who are used to having objects as values may confuse this with pass-by-reference.<br />

• Java built-in types are of a specified size and range defined by the virtual machine; In C++, a minimal range of<br />

values is defined for built-in types, but the exact representation (number of bits) can be mapped to whatever<br />

native types are supported on a given platform.<br />

• For instance, Java characters are 16-bit Unicode characters, and strings are composed of a sequence of such<br />

characters. C++ offers both narrow and wide characters, but the actual size of each is platform dependent, as is

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

Saved successfully!

Ooh no, something went wrong!