EECE 309: Software Engineering

Slides:



Advertisements
Similar presentations
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Advertisements

CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
OOP Languages: Java vs C++
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
EECE 310: Software Engineering Lecture 2: Understanding Objects in Java and Types.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
More C++ Features True object initialisation
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Types in programming languages1 What are types, and why do we need them?
Constructors & Garbage Collection Ch. 9 – Head First Java.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Design issues for Object-Oriented Languages
Constructors and Destructors
Object Lifetime and Pointers
Dynamic Storage Allocation
Computer Organization and Design Pointers, Arrays and Strings in C
Overview 4 major memory segments Key differences from Java stack
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Java Primer 1: Types, Classes and Operators
Constructor & Destructor
Java Review: Reference Types
Pointers and References
Continuing Chapter 11 Inheritance and Polymorphism
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
CSC 253 Lecture 8.
Object Based Programming
Chapter 9 Inheritance and Polymorphism
CSC 253 Lecture 8.
Overview 4 major memory segments Key differences from Java stack
Extending Classes.
Java Programming Language
Overview of Memory Layout in C++
Initialization List.
Constructors and destructors
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
From C++ to Java Java history: Oak, toaster-ovens, internet language, panacea What it is O-O language, not a hybrid (cf. C++) compiled to byte-code, executed.
Constructors and Destructors
EECE 310: Software Engineering
Core Concepts.
9-10 Classes: A Deeper Look.
Java Programming Language
Java Basics Data Types in Java.
A Deeper Look at Classes
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
9-10 Classes: A Deeper Look.
Classes and Objects Object Creation
Corresponds with Chapter 5
SPL – PS3 C++ Classes.
SPL – PS2 C++ Memory Handling.
Initialization List.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

EECE 309: Software Engineering Lecture 2: Understanding Objects in Java and Types

Why Java ? Automated memory management Strong type safety for security Portability through VM No legacy baggage of C Excellent in-built libraries for networking/graphics

Brief history of Java … Developed by James Gosling at Sun in 1995 Initially code-named ‘Oak’, was meant to target small, embedded devices (such as microwaves) Became popular with the growth of WWW – Netscape had support for Applets Mature alternative to C/C++ by late nineties MS develops C# as alternative to Java (early 2000) Today: Java used mainly in server/business apps

Learning Objectives Differentiate between objects on stack and heap and understand garbage collection Understand differences between mutable and immutable objects and Java calling semantics Define apparent types and actual types and identify them for various statements Identify implicit type conversions in Java and overloading

Objects and Variables Local variables Objects Confined to single context: allocated on stack Primitive types such as int or object references Must be initialized before use (or fail compilation) Objects Shared among multiple procedure contexts Allocated on heap using new operator Can be initialized after creation (by constructor)

Variables and Objects: Example heap int i = 6; int j; int [] a = {1, 3, 5, 7, 9}; int [] b = new int[3]; String s = “abcdef”; String t = null; j = i; b = a; t = s; 1 3 5 7 9 i = 6 j = 6 j 0 0 0 a b “abcdef” s t = null t Stack

Object references All object references are uninitialized initially Can be initialized to null, but not necessary Need to explicitly allocate the object on the heap or assign it to (the start of ) an existing object No pointer arithmetic possible once assigned No need to explicitly de-allocate the reference (garbage collection frees it when not in use) Can be passed to procedures and copied around

Example of Objects and References { Object b = null; Object a = new Object(); b = a; } int c = b.hashCode(); Reference b is allocated on stack and initialized to null Reference a is allocated on stack Object is allocated on the heap and reference a points to it b and a both point to the same object a goes out of scope, so only b points to object b goes out of scope too, so nobody points to the object. Object is automatically reclaimed by garbage collector

Learning Objectives Differentiate between objects on stack and heap and understand garbage collection Understand differences between mutable and immutable objects and Java calling semantics Define apparent types and actual types and identify them for various statements Identify implicit type conversions in Java and overloading

Object Mutability By default, Java objects are mutable Modifications made through one reference will be visible when the object is accessed through another reference to the object Example: Arrays int [] a = {1, 3, 5, 7, 9}; a[3] = -1; b = a; b[4] = -2; 1 3 5 -1 -2 1 3 5 7 9 1 3 5 -1 9

Exception: Immutable objects State of immutable object never changes once it is assigned Example: String object String s1 = “abcdef”; String s2 = “ghij”; String s3 = s1; s3 = s1 + s2; String s4 = s3; s4 = s2 + s1; “abcdef” “ghij” “abcdefghij” “ghijabcdef” Heap

Group Activity: Try it yourself What happens after these ? int[ ] a = {1, 2, 3}; int[ ] b = new int[2]; int[] c = a; int x = c[0]; b[0] = x; a[1] = 6; x = b[1]; int y = a[1]; What happens after these ? String s1 = “ace”; String s2 = “f”; String s3 = s1; String s4 = s3 + s2; s1 = s4; s4 = s1 + s2;

Java Calling Convention Some textbooks will say the following In Java, Objects are passed by reference, and primitives are passed by value… […] This is wrong ! Java has only call-by-value Both primitive types and object references are passed by value i.e., copied in to the stack frame Can modify the object through the passed in reference provided object is not immutable

Calling convention: Call-by-value stack void foo(int a, int[] b) { a = a + 1; b[0] = 3; } heap a = 11 a = 10 b 3 int[] q = new int[3]; int p = 10; foo (p, q); // What are p and q’s value ? p = 10 q

Method calls in Java – Example 1 public static void swap(int a, int b) { int temp = a; a = b; b = temp; } m = 5; n = 10; swap(m, n); // What are the values of m and n here ?

Method calls in Java – Example 2 public static void findAndRemove(int[ ] a, int m) { if (a ==null) return; // avoid null pointer exception for (int i = 0; i < a.length; ++i) { if (a[i]==m) a[i] = 0; } int[ ] b = { 0, 2, 4, 6, 8 }; findAndRemove( b, 2 ); // What is the value of the array b here ?

Method calls in Java – Example 3 public static void findAndRemove(int[ ] a, int m) { if (a ==null) return; // avoid null pointer exception a = new int[5]; // initialized to all ‘0’s for (int i = 0; i < a.length; ++i) { if (a[i]==m) a[i] = 0; } int[ ] b = { 0, 2, 4, 6, 8 }; findAndRemove( b, 2 ); // What is the value of the array b here ?

Learning Objectives Differentiate between objects on stack and heap and understand garbage collection Understand differences between mutable and immutable objects and Java calling semantics Define apparent types and actual types and identify them for various statements Identify implicit type conversions in Java and overloading

Type Safety Java is strongly typed (i.e., type-safe) No “unsafe” casts are possible (e.g., reference to int) Type-safety enforced by compiler (by language design) Memory safety follows from type-safety No writing past the end of an object (buffer overflows) Automatic garbage collection provided by runtime, so no explicit frees or dangling pointers

Type Checking public static void findAndRemove(int[] a, int m); The compiler checks the call-site of the function to ensure that it matches its type-signature. int[] b = [0, 2, 4, 8, 10]; int n = 5; Vector v = new Vector(); findAndRemove(b, n); // Is this legal ? findAndRemove(n, b); // What about this ? findAndRemove(v, n); // Ok, what about this ?

Type Hierarchy Consider a type S which is a sub-type of T (i.e., S is a sub-class of T). Now, S can be substituted in place of T whenever a function expects T. Example: All objects in Java are sub-types of Object, which defines an equals method. String s1 = “hello; Object 01 = s1; if ( 01.equals(“hello”) ) ….; // legal if ( O1.length() ) …. ; // illegal

Type Conversions Type-checking is done using the apparent type of an object, NOT its actual type If the actual type supports a member function, but the apparent type does not, it is ILLEGAL to invoke the member directly However, if we cast the apparent type of an object to its actual type, we can invoke its member functions String s1 = “hello; Object O1 = s1; if ( ( (String)O1 ).length() ) …. ; // legal NOTE: If the actual type of the object does not match the cast type, a runtime exception is raised (ClassCastException) Example: (Integer)(O1) will raise a runtime exception

Apparent vs. Actual type Apparent type -- the type inferred from declarations Actual type -- the type received at (object) creation NOTE: Apparent type is a super-type of the actual type Statement Apparent type Actual type String s = “abcdef”; String Object o = s; Object s = o; Illegal ! s = (String)o; int [] a = {0, 1, 2}; Int[] o = a; int[] a [2] = 3; Int

Group Activity Which of the following will compile ? For the ones that will compile, determine the result (or exception thrown) Object o = “abc”; Boolean b = new Boolean( o.equals(“a, b, c”) ); char c = o.charAt(1); Object o2 = b; String s = o; String t = (String) o; String u = (String) o2; char d = t.charAt(1);

Learning Objectives Differentiate between objects on stack and heap and understand garbage collection Understand differences between mutable and immutable objects and Java calling semantics Define apparent types and actual types and identify them for various statements Identify implicit type conversions in Java and overloading

Type overloading Same function can have multiple definitions based on argument types and/or return value static int compare(int, float); // defn 1 static int compare(float, float); // defn 2 static int compare(float, int); // defn 3 The compiler “knows” which definition to call depending on the type of the parameters compare(5.0, 3); // Calls defn 3 compare(5.0, 6.0); // Calls defn 2 compare(3, 5.0); // Calls defn 1

Implicit Type Conversions Often, the compiler will implicitly convert one primitive type to another (widening) int => float int => long But long => int, float=>int NOT possible So which version of compare do these call ? compare(3, 5.0); // Definition 1 or Definition 2 ? compare(5.0, 6); // Definition 2 or Definition 3 ?

Matching Rule for Implicit Conversions “Most specific” method Find the method that matches the types of the parameters with the least number of type conversions Example: compare(3, 5.0); // Calls definition 1 compare(5.0, 6) // Calls definition 3 compare(5.0, 3.0); // Calls definition 2 compare(3, 4); // What about this one ? NO “most specific method”  compilation error Avoided by explicit type-casting: compare(3, (float)4 );

Group Activity Consider the following method: void m(Object o, long x, long y); // D1 void m(String s, int x, long y); // D2 void m(Object o, int x, long y); // D3 void m(String s, long x, int y); // D4 Suppose you have the following declarations: Object o; String v; int a; long b;

Group activity (contd…) Which of the definitions match the call (if a call is illegal, please put an ‘x’ near it). m(v, a, b); m(v, a, a); m(v, b, a); m(v, b, b); m(o, b, b); m(o, a, a);