From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.
-
Upload
myrtle-knight -
Category
Documents
-
view
218 -
download
2
description
Transcript of From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.
![Page 1: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/1.jpg)
From C++ to Java
SNU iDB Lab.Hyoung-woo Park
March 26, 2007
![Page 2: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/2.jpg)
2
Contents History Primary Goals of Java Comparing Java to C++
Features Omitted from C++ Features Added in Java
Improving the Performance Summary Reference
![Page 3: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/3.jpg)
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
![Page 4: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/4.jpg)
4
Contents History Primary Goals of Java Comparing Java to C++
Features Omitted from C++ Features Added in Java
Improving the Performance Summary Reference
![Page 5: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/5.jpg)
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
![Page 6: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/6.jpg)
6
Primary Goals of Java (2/10) Java is simple (2/2)
There is neither pointers nor macros in Java
Pointer Macro
MethodMemberVariable
![Page 7: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/7.jpg)
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
![Page 8: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/8.jpg)
8
Primary Goals of Java (4/10) Java is object-oriented
As with C++, Java has object-orientation facilities Encapsulation Inheritance Polymorphism
![Page 9: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/9.jpg)
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
![Page 10: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/10.jpg)
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…
![Page 11: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/11.jpg)
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
![Page 12: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/12.jpg)
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
![Page 13: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/13.jpg)
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
![Page 14: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/14.jpg)
14
Primary Goals of Java (10/10) Java is secure (2/2)
Java has built-in security features Security manager Digital Signature Cryptographic interfaces
![Page 15: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/15.jpg)
15
Contents History Primary Goals of Java Comparing Java to C++
Features Omitted from C++ Features Added in Java
Improving the Performance Summary Reference
![Page 16: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/16.jpg)
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
![Page 17: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/17.jpg)
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( ); }}
![Page 18: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/18.jpg)
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
![Page 19: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/19.jpg)
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
![Page 20: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/20.jpg)
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.
![Page 21: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/21.jpg)
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
![Page 22: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/22.jpg)
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
![Page 23: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/23.jpg)
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( ) { … }};
![Page 24: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/24.jpg)
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
![Page 25: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/25.jpg)
25
Contents History Primary Goals of Java Comparing Java to C++
Features Omitted from C++ Features Added in Java
Improving the Performance Summary Reference
![Page 26: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/26.jpg)
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
![Page 27: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/27.jpg)
27
Features Added in Java (2/16) Java virtual machine (2/3)
Java system overview
![Page 28: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/28.jpg)
28
Features Added in Java (3/16) Java virtual machine (3/3)
![Page 29: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/29.jpg)
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
![Page 30: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/30.jpg)
30
Features Added in Java (5/16) Garbage collection (2/2)
root
Deallocatedby GC
![Page 31: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/31.jpg)
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
![Page 32: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/32.jpg)
32
Features Added in Java (7/16) Packages (2/3)
java
lang util io net applet
![Page 33: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/33.jpg)
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( ); }
![Page 34: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/34.jpg)
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
![Page 35: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/35.jpg)
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
![Page 36: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/36.jpg)
36
Features Added in Java (11/16) Interfaces (3/3)
A
C
B A
C
B
: inheritance: implementation
: interface: class
C++ Java
![Page 37: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/37.jpg)
37
Features Added in Java (12/16) Multi-threading (1/3)
Modern, networked applications are often required to perform multiple tasks simultaneously
![Page 38: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/38.jpg)
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
![Page 39: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/39.jpg)
39
Features Added in Java (14/16) Multi-threading (3/3)
P
: thread: processP
![Page 40: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/40.jpg)
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
![Page 41: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/41.jpg)
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 } ); }}
![Page 42: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/42.jpg)
42
Contents History Primary Goals of Java Comparing Java to C++
Features Omitted from C++ Features Added in Java
Improving the Performance Summary Reference
![Page 43: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/43.jpg)
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
![Page 44: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/44.jpg)
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
![Page 45: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/45.jpg)
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
![Page 46: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/46.jpg)
46
Improving the Performance (4/5) Java native interface (2/3)
![Page 47: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/47.jpg)
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
![Page 48: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/48.jpg)
48
Contents History Primary Goals of Java Comparing Java to C++
Features Omitted from C++ Features Added in Java
Improving the Performance Summary Reference
![Page 49: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/49.jpg)
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
![Page 50: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/50.jpg)
50
Contents History Primary Goals of Java Comparing Java to C++
Features Omitted from C++ Features Added in Java
Improving the Performance Summary Reference
![Page 51: From C++ to Java SNU iDB Lab. Hyoung-woo Park March 26, 2007.](https://reader031.fdocuments.us/reader031/viewer/2022013013/5a4d1ad67f8b9ab059973092/html5/thumbnails/51.jpg)
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.