C++ vs. Java

For C++ Programmers

Java bears a strong (intentional) relationship to the widely used C++ programming language. There are, however, a number of significant differences, the most significant of which include:

Data Types: In C/C++ many data types (such as int) are hardware dependent. In Java, the size and manner of representation of data is specified, to facilitate machine independence of code.

References vs. Pointers: In C/C++, variables that contain addresses of data and functions, pointers, can be declared. In Java, no addresses are used. Instead, references are used to establish variable names for objects.

Automatic Variables: In C/C++, memory for variables declared within functions (i.e., local variables) will normally be allocated automatically, on the stack. In Java, all memory for non-intrinsic data objects must be acquired with the new operator.

Dynamic Memory Management: In C++, memory is dynamically assigned by the programmer (using the new operator) and must also explicitly be freed (using the delete operator). In Java, a garbage collection process automatically locates the memory associated with any object that is no longer referenced within the program, meaning no explicit deletion is required.

Global Data and Functions: In C++, data and functions can be declared outside of any class definition. (In C, all functions are declared that way). In Java, all data and functions must be declared within class definitions. Like C++, however, a static declaration may be used to make member data/functions object dependent.

Inheritance: Unlike C++, Java permits only one parent per class. Many benefits of multiple inheritance be achieved, however, using JavaInterfaces. Java class constructor functions can also explicitly invoke parent constructors with the super keyword.

Boolean Expressions: In C/C++, boolean (false/true) expressions translate to 0, nonzero integers. In Java, test are done with the boolean data type. As a result, expressions containing && and || stop evaluating an expression once its results are known to be true or false, referred to as a shortcircuit. The & and | operators, in contrast, evaluate both sides of their expressions even if the final result is already known (e.g., the left side of an & expression is false ).

The information above has been reproduced with permission from BarCharts, Inc.

Source

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s