1 / 11

Algorithm Programming 1 89-210

Algorithm Programming 1 89-210. Bar-Ilan University 2007-2008 תשס"ח by Moshe Fresko. Java for C++ programmers. C++ vs. Java. Java with Interpreter is Slower then compiled C++ Both kind of Comments like C++ // one line comment /* block comment */

Download Presentation

Algorithm Programming 1 89-210

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. Algorithm Programming 189-210 Bar-Ilan University 2007-2008 תשס"ח by Moshe Fresko

  2. Java for C++ programmers

  3. C++ vs. Java • Java with Interpreter is Slower then compiled C++ • Both kind of Comments like C++ • // one line comment • /* block comment */ • No global methods or data, Everything is in class. • Instead one can use Static Methods / Static Member • No structs, enumerators, or unions. Only classes. • All definitions are in class. • No method declaration. • No class pre-declaration, only definition. • No scope resolution operator :: only .

  4. C++ vs. Java • Similar to #include in C++, there is import in Java • Not the same meaning. import is like namespace. • Standard Primitive Data Types • boolean, char, byte, short, int, long, float, double. • char is 16-bit Unicode • Type checking is much tighter in Java. • Static quoted strings are automatically converted into String object. • Arrays look similar, but have different structure and behavior from C++. Arrays are treated as objects.

  5. C++ vs. Java • Objects are created with new. • Triangle trg = new Triangle(3,4,5) ; • Only primitive types are created on Stack. • No forward declarations are needed. • Java has no preprocessor. • Packages in place of Namespaces. • Default Initialization. Object Handles initialized to null, primitive class initialized 0 or equivalent. • No pointers in the sense of C++. New creates a reference (or handle). No pointer arithmetic.

  6. C++ vs. Java • Constructors are similar to C++. • No destructors in Java. • Only finalize() method, called during Garbage Collection. • Method overloading like in C++. • Does not support default arguments. • No ‘goto’, There is ‘break label’ and ‘continue label’ • Singly rooted hierarchy, everything derives from “Object”. • No parameterized types (no templates)

  7. C++ vs. Java • With GC memory leaks are much harder • Java has built-in Multi-Threading support • Mutual exclusion at object level. • Access Specifiers for each member (public, private, protected) • Everything in a Package is Friendly • Nested classes. Inner class knows the outer class (keeps a handle). • No inline keyword. • Compiler optimization can do it.

  8. C++ vs. Java • Inheritance with “extends” keyword. • Only to one-level up parent calls with “super” keyword. • Inheritance doesn’t change protection level. • Only ‘public’ inheritance • “interface” keyword for abstract base class. “implements” keyword for implementing it. • No “virtual” keyword, since all non-static methods are dynamically binded. “final” keyword for efficiency. • No Multiple Class Inheritance. But, many interfaces can be implemented. • Run-time type identificationX.getClass().getName();

  9. C++ vs. Java • All Downcast with run-time check. So there is no bad casts like in C++. • derived d = (derived) base ; • Exceptions are derived from Throwable. “finally” keyword for cleanup after exception. • Exception Specifications are superior to C++. • No operator overloading. • No const. For compile time constant value use “static final”. • No by value only by reference. • Java static variables are started in the first binding of the class.

  10. C++ vs. Java • For security issues Application and Applets differ. • Native method calls. (Only applications, not applets) • Built-in support for comment documentation. • Standard libraries for certain tasks. • Networking • Database connection • Multithreading • Distributed Objects (RMI and CORBA) • Compression • Commerce • Java 1.1 includes Java Beans standard for component development

  11. Garbage Collection • No on stack memory management, only from Heap. • Different GC Schemes • Reference Counting • Problem: Self-referential Groups • Chain Search • Stop-and-Copy • Two Heaps needed (or chunking) • Copying (If there are little or no garbage) • Lot’s of memory transfer • Mark-and-Sweep • Adaptive Generational Stop-and-Copy Mark-and-Sweep

More Related