1 / 14

Java & C++ Comparisons

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”??. //C++ #include <iostream> using namespace std; int main() {

wilton
Download Presentation

Java & C++ Comparisons

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. //C++ #include <iostream> using namespace std; int main() { cout << “Hello world” << endl; return 0; } Hello World //Java public class Hello {public static void main() {System.out.println(“Hello world”); } }

  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 • //Java • int oddCount = 0; • for (int j=0; j<20; j++) • { • if (A[j] % 2 == 1) • { • oddCount++; • } • } //C++ int oddCount = 0; for (int j=0; j<20; j++) { if (A[j] % 2 == 1) { oddCount++; } }

  6. Variable declarations //Java // Primitives int x; double d; boolean done; // Objects Dice cube = new Dice(6); //C++ // Primitives int x; double d; bool done; // Objects Dice cube(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 Java arrays Bounds checking C++ arrays No 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 Java Value (primitives) Reference (objects) C++ Value Reference const Reference 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; } while (p != null) { if (key == p.data()) return p; p = p.next(); } Searching a linked list 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 • InC++ • 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

More Related