Date post: | 06-Jan-2018 |
Category: |
Documents |
Upload: | myrtle-knight |
View: | 218 times |
Download: | 2 times |
From C++ to Java
SNU iDB Lab.Hyoung-woo Park
March 26, 2007
2
Contents History Primary Goals of Java Comparing Java to C++
Features Omitted from C++ Features Added in Java
Improving the Performance Summary Reference
3
History 1991
Java started as a project called Oak by James Gosling of Sun Microsystems
1993 The name is changed to Java
1994 Major web browsers like Netscape Navigator
incorporated the ability to run secure Java applets within web pages
1995 Java 1.0 was released
… 2006
J2SE (Java 2 Standard Edition) 6 was released
4
Contents History Primary Goals of Java Comparing Java to C++
Features Omitted from C++ Features Added in Java
Improving the Performance Summary Reference
5
Primary Goals of Java (1/10) Java is simple (1/2)
int *a;a = (int *)malloc(sizeof(int) * 50);
C++
#include <iostream>#include <algorithm>
Manual management of memory allocation Major source of bugs
Same file can be included multiple timesMajor source of code bloat
int [ ]a;a = new int[50];
import java.io.*;import java.util.*;
Java
6
Primary Goals of Java (2/10) Java is simple (2/2)
There is neither pointers nor macros in Java
Pointer Macro
MethodMemberVariable
7
Primary Goals of Java (3/10) Java is familiar
Syntax and general principles of Java are similar to C++ But there are so many differences, of course
Syntaxof
C++
Syntaxof
Java
Similar
8
Primary Goals of Java (4/10) Java is object-oriented
As with C++, Java has object-orientation facilities Encapsulation Inheritance Polymorphism
9
Primary Goals of Java (5/10) Java is independent to architecture (1/2)
Win32 UNIX MacOS
Compilerfor Win32
C++ Java
Compilerfor UNIX
Compilerfor MacOS
Source codefor Win32
Source codefor Win32
Source codefor Win32
10
Primary Goals of Java (6/10) Java is independent to architecture (2/2)
Java uses virtual machine which reads platform-independent code and execute corresponding commands The same code can be run on any machine if
appropriate JVM is installed on it Java can be run on
LINUX Mac OS Windows NT Windows XP AIX…
11
Primary Goals of Java (7/10) Java supports threads (1/2)
Modern, networked applications are often required to perform multiple tasks simultaneously
Although concurrent execution can be handled at OS level, it is more convenient for programmers to handle it using language constructs
P: thread: processP
P P P
job1 job2 job3
job1 job2 job3
Concurrencycontrolled by OS
Concurrencycontrolled by language
12
Primary Goals of Java (8/10) Java supports threads (2/2)
Java has built-in support for threads via Thread class and Runnable interface
Thread
Runnacle
13
Primary Goals of Java (9/10) Java is secure (1/2)
Security is getting more important as people are getting more and more online
Program
SecurityResources
14
Primary Goals of Java (10/10) Java is secure (2/2)
Java has built-in security features Security manager Digital Signature Cryptographic interfaces
15
Contents History Primary Goals of Java Comparing Java to C++
Features Omitted from C++ Features Added in Java
Improving the Performance Summary Reference
16
Features Omitted from C++ (1/9) Destructors (1/2)
Object destruction is often associated with the explicit freeing of dynamic memory
Java performs garbage collection instead of explicitly deallocating memory
Java uses finalize( ) function to release / close / clean up resources before garbage collection
17
Features Omitted from C++ (2/9) Destructors (2/2)
class A{public: void f( ){ Foo *x = new Foo( ); delete x; }};
C++ Java
public class A{ public void f( ){ Foo x = new Foo( ); }}
18
Features Omitted from C++ (3/9) Operator overloading (1/2)
C++ allows the primitive operators to be overloaded to work with classes This can lead to elegant code if carefully used But operator overloading is one of the most
confusing and consistently misused facilities of C++
Matrix Matrix+ : Matrix addition(Good and intuitive)
LinkedList
>> : ???(Unnatural and misused) HashTable
19
Features Omitted from C++ (4/9) Operator overloading (2/2)
Java uses methods instead of overloaded operators
map<string, string>
foo;
foo["DB"] = "2006";
Map foo;
foo.put("DB",
"2006");C++ Java
20
Features Omitted from C++ (5/9) Multiple inheritance
The concept of multiple inheritance is sound but the implementation proves difficult and adds complexity to the language
Studentid
Staffid
Workstudyid
Ambiguous!!
In this example we can resolve conflict by substituting id with Staff::id.But in reality, the class hierarchy may much more complicated than this example.
21
Features Omitted from C++ (6/9) Pointers and references (1/2)
Java does not support pointer arithmetic because it is unnecessary to deal with memory allocation and
deallocation with the existence of garbage collector type safety and security can no longer be
guaranteed if arbitrary manipulation of pointers is allowed1 2 3
pointer1 pointer3pointer2
Pointer2 and pointer3 refer to illegal addresses
22
Features Omitted from C++ (7/9) Pointers and references (2/2)
int *a;
a = (int *)malloc(sizeof(int) *
50);
*(a + 100) = 1; // pointer
error
free(a); C++ Java
int[ ] a;
a = new int[50];
a[100] = 1; // exception
23
Features Omitted from C++ (8/9) Friend classes (1/2)
class Complex {public: friend class Pal;private: double r, i;};
C++
package kr.ac.snu.idb.oasis;public class SLGuide { void func2(GOGuide g) { int i = g.a; g.func1( ); } Java
class Pal { void negate(Complex &c) { c.r = –c.r; c.i = –c.i; }};
package kr.ac.snu.idb.oasis;public class GOGuide { int a; void func1( ) { … }};
24
Features Omitted from C++ (9/9) Friend classes (2/2)
Allows one class to have access to protected or private data members and methods of another class in C++
Although Java doesn't support friend classes, Java supports packages Package members are accessible to classes in the
same package
25
Contents History Primary Goals of Java Comparing Java to C++
Features Omitted from C++ Features Added in Java
Improving the Performance Summary Reference
26
Features Added in Java (1/16) Java virtual machine (1/3)
An abstract computer architecture Software on top of a real hardware Can run the same application on different
machines JVM has two primary jobs
Execute code Manage memory
27
Features Added in Java (2/16) Java virtual machine (2/3)
Java system overview
28
Features Added in Java (3/16) Java virtual machine (3/3)
29
Features Added in Java (4/16) Garbage collection (1/2)
Unlike C++, in Java objects cannot be reclaimed or freed by explicit language directives
Objects become garbage when there are no more references to the object
30
Features Added in Java (5/16) Garbage collection (2/2)
root
Deallocatedby GC
31
Features Added in Java (6/16) Packages (1/3)
In Java, a package is a container for related classes and interfaces
A package can have packages, i.e. package structure is similar to directory structure we are familiar with
Java does allow a default friendliness among all classes inside a package
32
Features Added in Java (7/16) Packages (2/3)
java
lang util io net applet
33
Features Added in Java (8/16) Packages (3/3)
package kr.ac.snu.idb.oasis;public class GOGuide { int a; void func1( ) { …}
package kr.ac.snu.idb.oasis;public class SLGuide { void func2(GOGuide g) { int i = g.a; g.func1( ); }
34
Features Added in Java (9/16) Interfaces (1/3)
Interfaces are a named collection of method definitions without implementations and constants
Interfaces are used to resolve ambiguity problem without losing the usefulness of multiple inheritance
Interfaces are so different from multiple inheritance No interfaces or classes inherit variables No interfaces or classes inherit method implementations The interface hierarchy is independent of the class
hierarchy
35
Features Added in Java (10/16) Interfaces (2/3)
public interface Sleeper { public static final ONE_SECOND = 1000; public void wakeup( );}public class SleeperImpl implements Sleeper { public void wakeup( ){ System.out.println("This is an example."); }}An example interface and a class implementing it
36
Features Added in Java (11/16) Interfaces (3/3)
A
C
B A
C
B
: inheritance: implementation
: interface: class
C++ Java
37
Features Added in Java (12/16) Multi-threading (1/3)
Modern, networked applications are often required to perform multiple tasks simultaneously
38
Features Added in Java (13/16) Multi-threading (2/3)
The Java platform is designed from the ground up to support concurrent programming
Multi-threading is the ability of a single process to spawn multiple, simultaneous execution paths
Threads exist within a process and provide an execution environment
Threads share the process's resources Threads require fewer resources than processes
39
Features Added in Java (14/16) Multi-threading (3/3)
P
: thread: processP
40
Features Added in Java (15/16) Anonymous classes (1/2)
In Java, you can declare a class within the body of a method without naming it
Anonymous classes enhance developer efficiency in many cases, but they can reduce code readability
41
Features Added in Java (16/16) Anonymous classes (2/2)
public class Spot extends Applet { public void init( ) { addMouseListener(new MyMouseAdapter( )); } class MyMouseAdapter extends MouseAdapter { // implementation of custom adapter }}
Using named class
Using anonymous class
public class Spot extends Applet { public void init( ) { addMouseListener(new MouseAdapter( ) { // implementation of custom adapter } ); }}
42
Contents History Primary Goals of Java Comparing Java to C++
Features Omitted from C++ Features Added in Java
Improving the Performance Summary Reference
43
Improving the Performance (1/5) Just-in-time compilation (1/2)
JIT compilation (dynamic translation) is a technique for improving the runtime performance of program
JIT compiler caches the result of translation (execution code) to improve performance
Programmers had to decide whether or not to use JIT compilers in the past, but now it's automatic in most JVM
44
Improving the Performance (2/5) Just-in-time compilation (2/2)
Java source code
Java bytecode
output
compiler
interpreter
JIT compiler
native code
native exec
45
Improving the Performance (3/5) Java native interface (1/3)
JNI is a programming framework that allows Java code to call and be called by native applications Native applications are programs which are
dependent to platforms and possibly written in other languages
JNI is used to implement platform-specific features or to write performance-critical modules
46
Improving the Performance (4/5) Java native interface (2/3)
47
Improving the Performance (5/5) Java native interface (3/3)
An example program using JNI
public class HelloJNI { private native void print( ); public static void main(String[ ] args) { new HelloJNI( ).print( ); } static { System.loadLibrary("HelloJNI"); }}#include <jni.h>#include "HelloJNI.h"JNIEXPORT void JNICALL Java_HelloJNI_print(JNIEnv *env, jobject obj) { printf("Hello JNI\n" ); return;}
Compilerfor Win32
HelloJNI.class
HelloJNI.dll
48
Contents History Primary Goals of Java Comparing Java to C++
Features Omitted from C++ Features Added in Java
Improving the Performance Summary Reference
49
Summary Although C++ and Java has commonalities, some
features of C++ are omitted in Java, and some features are added in Java
Java is fit to current web environment because Java is platform-independent code reuse & sharing Java is object-oriented modular programming Java is multi-threaded distributed computing Java is secure secure web programming
Through JNI programmers can merge native codes into Java program
JIT compiler improves the performance of Java
50
Contents History Primary Goals of Java Comparing Java to C++
Features Omitted from C++ Features Added in Java
Improving the Performance Summary Reference
51
Reference Setrag, K., Razmik A. (1995). Object
Orientation. John Wiley & Sons, Inc., 267-319. Richard C. (1998). Java Tutorial 2nd Ed.
Addison-Wesley, 105-158. Michael D. (1996). Java for C/C++
Programmers, Wiley Computer Publishing, 245-287.
Barry B. (1996). Java Essentials for C and C++ Programmers, Wesley Developers Press, 3-11.