Contents - Cultural View

Contents - Cultural View Contents - Cultural View

culturalview.com
from culturalview.com More from this publisher
14.07.2013 Views

Comparison of Java and C++ 84 Source code can be written to be platform independent (can be compiled for Windows, BSD, Linux, Mac OS X, Solaris etc. without needing modification) and written to take advantage of platform specific features. Is typically compiled into native machine code. Is compiled into byte code for the JVM. Is dependent on the Java platform but the source code is typically written not to be dependent on operating system specific features. C++ is a powerful language designed for system programming. The Java language was designed to be simple and easy to learn with a powerful cross-platform library. The Java standard library is considerably large for a standard library. However, Java does not always provide full access to the features and performance of the platform that the software runs on. The C++ standard libraries are simple and robust providing containers and associative arrays. [2] Language features Syntax • Java syntax has a context-free grammar which can be parsed by a simple LALR parser. Parsing C++ is somewhat more complicated; for example, Foo(3); is a sequence of comparisons if Foo is a variable, but it creates an object if Foo is the name of a class template. • C++ allows namespace level constants, variables, and functions. All such Java declarations must be inside a class or interface. • In C++ declarations, a class name declares an object of that class as a value (a.k.a. value semantics). There is no way to do this in Java. Objects are not values in Java. In Java declarations, a class name declares a reference to an object of that class (a.k.a. reference semantics). The equivalent way to do this in C++ is to use "*" to declare a pointer. • In C++, the operator "." takes an object as the left operand and accesses a member of the object. Since objects cannot be values in Java, and all objects are accessed through references, this cannot be done in Java. In Java, the "." operator takes a reference to an object as the left operand and access a member of that object. The equivalent operator in C++ is "->". e="7.71"> { // Declares class Foo ; // Member variable : x(0) { // Constructor for Foo, // initializes x ar(int i) { // Member function bar() eturn 3*i + x; e="7.71"> C++ Java es a to be a Foo object value, lized using the default constructor wanted to use another constructor, uld declare it as "Foo a(args);" class Foo { // Defines class Foo public int x = 0; // Member variable, // with initializer public Foo() { // Constructor for Foo } public int bar(int i) {// Member method bar() return 3*i + x; } } Foo a; // declares a to be a reference to a Foo object a = new Foo(); // initializes using the default constructor // If you wanted to use another constructor, // you would declare it as "Foo a = new Foo(args);

Comparison of Java and C++ 85 e="7.71"> ; the contents of a to a new Foo object b; ative syntax is "Foo b(a)" Foo b = a.clone(); // copies the values of all members // of this instance if, and only if, // Foo implements a public method called // clone() which returns a new copy of the object e="7.71">a.x = 5; // modifies the object a a.x = 5; // modifies the object e="7.71"> .x es c to be a pointer to a ject (initially ned; could point anywhere) e="7.71"> oo(); c to reference a new Foo object e="7.71"> c; d to reference the same object as c e="7.71"> es the object referenced by c e="7.71"> // invokes Foo::bar() for a ; // invokes Foo::bar() for *c e="7.71"> ->x

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

Source code can be written to be platform independent (can be<br />

compiled for Windows, BSD, Linux, Mac OS X, Solaris etc. without<br />

needing modification) and written to take advantage of platform<br />

specific features. Is typically compiled into native machine code.<br />

Is compiled into byte code for the JVM. Is dependent on the Java platform<br />

but the source code is typically written not to be dependent on operating<br />

system specific features.<br />

C++ is a powerful language designed for system programming. The Java language was designed to be simple and<br />

easy to learn with a powerful cross-platform library. The Java standard library is considerably large for a standard<br />

library. However, Java does not always provide full access to the features and performance of the platform that the<br />

software runs on. The C++ standard libraries are simple and robust providing containers and associative arrays. [2]<br />

Language features<br />

Syntax<br />

• Java syntax has a context-free grammar which can be parsed by a simple LALR parser. Parsing C++ is somewhat<br />

more complicated; for example, Foo(3); is a sequence of comparisons if Foo is a variable, but it creates an<br />

object if Foo is the name of a class template.<br />

• C++ allows namespace level constants, variables, and functions. All such Java declarations must be inside a class<br />

or interface.<br />

• In C++ declarations, a class name declares an object of that class as a value (a.k.a. value semantics). There is no<br />

way to do this in Java. Objects are not values in Java. In Java declarations, a class name declares a reference to an<br />

object of that class (a.k.a. reference semantics). The equivalent way to do this in C++ is to use "*" to declare a<br />

pointer.<br />

• In C++, the operator "." takes an object as the left operand and accesses a member of the object. Since objects<br />

cannot be values in Java, and all objects are accessed through references, this cannot be done in Java. In Java, the<br />

"." operator takes a reference to an object as the left operand and access a member of that object. The equivalent<br />

operator in C++ is "->".<br />

e="7.71"><br />

{ // Declares class Foo<br />

; // Member variable<br />

: x(0) { // Constructor for Foo,<br />

// initializes x<br />

ar(int i) { // Member function bar()<br />

eturn 3*i + x;<br />

e="7.71"><br />

C++ Java<br />

es a to be a Foo object value,<br />

lized using the default constructor<br />

wanted to use another constructor,<br />

uld declare it as "Foo a(args);" <br />

<br />

class Foo { // Defines class Foo<br />

public int x = 0; // Member variable,<br />

// with initializer<br />

public Foo() { // Constructor for Foo<br />

}<br />

public int bar(int i) {// Member method bar()<br />

return 3*i + x;<br />

}<br />

}<br />

<br />

Foo a;<br />

// declares a to be a reference to a Foo object<br />

a = new Foo();<br />

// initializes using the default constructor<br />

// If you wanted to use another constructor,<br />

// you would declare it as "Foo a = new Foo(args);

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

Saved successfully!

Ooh no, something went wrong!