Skip to content

C++ Versus Java

August 7, 2009

ava bears a strong relationship to the widely used C++ programming language. But there are a number of significant differences

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, 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 ).
Some more differences:
  • C++ supports operator overloading; Java does not
  • C++ supports #define; Java does not
  • C++ supports typedef; Java does not
  • C++ supports enum; Java does not
  • C++ supports unions; Java does not
  • C++ supports structs; Java does not
  • C++ supports constants; Java supports final variables
No comments yet

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: