Ch3-Classes&Objects - Java Software Solutions...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Java Software Solutions Chapter 3 - Using Classes and Objects 1 Miguel A. Labrador Department of Computer Science & Engineering labrador@csee.usf.edu http://www.csee.usf.edu/~labrador Dr. Miguel A. Labrador Using Classes and Objects We can create more interesting programs using predefined classes and related objects We will focus on: object creation and object references 2 Copyright© Dr. Miguel A. Labrador Classes and Objects– 2 – the String class and its methods – the Java standard class library – the Random and Math classes – formatting output – wrapper classes Creating Objects A variable holds either a primitive type or a reference to an object num1 38 3 Copyright© Dr. Miguel A. Labrador Classes and Objects– 3 "Steve Jobs" name1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Creating Objects A class name can be used as a type to declare an object reference variable String title; No object is created with this declaration value of title is nul 4 Copyright© Dr. Miguel A. Labrador Classes and Objects– 4 No object is created with this declaration, value of title is null . ( null is a reserved word). An object reference variable holds the address of an object The object itself must be created separately Creating Objects Generally, we use the new operator to create an object title = new String ("Java Software Solutions"); 5 Copyright© Dr. Miguel A. Labrador Classes and Objects– 5 Creating an object is called instantiation An object is an instance of a particular class This calls the String constructor , which is a special method that sets up the object Invoking Methods We've seen that once an object has been instantiated, we can use the dot operator to invoke its methods count = title.length(); 6 Copyright© Dr. Miguel A. Labrador Classes and Objects– 6 A method may return a value , which can be used in an assignment or expression A method invocation can be thought of as asking an object to perform a service Remember to see http://java.sun.com/javase/6/docs/api/ – The String class is in the java.lang package
Background image of page 2
3 References An object reference can be thought of as a pointer to the location of the object Rather than dealing with arbitrary addresses, we often depict a reference graphically 7 Copyright© Dr. Miguel A. Labrador Classes and Objects– 7 "Steve Jobs" name1 num1 38 Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types: 8 Copyright© Dr. Miguel A. Labrador Classes and Objects– 8 num1 38 num2 96 Before: num2 = num1; num1 38 num2 38 After: Reference Assignment For object references, assignment copies the address: name1 name2 Before: "Steve Jobs" "Steve Wozniak" 9 Copyright© Dr. Miguel A. Labrador Classes and Objects– 9 name2 = name1; name1 name2 After: "Steve Jobs"
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 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 10 Copyright© Dr. Miguel A. Labrador
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/03/2010 for the course COP COP2510 taught by Professor Mlag. during the Fall '10 term at University of South Florida - Tampa.

Page1 / 19

Ch3-Classes&Objects - Java Software Solutions...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online