Java vs. C++

Download Report

Transcript Java vs. C++

Algorithm Programming 1
89-210
Bar-Ilan University
2007-2008 ‫תשס"ח‬
by Moshe Fresko
Java for C++ programmers
C++ vs. Java
1.
2.
Java with Interpreter is Slower then compiled C++
Both kind of Comments like C++
•
•
3.
No global methods or data, Everything is in class.
•
4.
5.
7.
Instead one can use Static Methods / Static Member
No structs, enumerators, or unions. Only classes.
All definitions are in class.
•
6.
// one line comment
/* block comment */
No method declaration.
No class pre-declaration, only definition.
No scope resolution operator :: only .
C++ vs. Java
8.
Similar to #include in C++, there is import in Java
•
9.
Standard Primitive Data Types
•
10.
11.
12.
13.
Not the same meaning. import is like namespace.
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
14.
Objects are created with new.
•
15.
16.
17.
18.
19.
20.
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
21.
22.
Constructors are similar to C++.
No destructors in Java.
•
23.
24.
25.
26.
27.
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
28.
29.
30.
31.
32.
33.
34.
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
35.
36.
37.
Inheritance with “extends” keyword.
Only to one-level up parent calls with “super” keyword.
Inheritance doesn’t change protection level.
•
38.
39.
40.
41.
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 identification
X.getClass().getName();
C++ vs. Java
42.
All Downcast with run-time check. So there is no bad casts
like in C++.
•
•
43.
44.
45.
46.
47.
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.
48.
49.
50.
51.
•
•
•
•
•
•
52.
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
1)
–
Chain Search
Stop-and-Copy
2)
3)
–
–
–
4)
5)
Problem: Self-referential Groups
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