1 / 26

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

avesey
Download Presentation

Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Java as a Programming Language • Object-oriented Encapsulation Inheritance Polymorphism • Strongly typed • Compiled and “Interpreted”. • Compiled once and run anywhere.

  5. 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 • 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) Program statements are interpreted one at a time during the run-time. Java Interpreter on OS2 (java) Binary Code for OS1 Binary Code for OS2 OS1 OS2

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

  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 All programming statements are compiled at compile time. Java JIT Compiler on OS2 Binary Code for OS1 Binary Code for OS2 OS1 OS2

  9. Differences between C++ & Java

  10. Differences between C++ & Java

  11. 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 The “Object” class

  12. More differences between C++ & Java http://www.cprogramming.com/tutorial/java/syntax-differences-java-c++.html

  13. Internal Memory Structures of Data Store

  14. 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 What is Data Type?

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

  16. 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 (text memory) C++ Function Pointer http://www.cs.uakron.edu/~xiao/ics-f99/fun-ptrs.html

  17. Class Name; In Java:“Rectangle rect” declares a reference of class Rectangle. rect “rect” is the name of a memory space that stores a reference. Instantiating a Class (in Java) 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++;

  18. Rectangle rect = new Rectangle (3, 4); // Use the second constructor rect 0x12345678 References in Java • Dereferencing int area = rect.Area();

  19. 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 Class Code

  20. 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). Signature of a Method

  21. Value Types are Stack Objects: memory allocated at compile time on the stack primitives (int, double, …) are value types, auto destruction, no garbage collection needed less overhead, code runs faster less flexible, sizes need to be known at compile time Value and Reference Types

  22. Reference Types are Heap Objects: memory allocated at run time on the heap objects of reference types are created using “new” garbage collected more flexible, sizes need not to be known at compile time more overhead, code runs slower Value and Reference Types

  23. Classes in Java (C# too) define reference types (heap objects) Struct in C# defines value types (stack objects. Value types can’t be derived from other types except interfaces. Value and Reference Types

  24. System.GC • Reference Counter • A reference counter is attached to each heap object when it is created by the “new” operator. • It increases by one when a reference is assigned a value of its address. • It decreases by one when one of its references is out of scope. • Garbage collection will free the memories of the objects with their reference counters being zero. • Garbage collection is a system-wide operation, will slow down all running programs. • The timing of garbage collection is determined by the operating system not by a program. • This non-deterministic behavior causes problems for real-time applications. • One can use System.GC.Collect() to force a garbage collection. Garbage Collection

  25. Destructor in C++ is called just before an object is destroyed by the program, when the object is freed (for heap objects) or out of scope (for stack objects). • No distructor for a class in Java. • Every class inherits the Finalize( ) method from System.Object. • It is invoked just before the memory of an object is freed by the garbage collector. • You don’t free memories in Finalize (), but to release resource handles, e.g., file handles, open databases, and network ports. Garbage Collection

  26. 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); Typecast References

More Related