6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 •...
-
Upload
nguyenminh -
Category
Documents
-
view
224 -
download
2
Transcript of 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 •...
![Page 1: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/1.jpg)
Lecture 4
Classes and Objects
![Page 2: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/2.jpg)
Review
![Page 3: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/3.jpg)
Solutions 1
public static int getMinIndex(int[] values) {
int minValue = Integer.MAX_VALUE;int minIndex = -1;
for(int i=0; i<values.length; i++)if (values[i] < minValue) {
minValue = values[i];minIndex = i;
}
return minIndex; }
![Page 4: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/4.jpg)
Solutions 2public static int getSecondMinIndex(int[] values) {
int secondIdx = -1; int minIdx= getMinIndex(values);
for(int i=0; i<values.length; i++) {if (i == minIdx)
continue;if (secondIdx == -1 ||
values[i] < values[secondIdx])secondIdx = i;
}return secondIdx;
} • What happens if values = {0}? values = {0, 0}? values = {0,1}?
![Page 5: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/5.jpg)
Popular Issues 1
• Array Index vs Array Value
int[] values = {99, 100, 101};
System.out.println(values[0] ); // 99
Values 99 100 101
Indexes 0 1 2
![Page 6: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/6.jpg)
Popular Issues 2
• Curly braces { … } after if/else, for/while
for (int i = 0; i < 5; i++)
System.out.println(“Hi”);
System.out.println(“Bye”);
• What does this print?
![Page 7: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/7.jpg)
Popular Issues 3
• Variable initialization int getMinValue(int[] vals) {int min = 0;for (int i = 0; i < vals.length; i++) {if (vals[i] < min) {
min = vals[i]}
}}
Problem? • What if vals = {1,2,3}? • Set min = Integer.MAX_VALUE or vals[0]
![Page 8: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/8.jpg)
Popular Issues 4
• Variable Initialization – secondMinIndexint minIdx = getMin(vals)
int secondIdx = 0;for (int i = 0; i < vals.length; i++) {
if (i == minIdx) continue;if (vals[i] < vals[secondIdx])
secondIdx = i;}
• What if vals = {0, 1, 2}? • See solutions
![Page 9: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/9.jpg)
Popular Issues 5
Defining a method inside a method
public static void main(String[] arguments) {
public static void foobar () {
}
}
![Page 10: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/10.jpg)
Debugging Notes 1
• Use System.out.println throughout your code to see what it’s doing
for ( int i=0; i< vals.length; i++) {
if ( vals[i] < minVal) {
System.out.println(“cur min: ” + minVal);
System.out.println(“new min: ” + vals[i]);
minVal = vals[i];
}
}
![Page 11: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/11.jpg)
Debugging Notes 2
• Formatting
• Ctrl-shift-f is your friend
for (int i = 0; i < vals.length; i++) {
if (vals[i] < vals[minIdx]) {
minIdx=i;}
return minIdx;}
• Is there a bug? Who knows! Hard to read
![Page 12: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/12.jpg)
Today’s Topics
Object oriented programmingDefining Classes
Using ClassesReferences vs Values
Static types and methods
![Page 13: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/13.jpg)
Today’s Topics
Object oriented programmingDefining Classes
Using ClassesReferences vs Values
Static types and methods
![Page 14: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/14.jpg)
Object oriented programming
• Represent the real world
Baby
![Page 15: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/15.jpg)
Object oriented programming
• Represent the real world
Baby Name
SexWeight
Decibels# poops so far
![Page 16: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/16.jpg)
Object Oriented Programming
• Objects group together – Primitives (int, double, char, etc..)
– Objects (String, etc…)
Baby String name
boolean isMale double weight
double decibels int numPoops
![Page 17: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/17.jpg)
Why use classes? • Why not just primitives?
// little baby alex
String nameAlex;
double weightAlex;
// little baby david
String nameDavid;
double weightDavid;
![Page 18: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/18.jpg)
Why use classes? • Why not just primitives?
// little baby alex
String nameAlex;
double weightAlex;
// little baby david
String nameDavid;
double weightDavid;
// little baby david
String nameDavid2;
double weightDavid2;
David2? Terrible �
![Page 19: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/19.jpg)
Why use classes? • Why not just primitives?
// little baby alex
String nameAlex;
double weightAlex;
// little baby david
String nameDavid;
double weightDavid;
// little baby david
String nameDavid2;
double weightDavid2;
David2? Terrible �
500 Babies? That Sucks!
![Page 20: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/20.jpg)
Why use classes?
Name Weight
Sex …
Baby1
![Page 21: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/21.jpg)
Why use classes?
Name Weight
Sex …
496 more Babies
Name Weight
Sex …
Name Weight
Sex …
Name Weight
Sex …
Baby1 Baby2 Baby3 Baby4 …
![Page 22: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/22.jpg)
Why use classes?
Baby1 Baby2 Baby3 Baby4
496 more Babies …
Nursery
![Page 23: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/23.jpg)
Why use classes?
Baby1 Baby2 Baby3 Baby4
496 more Babies …
Nurse1 Nurse2 Nurse3 Nurse4
More nurses…
Nursery
![Page 24: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/24.jpg)
Why use classes?
Baby
Nurse
[]
[] Nursery
![Page 25: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/25.jpg)
Why use classes?
Nurse
[] ER
[]
Baby
Nursery
Hospital
![Page 26: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/26.jpg)
Defining classes
![Page 27: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/27.jpg)
Class - overviewpublic class Baby {
String name;
boolean isMale;
double weight;
double decibels;
int numPoops = 0; Class Definition
void poop() {
numPoops += 1;
System.out.println(“Dear mother, ”+
“I have pooped. Ready the diaper.”);
}
}
![Page 28: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/28.jpg)
Class - overview
ClassBaby myBaby = new Baby();
Instance
![Page 29: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/29.jpg)
Let’s declare a baby!
public class Baby {
}
![Page 30: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/30.jpg)
Let’s declare a baby!
public class Baby {
fields
methods
}
![Page 31: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/31.jpg)
Note
• Class names are Capitalized
• 1 Class = 1 file
• Having a main method means the class can be run
![Page 32: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/32.jpg)
Baby fields
public class Baby {
TYPE var_name;TYPE var_name = some_value;
}
![Page 33: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/33.jpg)
Baby fields
public class Baby {String name;
double weight = 5.0;
boolean isMale;
int numPoops = 0;
}
![Page 34: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/34.jpg)
Baby Siblings?
public class Baby {String name;
double weight = 5.0;
boolean isMale;
int numPoops = 0;
XXXXX YYYYY;
}
![Page 35: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/35.jpg)
Baby Siblings?
public class Baby {String name;
double weight = 5.0;
boolean isMale;
int numPoops = 0;
Baby[] siblings;
}
![Page 36: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/36.jpg)
Ok, let’s make this baby!
Baby ourBaby = new Baby();
But what about it’s name? it’s sex?
![Page 37: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/37.jpg)
Constructors
public class CLASSNAME{
CLASSNAME ( ) {
}
CLASSNAME ([ARGUMENTS]) {
}
}
CLASSNAME obj1 = new CLASSNAME();
CLASSNAME obj2 = new CLASSNAME([ARGUMENTS])
![Page 38: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/38.jpg)
Constructors
• Constructor name == the class name
• No return type – never returns anything
• Usually initialize fields
• All classes need at least one constructor– If you don’t write one, defaults to CLASSNAME () {
}
![Page 39: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/39.jpg)
Baby constructor
public class Baby {
String name;
boolean isMale;
Baby(String myname, boolean maleBaby){
name = myname;
isMale = maleBaby;
}
}
![Page 40: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/40.jpg)
Baby methods
public class Baby { String name = “Slim Shady”;
...
void sayHi() {
System.out.println(
“Hi, my name is.. “ + name);
}}
![Page 41: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/41.jpg)
Baby methods
public class Baby { String weight = 5.0;
void eat(double foodWeight) {
if (foodWeight >= 0 &&
foodWeight < weight) {
weight = weight + foodWeight;
}
}
}
![Page 42: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/42.jpg)
Baby class
public class Baby { String name;
double weight = 5.0;
boolean isMale;
int numPoops = 0;
Baby[] siblings;
void sayHi() {…}
void eat(double foodWeight) {…}
}
![Page 43: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/43.jpg)
Using classes
![Page 44: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/44.jpg)
Classes and Instances
// class Definition
public class Baby {…}
// class Instances
Baby shiloh = new Baby(“Shiloh Jolie-Pitt”, true);
Baby knox = new Baby(“Knox Jolie-Pitt”, true);
![Page 45: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/45.jpg)
Accessing fields
• Object.FIELDNAME
Baby shiloh = new Baby(“Shiloh Jolie-Pitt”,true)
System.out.println(shiloh.name);
System.out.println(shiloh.numPoops);
![Page 46: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/46.jpg)
Calling Methods
• Object.METHODNAME([ARGUMENTS])
Baby shiloh = new Baby(“Shiloh Jolie-Pitt”,true)
shiloh.sayHi(); // “Hi, my name is ...”
shiloh.eat(1);
![Page 47: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/47.jpg)
References vs Values
![Page 48: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/48.jpg)
Primitives vs References
• Primitive types are basic java types – int, long, double, boolean, char, short, byte, float
– The actual values are stored in the variable
• Reference types are arrays and objects – String, int[], Baby, …
![Page 49: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/49.jpg)
How java stores primitives
• Variables are like fixed size cups
• Primitives are small enough that they just fit into the cup
int double char boolean
![Page 50: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/50.jpg)
How java stores objects
• Objects are too big to fit in a variable – Stored somewhere else
– Variable stores a number that locates the object
Object
![Page 51: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/51.jpg)
How java stores objects
• Objects are too big to fit in a variable – Stored somewhere else
– Variable stores a number that locates the object
Object Object Object
Object Object Object
Object’s location
![Page 52: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/52.jpg)
References
• The object’s location is called a reference
• == compares the references Baby shiloh1 = new Baby(“shiloh”);
Baby shiloh2 = new Baby(“shiloh”);
Does shiloh1 == shiloh2?
![Page 53: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/53.jpg)
References
• The object’s location is called a reference
• == compares the references Baby shiloh1 = new Baby(“shiloh”);
Baby shiloh2 = new Baby(“shiloh”);
Does shiloh1 == shiloh2?
no
![Page 54: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/54.jpg)
References
Baby shiloh1 = new Baby(“shiloh”);
Baby shiloh2 = new Baby(“shiloh”);
Name=“shiloh”
Name=“shiloh”
reference reference
shiloh1 shiloh2
![Page 55: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/55.jpg)
References
Baby mybaby = new Baby(“davy”, true)mybaby.name = “david”
mybaby’s location
name = ‘davy’ ismale = true
…
![Page 56: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/56.jpg)
References
Baby mybaby = new Baby(‘davy’, true)mybaby.name = ‘david’
mybaby’s location
name = ‘david’ Ismale = true
…
![Page 57: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/57.jpg)
References
• Using = updates the reference.
baby1 = baby2
baby2 location
baby1 object
baby2 object
baby2 location
baby1 baby2
![Page 58: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/58.jpg)
References
• Using = updates the reference.
baby1 = baby2
baby2 location
baby1 object
baby2 object
baby1 baby2
![Page 59: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/59.jpg)
References
• using [ ] or – Follows the reference to the object – May modify the object, but never the reference
• Imagine – Following directions to a house – Moving the furniture around
• Analogous to – Following the reference to an object – Changing fields in the object
![Page 60: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/60.jpg)
Methods and references
void doSomething(int x, int[] ys, Baby b) { x = 99;ys[0] = 99;b.name = “99”;
}...
int i = 0; int[] j = {0};Baby k = new Baby(“50”, true);doSomething(i, j, k);
i=? j=? k=?
![Page 61: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/61.jpg)
static types and methods
![Page 62: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/62.jpg)
static
• Applies to fields and methods
• Means the field/method – Is defined for the class declaration,
– Is not unique for each instance
![Page 63: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/63.jpg)
static
public class Baby {static int numBabiesMade = 0;
}Baby.numBabiesMade = 100; Baby b1 = new Baby();Baby b2 = new Baby(); Baby.numBabiesMade = 2;
What is b1.numBabiesMade? b2.numBabiesMade?
![Page 64: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/64.jpg)
static example
• Keep track of the number of babies that have been made.
public class Baby {int numBabiesMade = 0;Baby() {
numBabiesMade += 1;}
}
![Page 65: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/65.jpg)
static field
• Keep track of the number of babies that have been made.
public class Baby {
static int numBabiesMade = 0;Baby() {
numBabiesMade += 1;}
}
![Page 66: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/66.jpg)
static method
public class Baby {
static void cry(Baby thebaby) {
System.out.println(thebaby.name + “cries”);
}
}
Or
public class Baby {
void cry() {
System.out.println(name + “cries”);
}
}
![Page 67: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/67.jpg)
static notes
• Non-static methods can reference static methods, but not the other way around –Why?
public class Baby {String name = “DMX”;static void whoami() {
System.out.println(name);}
}
![Page 68: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/68.jpg)
main
• Why is main static?
public static void main(String[] arguments) {}
![Page 69: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/69.jpg)
Assignment 4
• Modeling Book and Libraries– class Book {} – class Library{}
• Books can be – Borrowed – Returned
• Library – Keeps track of books – Hint: use Book[]
![Page 70: 6.092 Lecture 4: Classes and Objects - MIT OpenCourseWare · PDF fileDebugging Notes 1 • Use ... How java stores objects • Objects are too big to fit in a variable ... 6.092 Lecture](https://reader031.fdocuments.in/reader031/viewer/2022030413/5a9f4d037f8b9a8e178c8eaa/html5/thumbnails/70.jpg)
MIT OpenCourseWarehttp://ocw.mit.edu
6.092 Introduction to Programming in Java January (IAP) 2010
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.