Presentation is loading. Please wait.

Presentation is loading. Please wait.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive.

Similar presentations


Presentation on theme: "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive."— Presentation transcript:

1 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive variables: declaration and creation Animated Version

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 2 Objectives After you have read and studied this chapter, you should be able to Describe the difference between object declaration and creation Understand the rules for data names Declare, create, and use objects Describe the difference between primitives and objects Declare, create, and use primitives

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 3 Java Objects In Java, everything is modeled by an object: –Circle –Room –NimPlayer Objects have Attributes and Behavior –Attributes: The set of descriptions needed for the object –Behavior: The things that the object can do Examples: –Teacher –Account –Coin

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 4 Classes To create an object inside the computer program, we must provide a definition for objects—how they behave and what kinds of information they maintain —called a class. An object is called an instance of a class. Examples: –Teacher class; instances are joel and shannon –Account class; instances are account123 and account456

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 5 Messages and Methods To instruct a class or an object to perform a task, we send a message to it. You can send a message only to the classes and objects that understand the message you sent to them. A value we pass to an object when sending a message is called an argument of the message. Some give answers, some don’t –deposit(12) –balance()

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 6 account456 : BankAccount account789 : BankAccount account123 : BankAccount Sample Instance Data Value (Attributes) current balance 908.55 1304.98 354.00 All three BankAccount objects possess the same instance data value current balance. The actual dollar amounts are, of course, different.

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 7 How do you implement it? Implement: to write the ideas in a program We need to be able to: –Create objects –Give them a name for reference later –Pass messages to them

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 8 Declaring an Object creates memory space for it, and gives it a name. The name is called an identifier. Object Declaration Account myAccount; Accountcustomer; Studentjan, jim, jon; Vehiclecar1, car2; Object Name One object is declared here. Object Name One object is declared here. Class Name This class must be defined before this declaration can be stated. Class Name This class must be defined before this declaration can be stated.

9 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 9 Identifiers Names for pieces of data, objects, classes, methods Must be: –One Word –Letters, numbers, $, _ –Start with a letter –Not a reserved Java word Convention –Class names start with upper case letters (Account) –Object names start with lower case letters (myAccount)

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 10 Object Creation myAccount = new Account (“Shannon Duvall”) ; More Examples customer = new Customer( ); jon= new Student(“John Java”); car1= new Vehicle( ); Object Name Name of the object we are creating here. Object Name Name of the object we are creating here. Class Name An instance of this class is created. Class Name An instance of this class is created. Argument or Parameter The name of the account holder is given. Argument or Parameter The name of the account holder is given.

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 11 Declaration vs. Creation Customer myCustomer; myCustomer = new Customer( ); Customer myCustomer; myCustomer = new Customer( ); 1. The identifier myCustomer is declared and space is allocated in memory. 2. A Customer object is created and the identifier myCustomer is set to refer to it. 1 2 myCustomer 1 : Customer 2

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 12 Name vs. Objects Customer myCustomer; myCustomer = new Customer( ); Customer myCustomer; myCustomer myCustomer = new Customer( ); : Customer Created with the first new. Created with the second new. Reference to the first Customer object is lost.

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 13 Shortcut Customer myCustomer; myCustomer = new Customer( ); Customer myCustomer; myCustomer = new Customer( ); Customer myCustomer = new Customer();

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 14 Sending a Message myAccount. deposit ( 400 ) ; More Examples account.addInterest(12); student.setName(“john”); car1.startEngine( ); Object Name Name of the object to which we are sending a message. Object Name Name of the object to which we are sending a message. Method Name The name of the message we are sending. Method Name The name of the message we are sending. Argument or Parameter The argument we are passing with the message. Argument or Parameter The argument we are passing with the message.

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 15 Program1 class Program1 { public static void main(String[ ] args) { AccountmyAccount; myAccount = new Account(“Shannon Duvall”); myAccount.deposit(500); myAccount.addInterest(12); myAccount.printAccountInfo(); } Declare a name Create an object Use an object

16 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 16 name rate amount :Account Execution Flow myAccount.deposit(400); Account myAccount; myAccount State-of-Memory Diagram.02 0 S D myAccount = new Account(“S D”); Program Code 400

17 Objects vs. Primitives 4 th Ed Chapter 2 - 17 Primitives Smallest pieces of data (like letters, numbers) Building blocks for objects Don’t have attributes and behavior Still declare and create them Types have lower-case letters since they aren’t classes: int double char boolean String (kinda)

18 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 18 Just like declaring objects! Primitive Declaration int width; doubleearnings; charfirstLetter; booleanisCorrect; String word; Variable Name Same rules for names apply. Variable Name Same rules for names apply. Type int, double, char, boolean Type int, double, char, boolean

19 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 19 Primitive Creation width = 24; More Examples earnings = 315.41; firstLetter= ‘J’; isCorrect= false; word = “John”; Variable Name Name of the variable we are creating here. Variable Name Name of the variable we are creating here. Value To create it, just give it a value that matches its type. It is now said to be initialized. Value To create it, just give it a value that matches its type. It is now said to be initialized.

20 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 20 Shortcut int width; width = 24; int width = 24;

21 Use the variables Now you can use your variables to do calculations or to issue commands between objects. Each “behavior” is a method. Using the behaviors is “calling” the methods. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 21


Download ppt "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive."

Similar presentations


Ads by Google