Download Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
From C++ to Java
SNU iDB Lab.
Hyoung-woo Park
March 26, 2007
Contents
 History
 Primary Goals of Java
 Comparing Java to C++
 Features Omitted from C++
 Features Added in Java
 Improving the Performance
 Summary
 Reference
2
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
3
Contents
 History
 Primary Goals of Java
 Comparing Java to C++
 Features Omitted from C++
 Features Added in Java
 Improving the Performance
 Summary
 Reference
4
Primary Goals of Java (1/10)
 Java is simple (1/2)
int *a;
a = (int *)malloc(sizeof(int) * 50);
int [ ]a;
a = new int[50];
Manual management of memory allocation
 Major source of bugs
#include <iostream>
#include <algorithm>
import java.io.*;
import java.util.*;
Same file can be included multiple times
Major source of code bloat
C++
Java
5
Primary Goals of Java (2/10)
 Java is simple (2/2)
 There is neither pointers nor macros in Java
Pointer
Member
Variable
Macro
Method
6
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
Syntax
of
C++
Similar
Syntax
of
Java
7
Primary Goals of Java (4/10)
 Java is object-oriented
 As with C++, Java has object-orientation facilities
 Encapsulation
 Inheritance
 Polymorphism
8
Primary Goals of Java (5/10)
 Java is independent to architecture (1/2)
Source code
for Win32
Source code
for Win32
Source code
for Win32
Compiler
for Win32
Compiler
for UNIX
Compiler
for MacOS
Win32
UNIX
MacOS
C++
Java
9
Primary Goals of Java (6/10)
 Java is independent to architecture (2/2)
 Java uses virtual machine which reads platformindependent 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
…
10
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
P
job1
job2
P
job3
Concurrency
controlled by OS
job1
job2 job3
P
Concurrency
controlled by language
P
: thread
: process
11
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
12
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
Security
Resources
13
Primary Goals of Java (10/10)
 Java is secure (2/2)
 Java has built-in security features
 Security manager
 Digital Signature
 Cryptographic interfaces
14
Contents
 History
 Primary Goals of Java
 Comparing Java to C++
 Features Omitted from C++
 Features Added in Java
 Improving the Performance
 Summary
 Reference
15
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
16
Features Omitted from C++ (2/9)
 Destructors (2/2)
class A{
public:
void f( ){
Foo *x = new Foo( );
delete x;
}
};
C++
public class A{
public void f( ){
Foo x = new Foo( );
}
}
Java
17
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++
(Good and intuitive)
Matrix
+
Matrix
: Matrix addition
(Unnatural and misused)
Linked
List
>>
Hash
Table
: ???
18
Features Omitted from C++ (4/9)
 Operator overloading (2/2)
 Java uses methods instead of overloaded operators
map<string, string> foo;
Map foo;
foo["DB"] = "2006";
foo.put("DB", "2006");
C++
Java
19
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
Student
Staff
id
id
Workstudy
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.
id
20
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 allowed
1
pointer1
2
3
pointer2
pointer3
Pointer2 and pointer3 refer to illegal addresses
21
Features Omitted from C++ (7/9)
 Pointers and references (2/2)
int *a;
int[ ] a;
a = (int *)malloc(sizeof(int) * 50);
a = new int[50];
*(a + 100) = 1; // pointer error
a[100] = 1; // exception
free(a);
C++
Java
22
Features Omitted from C++ (8/9)
 Friend classes (1/2)
class Complex {
public:
friend class Pal;
private:
double r, i;
};
package kr.ac.snu.idb.oasis;
public class GOGuide {
int a;
void func1( ) { … }
};
class Pal {
void negate(Complex &c) {
c.r = –c.r;
c.i = –c.i;
}
};
package kr.ac.snu.idb.oasis;
public class SLGuide {
void func2(GOGuide g) {
int i = g.a;
g.func1( );
}
C++
Java
23
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
24
Contents
 History
 Primary Goals of Java
 Comparing Java to C++
 Features Omitted from C++
 Features Added in Java
 Improving the Performance
 Summary
 Reference
25
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
26
Features Added in Java (2/16)
 Java virtual machine (2/3)
Java system overview
27
Features Added in Java (3/16)
 Java virtual machine (3/3)
28
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
29
Features Added in Java (5/16)
 Garbage collection (2/2)
root
Deallocated
by GC
30
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
31
Features Added in Java (7/16)
 Packages (2/3)
java
lang
util
io
net
applet
32
Features Added in Java (8/16)
 Packages (3/3)
package kr.ac.snu.idb.oasis;
package kr.ac.snu.idb.oasis;
public class GOGuide {
public class SLGuide {
int a;
void func2(GOGuide g) {
void func1( ) {
int i = g.a;
…
}
g.func1( );
}
33
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
34
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
35
Features Added in Java (11/16)
 Interfaces (3/3)
A
B
A
B
C
C
C++
Java
: class
: interface
: inheritance
: implementation
36
Features Added in Java (12/16)
 Multi-threading (1/3)
 Modern, networked applications are often required to
perform multiple tasks simultaneously
37
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
38
Features Added in Java (14/16)
 Multi-threading (3/3)
P
P
: thread
: process
39
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
40
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
}
}
public class Spot extends Applet {
public void init( ) {
addMouseListener(new MouseAdapter( ) {
// implementation of custom adapter
} );
}
}
Using named class
Using anonymous class
41
Contents
 History
 Primary Goals of Java
 Comparing Java to C++
 Features Omitted from C++
 Features Added in Java
 Improving the Performance
 Summary
 Reference
42
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
43
Improving the Performance (2/5)
 Just-in-time compilation (2/2)
Java source code
compiler
Java bytecode
JIT compiler
interpreter
output
native code
native exec
44
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
45
Improving the Performance (4/5)
 Java native interface (2/3)
46
Improving the Performance (5/5)
 Java native interface (3/3)
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;
}
HelloJNI.class
Compiler
for Win32
An example program using JNI
HelloJNI.dll
47
Contents
 History
 Primary Goals of Java
 Comparing Java to C++
 Features Omitted from C++
 Features Added in Java
 Improving the Performance
 Summary
 Reference
48
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
49
Contents
 History
 Primary Goals of Java
 Comparing Java to C++
 Features Omitted from C++
 Features Added in Java
 Improving the Performance
 Summary
 Reference
50
Reference
 Setrag, K., Razmik A. (1995). Object Orientation. John
Wiley & Sons, Inc., 267-319.
 Richard C. (1998). Java Tutorial 2nd Ed. AddisonWesley, 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.
51