Java Yingcai Xiao.

Slides:



Advertisements
Similar presentations
Chapter 2 Types & Exceptions Yingcai Xiao. Part I Moving from C++/Java to C#
Advertisements

ITEC200 – Week03 Inheritance and Class Hierarchies.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Data Structure and Algorithm 1 Yingcai Xiao. You Me The Course (
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
OOP Languages: Java vs C++
Java Security. Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security Manager.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming. Table of Contents History What is C++? Development of C++ Standardized C++ What are the features of C++? What is Object Orientation?
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
University of Houston-Clear Lake Proprietary© 1997 Evolution of Programming Languages Basic cycle of improvement –Experience software difficulties –Theory.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
C# Yingcai Xiao. Part I Moving from Java to C# .NET Framework ’ s Data Types: CTS Six categories of data types in CTS: system-defined: Primitives (int,
JAVA BASICS Prepared by The Smartpath Information Systems
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Programming in Java CSCI-2220 Object Oriented Programming.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Constructors & Garbage Collection Ch. 9 – Head First Java.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Object-Oriented Programming Chapter Chapter
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
ISBN Object-Oriented Programming Chapter Chapter
Introduction to Object-Oriented Programming Lesson 2.
Object Oriented Software Development 4. C# data types, objects and references.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Yingcai Xiao Programming and Debugging in Unity Yingcai Xiao.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Memory Management in Java Mr. Gerb Computer Science 4.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Design issues for Object-Oriented Languages
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Classes C++ representation of an object
Programming and Debugging
Static data members Constructors and Destructors
Java Programming Language
Chapter 5 Classes.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
OOP’S Concepts in C#.Net
C# for Unity3D Yingcai Xiao.
Polymorphism Polymorphism
Constructors and Destructors
Programming and Debugging
Java Programming Language
Classes C++ representation of an object
Lecture 10 Concepts of Programming Languages
CMSC 202 Constructors Version 9/10.
Presentation transcript:

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

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.

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

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.

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.

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.

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.

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

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 0x12345678 int width int height Rectangle () Rectangle (int w, int h) Area () 3 4 0x12345678 Dereferencing rect-> int area = rect->Area(); Please note the notation difference between a “pointer/reference” and a “name” in this lecture.

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 0x01234567 a = height*width; return a; 0x01234567 (text memory) http://www.cs.uakron.edu/~xiao/ics-f99/fun-ptrs.html

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

References in Java 0x12345678 int width int height Rectangle () Rectangle rect = new Rectangle (3, 4); // Use the second constructor rect 0x12345678 int width int height Rectangle () Rectangle (int w, int h) Area () 3 4 0x12345678 Dereferencing int area = rect.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) } 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

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.