110 likes | 225 Views
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 */
E N D
Algorithm Programming 189-210 Bar-Ilan University 2007-2008 תשס"ח by Moshe Fresko
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 .
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.
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.
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)
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.
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();
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.
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
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