Java (vs. C++)
1
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
2
On the Web

API Specification


Java Tutorial


http://java.sun.com/docs/books/tutorial/getStarted/in
dex.html
JDK


http://java.sun.com/j2se/1.4.2/docs/api/
http://java.sun.com/j2se/1.4.2/download.html
Eclipse

http://www.eclipse.org
3
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>
4
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!!
5
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++
6
Interpreted or Compiled
JAVA
source code
Java compiler
javac
Bytecode
Java
JVM
interpreter OS kernel
C++
source code
JIT
compiler
Both compiled and interpreted
C++ compiler
gcc, g++
Binary code
OS kernel
Compiled
7
Type System

Basic types

Integral types




Floating point




boolean
byte short int long
char
float double
Reference types (classes and interfaces)
Array types
C++: templates, typedef, pointers
8
Reference Types

Interface

Can extend other interfaces


Class

extends one class, implements many interfaces



No field declaration in interfaces
Equality



Single inheritance, Multiple subtyping
Single root: Object
Field layout as in single-inheritance


(Multiple inheritance)
==
equals()
Instance Duplication

clone()
9
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 = ...
10
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
}
11
Initialization

Class variables


Zero and Null
Automatic variable

Not initialized (checked by compiler)
class A {
Vector v_;
public A() {
// v_ is null!!
}
}
12
Export status

pubilic


protected


Accessible only to descendants
private


Accessible to all
Accessible only within the class
package

Accessible to all classes in this package
13
Field/method/class modifiers

Field


Class


volatile, final, static
abstract, final, static (inner classes)
Method

synchronized, native, abstract, final, static
14
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;
}
}
15
Runtime information (1/2)

Dynamic vs. static type


Message dispatch


Shape s = new Rectangle();
s.draw();
C++: only for virtual
Subtyping tests
Triangle T = null;
if(s instanceof Triangle)
s = (Triangle) s;
16
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());
}
17
Dispatching problem

Object o receives message m


One implementation of m is invoked,
according to the dynamic type of o.
A
Examples:






Type
Type
Type
Type
Type
Type

A
F
G
I
C
H






B
invoke
invoke
invoke
invoke
m1
m1
m2
m3
m2
D
G
m1
C
E
H
m3
F
I
message not understood
message ambiguous
Statically typed languages make sure these errors never occur
18
Descargar

Software Design - Technion – Israel Institute of Technology