Title: Java
1Java
2- Part I
- Moving from C to Java
3Data Structures Algorithms
-
- What you should do to design a language? How can
you design a language? - Computer a device for data processing
- storing and processing data
- Programming Data Structures Algorithms
- Computer Languages tools for users to define
data structures to store the data and to develop
algorithms to process the data. -
- Data Types System-defined Types User-defined
Types
4Java as a Programming Language
-
- Object-oriented
- Encapsulation
- Inheritance
- Polymorphism
- Strongly typed
- Compiled and Interpreted.
- Compiled once and run anywhere.
5- Traditional Compilation (Linking) C
6- Java Intermediate Language Java Bytecode
Java Source Code (.java)
Java Compiler (javac) on OS1
Java Compiler (javac) on OS2
Java Bytecode (.class)
Java Interpreter on OS1 (java)
Java Interpreter on OS2 (java)
Binary Code for OS2
Binary Code for OS1
OS1
OS2
- Program statements are interpreted one at a time
during the run-time.
7- An interpreter interprets intermediate code one
line at a time. Slow execution. - A JIT (Just-In-Time) Compiler compiles the
complete code all at once just into native binary
code before execution. Faster execution.
8- JIT Complier Java Bytecode Compiler
Java Source Code (.java)
Java Compiler (javac) on OS1
Java Compiler (javac) on OS2
Java Bytecode (.class)
Java JIT Compiler on OS1
Java JIT Compiler on OS2
Binary Code for OS2
Binary Code for OS1
OS1
OS2
- All programming statements are compiled at
compile time.
9Differences between C Java
Difference C Java
Ending a block with a semicolon Class myClass Class myClass
Object instance creation myClass myObject //myObject is an instance myClass myPointer new myClass() //myPointer is a pointer to an instance myClass myReference new myClass() //myReference is a reference (an internal pointer) to an instance
Dereferencing myPointer-gt myReference.
Inheritance Supports multiple inheritance No multiple inheritance The root of all classes is the Object class. There is a class called Class. A Class object describes the internal structures of the object.
Freeing heap memory free(myPointer) Automatically by garbage collection when myReference is out of extent.
10- The root class of all other classes.
- So, an object of any class is an Object
- So we can write
- Object obj new Rectangle (3, 4)
- Constructor Object ()
- String output toString()
- Read matadata getClass()
- Clean up finalize()
- https//docs.oracle.com/javase/7/docs/api/java/la
ng/Object.html
11- More differences between C Java
- http//www.cprogramming.com/tutorial/java/syntax-d
ifferences-java-c.html
12- Internal Memory Structures of Data Store
13- Data types describe the memory layout of objects.
- The name of an object is the name of the memory
space stores its data value. - For example,
- int i 8
- i is the name of the memory space for storing
the data value 8.
8
i
14- A pointer in C is a memory location that stores
an address. - Rectangle rect new Rectangle (3, 4)
-
rect
0x12345678
3
4
int width
int height
Rectangle ()
Rectangle (int w, int h)
Area ()
0x12345678
- Dereferencing rect-gt
- int area rect-gtArea()
- Please note the notation difference between a
pointer/reference and a name in this lecture.
15- A method is a (function) pointer that points to
the code location of the method in the text
memory, i.e., the pointer stores the address of
the code location of the method in the text
memory. -
Area
0x01234567
a heightwidth
return a
0x01234567 (text memory)
http//www.cs.uakron.edu/xiao/ics-f99/fun-ptrs.ht
ml
16- Instantiating a Class (in Java)
Class Name
In Java Rectangle rect declares a reference of
class Rectangle.
A reference to a Rectangle object.
rect
rect is the name of a memory space that stores
a reference.
A reference is an internal pointer, it needs to
point to an object before being dereferenced.
You can not perform arithmetic operations on
references no rect
17Rectangle rect new Rectangle (3, 4) // Use the
second constructor
rect
0x12345678
3
4
int width
int height
Rectangle ()
Rectangle (int w, int h)
Area ()
0x12345678
- Dereferencing
- int area rect.Area()
18class Point public int x public int
y Point p1 new Point () p1.x 1 p1.y
2 Point p2 p1 // Copies the underlying
pointer unless the assignment operator is
overwritten. p2.x 3 p2.y 4 Point p3//Creat
a reference(pointer), no memory allocated p3.x
5 // Will not compile p3.y 6 // Will not
compile
19- Why we have to name properties of different types
differently?
- Signature of a method name, number of
arguments, types of the arguments. Return type
is not part of the signature.
- Overloading two or more methods have the same
name but different arguments.
- Name Mangling encodes the name of an overloaded
method with its signature (by the compiler). The
internal names of the methods are unique (no
internal overloading).
20- Value and Reference Types
- Value Types are Stack Objects
- memory allocated at compile time on the stack
- auto destruction, no garbage collection needed
- less overhead, code runs faster
- less flexible, sizes need to be known at compile
time - Reference Types are Heap Objects
- memory allocated at run time on the heap
- garbage collected
- more flexible, sizes need not to be known at
compile time - more overhead, code runs slower
- Class defines reference types (heap objects)
- Struct in C defines value types (stack objects),
even though new is used to create struct
objects. Value types cant derive from other
types except interfaces.
21- Interfaces
- An interface is a group of zero or more abstract
methods - Abstract methods have no default implementation.
- Abstract methods are to be implemented in a
child class or child struct. - Subclassing an interface by a class or struct is
called implementation of the interface. - An interface can be implemented but not
instantiated. - You cant use an interface class to create an
object. - An interface defines a contract between a type
and users of that type. Used to define software
interface standards. - All interface methods are public, no
specifiers needed. - A class can implement multiple interfaces.
22interface ISecret void Encrypt (byte inbuf,
byte outbuf, Key key) void Unencrypt (byte
inbuf, byte outbuf, Key key) //no
implementation, just prototyping. class Message
ISecret public void Encrypt (byte inbuf,
byte outbuf, Key key) / implementation
here / public void Unencrypt(byte inbuf,
byte outbuf, Key key) / implementation
here / Message msg new Message() //
e.g. check if object msg implements interface
ISecret if (msg is ISecret) // type checking,
// an object of a child type is also an object
of the parent type, but not the other way around
ISecret secret (ISecret) msg // from child
to parent, explicit cast secret.Encrypt
(...)
23- class Parent
- int i
- setParent(int k) ik
- class Child Parent
- int j
- public setChild(int m, int n) im jn
- Parent p1 new Parent () p1.setParent(1)
- Child c1 new Child() c1.setChild(2,3)
- // child objects can be treated as parent objects
- Parent p2 (Parent) c1 p2.setParent(4)
- // dont do this!!! parent objects cant be
treated as child objects - Child c2 (Child) p1 c2.setChild(5,6)