Download presentation
Presentation is loading. Please wait.
Published byDora Waters Modified over 9 years ago
1
1 Training Lecture 1
2
2 Primitive Data Declaration and Assignments Code State of Memory int firstNumber, secondNumber; firstNumber = 234; secondNumber = 87; A A int firstNumber, secondNumber; B B firstNumber = 234; secondNumber = 87; int firstNumber, secondNumber; firstNumber = 234; secondNumber = 87; firstNumber secondNumber A. A. Variables are allocated in memory. B. B. Values are assigned to variables. 234 87
3
3 Assigning Numerical Data Code State of Memory int number; number = 237; number = 35; number A. A. The variable is allocated in memory. B. 237 number B. The value 237 is assigned to number. 237 int number; number = 237; number = 35; A A int number; B B number = 237; C C number = 35; C. 35 237. C. The value 35 overwrites the previous value 237. 35
4
Assigning Objects Code State of Memory Customer var; var = new Customer( ); var A. A. The variable is allocated in memory. Customer var; var = new Customer( ); A A Customer var; B B var = new Customer( ); C C B. var B. The reference to the new object is assigned to var. Customer C. var. C. The reference to another object overwrites the reference in var. Customer
5
Having Two References to a Single Object Code State of Memory Customer clemens, twain; clemens = new Customer( ); twain = clemens; Customer clemens, twain, clemens = new Customer( ); twain = clemens; A A Customer clemens, twain; B B clemens = new Customer( ); C C twain = clemens; A. A. Variables are allocated in memory. clemens twain B. clemens B. The reference to the new object is assigned to clemens. Customer C. clemens customer. C. The reference in clemens is assigned to customer.
6
Object Creation myWindow = new JFrame ( ) ; More Examples Customer Customer customer; 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 No arguments are used here. Argument No arguments are used here.
7
Declaration vs. Creation Customer customer; customer = new Customer( ); Customer customer; customer = new Customer( ); 1. The identifier customer is declared and space is allocated in memory. 2. A Customer object is created and the identifier customer is set to refer to it. 1 2 customer 1 : Customer 2
8
State-of-Memory vs. Program customer : Customer State-of-Memory Notation customer : Customer Program Diagram Notation Class Name Object Name
9
Name vs. Objects Customer customer; customer = new Customer( ); Customer customer; customer customer = new Customer( ); : Customer Created with the first new. Created with the second new. Reference to the first Customer object is lost.
17
JFrame myWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle (“My First Java Program”); myWindow.setVisible(true); Execution Flow myWindow.setSize(300, 200); Jframe myWindow; myWindow myWindow.setVisible(true); State-of-Memory Diagram : JFrame width height title visible 200 My First Java … 300 true myWindow = new JFrame( ); myWindow.setTitle (“My First Java Program”); The diagram shows only four of the many data members of a JFrame object. Program Code
18
4 th Ed Chapter 5 - 18 Using == With Objects (Sample 1) String str1 = new String("Java"); String str2 = new String("Java"); if (str1 == str2) { System.out.println("They are equal"); } else { System.out.println("They are not equal"); } They are not equal Not equal because str1 and str2 point to different String objects.
19
4 th Ed Chapter 5 - 19 Using == With Objects (Sample 2) String str1 = new String("Java"); String str2 = str1; if (str1 == str2) { System.out.println("They are equal"); } else { System.out.println("They are not equal"); } They are equal It's equal here because str1 and str2 point to the same object.
20
4 th Ed Chapter 5 - 20 Using equals with String String str1 = new String("Java"); String str2 = new String("Java"); if (str1.equals(str2)) { System.out.println("They are equal"); } else { System.out.println("They are not equal"); } They are equal It's equal here because str1 and str2 have the same sequence of characters.
21
4 th Ed Chapter 5 - 21 The Semantics of ==
22
4 th Ed Chapter 5 - 22 In Creating String Objects
23
Example 1
24
24
25
25
26
26
27
Example 2
30
enter the number of Sayarat that you want to process 3 enter the owner then price then model you want to process ali 87000 terios ali 's Car,price : 87000.0 model : terios The tax will be 1000 SR enter the owner then price then model you want to process mohamed 90000 camry mohamed 's Car,price : 90000.0 model : camry The tax will be 1000 SR enter the owner then price then model you want to process
31
31 END of Training Lecture 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.