Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.

Slides:



Advertisements
Similar presentations
The Random and String Classes The import statement.
Advertisements

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Chapter 41 Defining Classes and Methods Chapter 4.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.
Classes/Objects. Creating Objects Much like creating variables of primitive types –String name; –type name; Object variables hold references, not values.
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Introduction to Java Programming, 4E Y. Daniel Liang.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
CSE 114 Computer Science I Objects Lake Superior, Michigan.
Lecture 9 Using Objects. Remember: 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String []
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
CSE 501N Fall ‘09 04: Introduction to Objects 08 September 2009 Nick Leidenfrost.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Working With Objects Tonga Institute of Higher Education.
Using Classes and Objects Chapters 3 Creating Objects – Section 3.1 The String Class – Section 3.2 The Scanner Class – Section 2.6 Instructor: Scott Kristjanson.
CSE 1201 Object Oriented Programming Using Classes and Objects.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
SEEM Java – Basic Introduction, Classes and Objects.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
Today’s lecture Review chapter 5 go over exercises.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
Chapter 9 Introduction to Arrays Fundamentals of Java.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Chapter 5: Enhancing Classes
Variables in Java A variable holds either
Creating Objects & String Class
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
More Object Oriented Programming
An Introduction to Java – Part II
Classes & Objects: Examples
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Classes, Encapsulation, Methods and Constructors (Continued)
Building Java Programs
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Java Classes Aliases & Null & This
Building Java Programs
Object Oriented Programming Review
Packages & Random and Math Classes
Java Programming Language
Outline Creating Objects The String Class The Random and Math Classes
Lecture 9 Using Objects.
Corresponds with Chapter 5
String Objects & its Methods
Presentation transcript:

Chapter 3 Using Classes and Objects

2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference variable

3 Remember… Primitive Data Types X 5 Primitive Data Type int x = 5; A primitive data type variable contains the value itself, but an object variable contains the address of the object

4 References to Objects  An object reference variable holds the address of an object String s = “Hello”; //same as: String s = new String(“Hello”); s “Hello” Memory location An object variable contains the address of the object

5 Declaring the Object  The object itself must be created separately String s; s Memory location

6 Creating the Object itself  The object itself must be created separately String s; s = “Hello”; s “Hello” Memory location

7 Is this assignment? String s; String t; s = “hello”; t = s; s “Hello” Memory location t

8 Creating Objects, the formula  Generally, we use the new operator to create an object Object objectReference = new Object 0 or more Class name Class name parameters ( ); The identifier or variable name Specifications for the newly created object

9 Creating Objects  Generally, we use the new operator to create an object String name = new String (”Blanca J. Polo"); This keyword new calls the String constructor That is the one that creates/builds the object Creating an object is called instantiation An object is an instance of a particular class

10 Invoking Methods  We've seen that once an object has been instantiated, we can use the dot operator to invoke its methods int nameLength = 0; nameLength = name.length()  A method may return a value  The returned value can be used in an assignment or expression

11 The main method is void A void method doesn’t return anything Return types can be of any Primitive Data Type Return types can be of any Object kind, either pre-made by JAVA or made by you

12 Review: The difference ”Blanca Polo" Srting name int num38

13 Primitive Data Type Assignment Review  The act of assignment takes a copy of a value and stores it in a variable  For primitive types: num1 38 num2 96 Before: num2 = num1; num1 38 num2 38 After:

14 Object Assignment Review  For object references, assignment copies the address: name2 = name1; name1 name2 Before: ”Tom Hanks" ”Kurt Russell" name1 name2 After: ”Tom Hanks"

15 Aliases  Two or more references that refer to the same object are called aliases of each other  That creates an interesting situation: one object can be accessed using multiple reference variables  Aliases can be useful, but should be managed carefully  Changing an object through one reference changes it for all of its aliases, because there is really only one object

16 Questions