Java

Java
Yingcai Xiao
Part I
Moving from C++ to Java
Data 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
Java as a Programming Language
 Object-oriented
Encapsulation
Inheritance
Polymorphism
 Strongly typed
 Compiled and “Interpreted”.
 Compiled once and run anywhere.
Traditional Compilation (Linking) C++
Source Code for Language 1
Source Code for Language 1
Language 1 Compiler on OS1
Language 1 Compiler on OS2
Binary Code for OS1
Binary Code for OS2
OS1
OS2
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 OS1
Binary Code for OS2
OS1
OS2
Program statements are interpreted one at a time during the run-time.
JIT Compiler
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.
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 OS1
Binary Code for OS2
OS1
OS2
All programming statements are compiled at compile time.
Differences between C++ & Java
Difference
C++
Java
Ending a
block with a
semicolon
Class myClass{
…
};
Class myClass{
…
}
Object
instance
creation
myClass myObject;
myClass myReference
//myObject is an instance
= new myClass();
myClass *myPointer
//myReference is a reference (an internal
= new myClass();
pointer) to an instance
//myPointer is a pointer to an
instance
Dereferencing myPointer->
myReference.
Inheritance
No multiple inheritance
Supports 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.
The “Object” class
 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/lang/Object.html
More differences between C++ & Java
http://www.cprogramming.com/tutorial/java/
syntax-differences-java-c++.html
Internal Memory Structures of Data Store
What is Data Type?
 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
8
“i” is the name of the memory space for storing the data value 8.
C++ Pointer
A pointer in C++ is a memory location that stores an address.
Rectangle *rect = new Rectangle (3, 4);
rect
0x12345678
0x12345678
• Dereferencing rect->
int area = rect->Area();
• Please note the notation
difference between a
“pointer/reference” and a “name”
in this lecture.
int width
3
int height
4
Rectangle ()
Rectangle (int w, int h)
Area ()
C++ Function Pointer
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 = height*width;
0x01234567
(text memory)
http://www.cs.uakron.edu/~xiao
/ics-f99/fun-ptrs.html
return a;
Instantiating a Class (in Java)
Class Name;
In Java: “Rectangle rect” declares a reference of class Rectangle.
rect
A reference to a Rectangle object.
“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++;
References in Java
Rectangle rect = new Rectangle (3, 4); // Use the second constructor
rect
0x12345678
• Dereferencing
int area = rect.Area();
0x12345678
int width
3
int height
4
Rectangle ()
Rectangle (int w, int h)
Area ()
Class Code
class 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
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).
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 can’t derive
from other types except interfaces.
Interfaces
 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 can’t 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.
Interface Example
interface 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 (...);
}
Typecast References
class Parent {
int i;
setParent(int k) {i=k;} }
class Child: Parent{
int j;
public setChild(int m, int n) {i=m; j=n;} }
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);
// don’t do this!!! parent objects can’t be treated as child objects
Child c2 = (Child) p1; c2.setChild(5,6);