Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??

Similar presentations


Presentation on theme: "Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??"— Presentation transcript:

1 Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing?? How are parameters passed?? What makes Java “safer”??

2 Hello World //Java public class Hello {public static void main() {System.out.println(“Hello world”); } } //C++ #include using namespace std; int main() { cout << “Hello world” << endl; return 0; }

3 Object oriented design is very important in Java. All code should be either an application (client) class or a support class. Many classes come with Java. Some do not need to be imported. Example: System (always available, used for console output and other utilities)

4 Some aspects of Java are very similar to C++ semantics (reserved words) syntax decision statements control statements What do the following loops do?

5 Control Structures //C++ int oddCount = 0; for (int j=0; j<20; j++) { if (A[j] % 2 == 1) { oddCount++; } //Java int oddCount = 0; for (int j=0; j<20; j++) { if (A[j] % 2 == 1) { oddCount++; }

6 Variable declarations //C++ // Primitives int x; double d; bool done; // Objects Dice cube(6); //Java // Primitives int x; double d; boolean done; // Objects Dice cube = new Dice(6);

7 BankAccount bc1; C++Java Actually creates an instance of the class BankAccount: allocates enough memory to hold the object’s data Creates a reference to an instance of the class BankAccount. bc1 is only a variable that will hold the address of an instance of this class when memory is allocated for it. In Java this statement actually creates the object: bc1 = new BankAccount();

8 Assignment of objects: What does bc2 = bc1 do? A copy of the object, bc1, including all its data, is made and named bc2 in C++. bc2, a reference to an object of the BankAcct class, receives the address of the object, bc1 in Java. That is, both bc1 and bc2 refer to the same object !!

9 How can a copy of an object be made in Java? A “deep copy” must be made. After a new reference to an instance of the same class is declared, the programmer must copy each data field.

10 Linear Aggregate Structures C++ arrays No bounds checking Java arrays Bounds checking As in C++ the size of an array cannot be changed, once established. (Other classes exist for that.) In Java an array is an object, inheriting from the class Object, and is always available. Ex: int[] myInts; ( declares myInts a reference to an array) myInts = new int[50]; (allocates memory needed)

11 Parameter Passing C++ Value Reference const Reference Java Value (primitives) Reference (objects) In Java you can not pass a pointer by reference. Therefore, addresses can not be inadvertently changed. Of course there are no pointers - but references do the same job. There are no symbols: * or &.

12 while (p != NULL) { if (key == p->data) return p; p = p->next; } Searching a linked list while (p != null) { if (key == p.data()) return p; p = p.next(); } C++Java

13 Out with the old In C++ –Inheritance – It’s there, but more difficult to implement –Multiple implementations of Abstract Data Types – through classes only Java supports Inheritance (extends) – It’s there and it’s easy Multiple implementations through implements keyword

14 In with the New In C++ –Const –many uses –Operator overloading –delete – to clean up memory management In Java final – a Pascal/Ada-like use of const NO operator overloading Automatic garbage collection


Download ppt "Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??"

Similar presentations


Ads by Google