Miscellaneous OOP topics Java review continued. Simple data types & wrapper classes Simple data types are the built-in types provided as part of the Java.

Slides:



Advertisements
Similar presentations
ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
Advertisements

Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
1 Working with References. 2 References Every object variable is a reference to an object Also true when an object is passed as an argument When the object.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
 It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Chapter 10.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
VBA Modules, Functions, Variables, and Constants
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.
OOP Languages: Java vs C++
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Programming Languages and Paradigms Object-Oriented Programming.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
BPJ444: Business Programming using Java Java basics Tim McKenna
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Chapter 7: Characters, Strings, and the StringBuilder.
Types in programming languages1 What are types, and why do we need them?
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
Object Oriented Software Development 4. C# data types, objects and references.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Today’s lecture Review chapter 5 go over exercises.
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.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
“Unboxing” means taking an Integer object and assigning its value to a primitive int. This is done using the.intValue( ) method. Example; Integer z = new.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
The Object-Oriented Thought Process Chapter 03
Java Unit 11: Inheritance I
Java Primer 1: Types, Classes and Operators
Java Review: Reference Types
Pointers and References
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Java Classes and Objects 4th Lecture
Classes and Objects Static Methods
Java Programming Language
Classes, Objects and Methods
Outline Creating Objects The String Class The Random and Math Classes
Classes and Objects Object Creation
SPL – PS3 C++ Classes.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Miscellaneous OOP topics Java review continued

Simple data types & wrapper classes Simple data types are the built-in types provided as part of the Java language: int, double, char, etc. These differ from the vast majority of data types we usually work with in Java, in that variables of the simple types actually contain values, rather than referring to objects For each primitive data type, the Java API includes a wrapper class, which allows programmers to exchange information between objects and simple-type variables

Wrappers & their uses The wrapper classes are commonly used when we have data in the form of an object (e.g. a String) and want to convert that data to a simple type, or vice versa; the code fragment below illustrates such a situation: String s = JOptionPane.showInputDialog(null, “Please enter your age:”); int n = Integer.parseInt(s);

Primitive to wrapper conversion We can create an instance of a wrapper object in the same way as we create any object, as in the example below: Double dobj = new Double(17.35); We can obtain the original primitive value used to construct the object using a wrapper value method: double d = dobj.doubleValue();

Primitive to wrapper conversion boxing The act of converting a primitive value to a wrapper object is called boxing Starting with Java 5.0, we can shorten this code by using automatic boxing, as in the example below: Character cobj = ‘%’; unboxing Conversion in the other direction (aka unboxing) can also be done automatically: char c = cobj;// automatically calls charValue() Automatic boxing and unboxing applies to parameter passing as well as direct assignment

An object by any other name … Unlike primitive variables, variables of any class data type are not direct containers of data Each object variable can refer to the address of actual data In fact, object variables would be called, in any other language, pointers An important distinction: an object’s name is an indirect reference (indirectly) to (a set of, usually) value(s), while a primitive variable’s name is a direct reference to a value

Objects and equality Because objects are indirect references, care must be taken when comparing the values of two objects Object variables are pointers; the actual values they store are memory addresses, not data Two object variables can point to the same memory address, and/or two memory addresses can contain the same values and be referred to by different variables

Objects and equality For the reasons listed on the previous slide, it is almost never a good idea to compare objects using the == operator Instead, the equals method, and its more general cousin the compareTo method should be defined for any class whose instances you expect to compare

The null value The value null is a special constant that literally means no value Any object variable (that is, any pointer) can be assigned null The advantage of using this value is that we can test any object reference against it (using the == operator) and avoid program errors that might otherwise result The null value can stand in for any object type’s value – so, for example, we can pass null to a method instead of passing an object

Much more ado about nothing You may have already been bitten once or twice by the “Null Pointer Exception” runtime error From this you might deduce (correctly), that object variables are automatically assigned null unless they are assigned to point to something using the new operator and an appropriate constructor So why make an explicit null assignment? – good programmer habit: serves to remind you it’s something to check for – many programming languages aren’t as gentle as Java; an uninitialized pointer in C or C++ is like a loaded gun – not a good idea to wave that thing around and not know where you’re pointing

The new operator & anonymous objects An object can be created and assigned to an object variable using the new operator and its constructor We can also create objects to used on the fly, without any permanent reference Such an object is called an anonymous object

Copy constructors and cloning Constructors are typically used to initialize instance variables, and most classes provide multiple constructors, including a default constructor Some classes also provide a special copy constructor, which is used to create an identical copy of an existing object The code on the next slide illustrates a class with a copy constructor

Example public class Some Object { int example; // default constructor: public SomeObject() { example = 0; } // copy constructor: public SomeObject(SomeObject original) { this.example = original.example; } // etc. }

The clone() method The clone() method (like toString() and equals(), this is inherited from Object) provides essentially the same functionality as a copy constructor The clone() method is the preferred form, according to the Java API The next slide shows how each of these methods could be used to create a copy of an object

Example SomeObject first = new SomeObject(); // creates original object using default constructor SomeObject second = new SomeObject(first); // creates identical object using copy constructor SomeObject third = (SomeObject)first.clone(); // creates another identical object using clone() method // and explicit type cast