Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Java (vs. C++). 2 Object-oriented Programming Java and C++ are the most popular object-oriented programming languages Java Sun Microsystems in 1990.

Similar presentations


Presentation on theme: "1 Java (vs. C++). 2 Object-oriented Programming Java and C++ are the most popular object-oriented programming languages Java Sun Microsystems in 1990."— Presentation transcript:

1 1 Java (vs. C++)

2 2 Object-oriented Programming Java and C++ are the most popular object-oriented programming languages Java Sun Microsystems in 1990 C++ AT&T Bell Labs in 1979

3 3 On the Web API Specification http://java.sun.com/j2se/1.4.2/docs/api/ Java Tutorial http://java.sun.com/docs/books/tutorial/getStarted/i ndex.html http://java.sun.com/docs/books/tutorial/getStarted/i ndex.html JDK http://java.sun.com/j2se/1.4.2/download.html Eclipse http://www.eclipse.org

4 4 Command Line C:\temp\p1>type ClassA.java import java.lang.*; import java.util.*; public class ClassA { public static void main(String[] args) { System.out.println("argc=" + args.length); } C:\temp\p1>javac ClassA.java C:\temp\p1>java ClassA argc=0 C:\temp\p1>java ClassA a1 a2 a3 argc=3 C:\temp\p1>

5 5 Simple and robust No pointer No multiple inheritance No operator overloading No goto statement No structure and union data structures Automatic garbage collection Array bounds-checking Reference Semantics!!

6 6 Other features TCP/IP networking just like a local file system Multi-threading support Dynamic loading Loads classes (from the internet) as needed Class Path The cost: Java is considered much slower than C++

7 7 Bytecode JVM OS kernel Binary code OS kernel JAVA source code C++ source code javac JIT compiler C++ compiler gcc, g++ Java interpreter Java compiler Both compiled and interpretedCompiled Interpreted or Compiled

8 8 Type System Basic types Integral types boolean byte short int long char Floating point float double Reference types (classes and interfaces) Array types C++: templates, typedef, pointers

9 9 Reference Types Interface Can extend other interfaces (Multiple inheritance) Class extends one class, implements many interfaces Single inheritance, Multiple subtyping Single root: Object Field layout as in single-inheritance No field declaration in interfaces Equality == equals() Instance Duplication clone()

10 10 Arrays void printAll(Object[] arr) {… arr[i].toString() …} Student[] students = new Student[20]; printAll(students); Object[] arr = students; arr[0] = new Object(); students[0].id =... void printAll(Object[] arr) {… arr[i].toString() …} Student[] students = new Student[20]; printAll(students); Object[] arr = students; arr[0] = new Object(); students[0].id =...

11 11 Exceptions public void f1() { try { readFile("file1.txt"); … } catch(SocketException e) {…} catch(IOException e) {…} catch(RuntimeException e) {…} catch(Throwable e) {…} finally {…} } public void readFile(String s) throws IOException { // Do something } public void f1() { try { readFile("file1.txt"); … } catch(SocketException e) {…} catch(IOException e) {…} catch(RuntimeException e) {…} catch(Throwable e) {…} finally {…} } public void readFile(String s) throws IOException { // Do something }

12 12 Initialization Class variables Zero and Null Automatic variable Not initialized (checked by compiler) class A { Vector v_; public A() { // v_ is null!! } class A { Vector v_; public A() { // v_ is null!! }

13 13 Export status pubilic Accessible to all protected Accessible only to descendants private Accessible only within the class package Accessible to all classes in this package

14 14 Field/method/class modifiers Field volatile, final, static Class abstract, final, static (inner classes) Method synchronized, native, abstract, final, static

15 15 Shadowing, overloading, overriding class Point { int x = 0, y = 0; void move(int dx, int dy) { x += dx; y += dy; } } class RealPoint extends Point { float x = 0.0, y = 0.0; void move(int dx, int dy) { move((float)dx, (float)dy); } void move(float dx, float dy) { x += dx; y += dy; } } class Point { int x = 0, y = 0; void move(int dx, int dy) { x += dx; y += dy; } } class RealPoint extends Point { float x = 0.0, y = 0.0; void move(int dx, int dy) { move((float)dx, (float)dy); } void move(float dx, float dy) { x += dx; y += dy; } }

16 16 Runtime information (1/2) Dynamic vs. static type Shape s = new Rectangle(); Message dispatch s.draw(); Subtyping tests C++: only for virtual Triangle T = null; if(s instanceof Triangle) s = (Triangle) s; Triangle T = null; if(s instanceof Triangle) s = (Triangle) s;

17 17 Runtime information (2/2) Reflection mechanism Under java.lang.reflect Defines classes such as: Method, Field, Constructor, etc.. public void fieldsOf(Object a) { Class c = a.getClass(); Field[] fields = c.getFields(); for(int i = 0; i < fields.length; ++i) System.out.println(fields[i].toString()); } public void fieldsOf(Object a) { Class c = a.getClass(); Field[] fields = c.getFields(); for(int i = 0; i < fields.length; ++i) System.out.println(fields[i].toString()); }

18 18 Dispatching problem Object o receives message m One implementation of m is invoked, according to the dynamic type of o. Examples: Type A  invoke m 1 Type F  invoke m 1 Type G  invoke m 2 Type I  invoke m 3 Type C  message not understood Type H  message ambiguous Statically typed languages make sure these errors never occur


Download ppt "1 Java (vs. C++). 2 Object-oriented Programming Java and C++ are the most popular object-oriented programming languages Java Sun Microsystems in 1990."

Similar presentations


Ads by Google