Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Yingcai Xiao.

Similar presentations


Presentation on theme: "Java Yingcai Xiao."— Presentation transcript:

1 Java Yingcai Xiao

2 Part I Moving from C++ to Java

3 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

4 Compiled and “Interpreted”. Compiled once and run anywhere.
Java as a Programming Language Object-oriented Encapsulation Inheritance Polymorphism Strongly typed Compiled and “Interpreted”. Compiled once and run anywhere.

5 Traditional Compilation (Linking) C++
Source Code for Language 1 Language 1 Compiler on OS1 Binary Code for OS1 OS1 Source Code for Language 1 Language 1 Compiler on OS2 Binary Code for OS2 OS2

6 Program statements are interpreted one at a time during the run-time.
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.

7 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.

8 All programming statements are compiled at compile time.
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.

9 Differences between C++ & Java
Ending a block with a semicolon Class myClass{ }; } Object instance creation myClass myObject; //myObject is an instance myClass *myPointer = new myClass(); //myPointer is a pointer to an instance myClass myReference //myReference is a reference (an internal pointer) to an instance Dereferencing myPointer-> 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 “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()

11 More differences between C++ & Java

12 Internal Memory Structures of Data Store

13 What is Data Type? 8 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. i 8

14 C++ Pointer 0x12345678 int width int height Rectangle ()
A pointer in C++ is a memory location that stores an address. Rectangle *rect = new Rectangle (3, 4);  rect 0x int width int height Rectangle () Rectangle (int w, int h) Area () 3 4 0x Dereferencing rect-> int area = rect->Area(); Please note the notation difference between a “pointer/reference” and a “name” in this lecture.

15 C++ Function Pointer 0x01234567 a = height*width; return a;
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 0x a = height*width; return a; 0x (text memory)

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++;

17 References in Java 0x12345678 int width int height Rectangle ()
Rectangle rect = new Rectangle (3, 4); // Use the second constructor rect 0x int width int height Rectangle () Rectangle (int w, int h) Area () 3 4 0x Dereferencing int area = rect.Area();

18 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

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 can’t derive from other types except interfaces.

21 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.

22 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) } 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 (...); Day 2/18/2016, vDx next, abstract, done with value type property, but not boxing

23 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); Eve 2/23/16 will start here next.


Download ppt "Java Yingcai Xiao."

Similar presentations


Ads by Google