Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.
-
Upload
davin-morphew -
Category
Documents
-
view
218 -
download
0
Transcript of Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.
![Page 1: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/1.jpg)
http://proglit.com/
![Page 2: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/2.jpg)
the javalanguage
![Page 3: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/3.jpg)
BY
SA
![Page 4: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/4.jpg)
![Page 5: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/5.jpg)
introduced in 1995 by Sun Microsystems
![Page 6: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/6.jpg)
• imperative, object-oriented• C-style syntax• statically-typed (mostly)• no pointers• JVM (Java Virtual Machine)• garbage collection• exceptions
![Page 7: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/7.jpg)
big standard library• SE (standard edition)• EE (enterprise edition)• ME (micro edition)
![Page 8: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/8.jpg)
Java 6 (2006) Java 7 (2010?)
![Page 9: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/9.jpg)
class (data type definition)• field (data member)• method (function member)
object / instance (piece of data)
![Page 10: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/10.jpg)
class Moose {Rat r;Hamster h;
void foo() {…}}
![Page 11: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/11.jpg)
encapsulation(methods act as “interface” to object’s fields)
![Page 12: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/12.jpg)
object.fieldobject.method(args)
apple.bananaapple.banana.orangenadine.ed()nadine.ed().lauranadine.ed().laura.dale()
![Page 13: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/13.jpg)
object.fieldobject.method(args)
apple.banana(apple.banana).orangenadine.ed()(nadine.ed()).laura((nadine.ed()).laura).dale()
![Page 14: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/14.jpg)
type name;new type(args)
Moose m; // nullm = new Moose();
Moose m = new Moose();
![Page 15: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/15.jpg)
class Moose {Rat r;Hamster h;
Moose() {…} // constructor
void foo() {…}}
![Page 16: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/16.jpg)
Moose() { this.r = new Rat();this.h = new Hamster();
}
![Page 17: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/17.jpg)
Moose(Hamster h, Rat r) {this.r = r;this.h = h;
}
new Moose(new Hamster(), new Rat())
![Page 18: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/18.jpg)
inheritanceAnimal A C
Mammal D E
Cat F G
B
D E
CBA
CBA
![Page 19: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/19.jpg)
Ted
Kate
Milton
illegal
![Page 20: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/20.jpg)
Object
Jack Samantha
BradTed
Kate
LisaMilton
Oliver
![Page 21: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/21.jpg)
class Terry {…} // extends Object
class John extends Ben {…}
![Page 22: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/22.jpg)
Jack
Ted
Kate
Milton
illegal
![Page 23: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/23.jpg)
is-avs.
has-a
Think before using inheritance.
![Page 24: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/24.jpg)
Object
Jack
Ted
Kate
Lisa
Mike
Hugh
Olivia
Uri
Amber
(probably) bad
![Page 25: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/25.jpg)
Hamster h = new Hamster();Mammal m = h;
h.runOnWheel(); // OKm.runOnWheel(); // illegal
![Page 26: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/26.jpg)
void bar(Mammal m) {…}
x.bar(new Mammal())x.bar(new Hamster())
![Page 27: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/27.jpg)
Mammal foo() {if (…) {
return new Mammal();} else {
return new Hamster();}
}
![Page 28: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/28.jpg)
(type) expression
Mammal m = (Mammal) new Hamster(); // upcast
![Page 29: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/29.jpg)
(type) expression
Mammal m = new Hamster(); // implicit upcast
Hamster h = (Hamster) m; // downcastHamster h = m; // illegal
![Page 30: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/30.jpg)
Lamp l = (Lamp) new Duck (); // illegal
![Page 31: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/31.jpg)
Object
Jack
Ted
Kate
Lisa
Mike
Ted t;…t.foo();
Kate k;…k.foo();
void foo()
void foo()
overriding(redefining an
inherited method)
![Page 32: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/32.jpg)
? x;if (…) {
x = new Nick();} else {
x = new Diane();}x.foo();
Nick Diane
Object
void foo()
void foo()
![Page 33: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/33.jpg)
Object x;if (…) {
x = new Nick();} else {
x = new Diane();}x.foo(); // illegal Nick Diane
void foo()
void foo()
Object
![Page 34: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/34.jpg)
interface Philip {void foo();
}
class Diane implements Philip {…}class Nick implements Philip {…}
![Page 35: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/35.jpg)
Philip x;if (…) {
x = new Nick();} else {
x = new Diane();}x.foo(); // legal
Nick Diane
Object
void foo()
void foo()
![Page 36: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/36.jpg)
Philip x = new Ian();
Nick Diane
Object
Ian
void foo()
void foo()
![Page 37: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/37.jpg)
1. Compiler checks compile time type.2. Method invoked depends upon runtime type…3. …except this always invokes version of own class.
![Page 38: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/38.jpg)
void bar() {this.foo();
}Mary
void foo()
Leo
void foo()void bar()
Leo l = new Leo();l.bar();
![Page 39: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/39.jpg)
1. Compiler checks compile time type.2. Method invoked depends upon runtime type…3. …except this always invokes own class’s version…4. …and super always invokes inherited version.
![Page 40: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/40.jpg)
void ack() {super.foo();
}Ryan
void foo()void ack()
Heather
void foo()
Heather h = new Heather();h.ack();
![Page 41: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/41.jpg)
void foo() {super.foo();…
}
Ryan
void foo()void ack()
Heather
void foo()
![Page 42: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/42.jpg)
void foo(Rat r) {r.bar(this);
}
![Page 43: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/43.jpg)
void foo(Rat r) {r.bar(super); //
illegal}
![Page 44: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/44.jpg)
primitive typesbyte 1-byte signed integershort 2-byte signed integerint 4-byte signed integerlong 8-byte signed integer
char 2-byte unsigned integer
boolean true and false
float single-precision floating-pointdouble double-precision floating-point
![Page 45: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/45.jpg)
BigInteger
BigDecimal
(arbitrary-precision integer)
(arbitrary-precision decimal)
![Page 46: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/46.jpg)
primitive types:• variable holds a value• == tests equality • cast produces a new value
reference types:• variable holds a reference• == tests identity • cast appeases compiler
![Page 47: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/47.jpg)
int i = 60;float f = 5.4;
i = (int)f;f = (float)i;
![Page 48: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/48.jpg)
int i = 5;byte b = 3;
b = (byte)i;i = b; // implicit cast
![Page 49: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/49.jpg)
static member(a member which is not a member)
class Ian {static Fran f; // Ian.fstatic void foo() {…} //
Ian.foo()…
}
![Page 50: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/50.jpg)
class Ian {static Fran f; // Ian.fstatic void foo() {…} //
Ian.foo()…
}Ian i = new Ian();i.foo(); i.f = new Fran();
![Page 51: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/51.jpg)
1. Compiler checks compile-time type.2. Method invoked depends upon runtime type…3. …except this always invokes own class’s version…4. …and super always invokes inherited version…5. …and static determined solely by compile-time type.
![Page 52: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/52.jpg)
package name;
package shark;package pig.tiger;
![Page 53: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/53.jpg)
package pig.tiger;
shark.Ant h = new shark.Ant();
![Page 54: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/54.jpg)
package shark;
Ant h = new Ant();pig.tiger.Cow h = new pig.tiger.Cow();
![Page 55: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/55.jpg)
package shark;import pig.tiger.Cow;import pig.tiger.Lemur;
Lemur l = new Lemur();Cow c = new Cow();
![Page 56: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/56.jpg)
java.lang
java.lang.Object
![Page 57: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/57.jpg)
java.lang.StringString s = “hello”;int l = s.length(); // 5s = s.toUpperCase(); // “HELLO”
“Hello, ” + “Ron” “Hello, Ron”5 + “ golden rings” “5 golden rings”
![Page 58: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/58.jpg)
public (everywhere)protected * (same package + subclasses)default (same package)private * (same class)
* applicable only to members
visibility
![Page 59: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/59.jpg)
overloading(same name, different method)
void foo()void foo(Mammal m, Lamp l)byte foo(int a)void foo(Lamp l, Mammal m)
Sean s = new Sean();s.foo(new Lamp(), new Mammal());s.foo(35);
Sean
![Page 60: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/60.jpg)
overloading(same name, different method)
void foo()void foo(Mammal m, Lamp l)byte foo(int a) // conflictvoid foo(Lamp l, Mammal m)float foo(int b) // conflict
Sean s = new Sean();s.foo(35); // ambiguous
Sean
![Page 61: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/61.jpg)
overloading(same name, different method)
void foo()void foo(Mammal m, Lamp l)byte foo(int a)void foo(Lamp l, Mammal m)char foo(Lamp l, Animal m)
Sean s = new Sean();s.foo(new Lamp(), new Hamster());
Sean
![Page 62: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/62.jpg)
overloading(same name, different method)
void foo()void foo(Mammal m, Lamp l)byte foo(int a)void foo(Lamp l, Mammal m)char foo(Lamp l, Animal m)
Sean s = new Sean();s.foo(new Lamp(), (Animal) new Hamster());
Sean
![Page 63: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/63.jpg)
1. Compiler checks compile-time type and picks overload.2. Method invoked depends upon runtime type…3. …except this always invokes own class’s version…4. …and super always invokes inherited version…5. …and static determined solely by compile-time type.
![Page 64: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/64.jpg)
void foo(int a) (overload)void foo() (override)
void foo()Jill
Donald
![Page 65: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/65.jpg)
Jill
void foo(int a) (overload)char foo() (illegal)
Donald
void foo()
![Page 66: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/66.jpg)
Moose(Hamster h, Rat r) {this.r = r;this.h = h;}
Moose() { this.r = new Rat();this.h = new Hamster();}
new Moose(new Hamster(), new Rat())
![Page 67: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/67.jpg)
Moose(Hamster h, Rat r) {this.r = r;this.h = h;
}
Moose() {this(new Hamster(), new Rat());
}
![Page 68: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/68.jpg)
Moose(Hamster h, Rat r) {this.r = r;this.h = h;
}
Moose() {// not what we wantnew Moose(new Hamster(), new Rat());
}
![Page 69: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/69.jpg)
Moose(Hamster h, Rat r) {this.r = r;this.h = h;
}
Moose() {// sensical, but not legal JavaMoose(new Hamster(), new Rat());
}
![Page 70: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/70.jpg)
Moose(Hamster h, Rat r) {this.r = r;this.h = h;
}
Moose() {this(new Hamster(), new Rat());
}
![Page 71: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/71.jpg)
Moose(Hamster h, Rat r) {super(); // invoke constructor of Moose’s parentthis.r = r;this.h = h;}
Moose() {this(new Hamster(), new Rat());}
![Page 72: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/72.jpg)
Object
Jack
Ted
Kate
new Kate()
![Page 73: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/73.jpg)
1. Constructors invoked only via new, this(), or super().
2. Calls to this() and super() only can be first line.3. The first line is always this() or super().4. Default first line is super() with no arguments.
constructor rules
![Page 74: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/74.jpg)
// infinite recursion
Moose(Hamster h, Rat r) {this();
}
Moose() {this(new Hamster(), new Rat());
}
![Page 75: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/75.jpg)
1. Constructors invoked only via new, this(), or super().2. The first line is always this() or super().3. Calls to this() and super() only go in first line.4. Default first line is super() with no arguments. 5. A this() call cannot be recursive.6. To return, always just use return;
constructor rules
![Page 76: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/76.jpg)
Object
Throwable
Exception
RuntimeException
Error
exceptions
![Page 77: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/77.jpg)
Cat c;…c.meow(); // exception if c is
null
![Page 78: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/78.jpg)
Cat c;…try {c.meow(); // exception if c is null} catch (NullPointerException e) {…}
![Page 79: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/79.jpg)
Object
Throwable
Exception
RuntimeException*
Error*
*unchecked
![Page 80: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/80.jpg)
// illegalvoid bar() {
if (…) {throw new Leo(); // checked
exception}
}
![Page 81: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/81.jpg)
// OKvoid bar() throws Leo {
if (…) {throw new Leo(); // checked
exception}
}
![Page 82: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/82.jpg)
// OKvoid bar() {
try {if (…) {
throw new Leo(); // checked exception
}} catch (Leo e) {
…}
}
![Page 83: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/83.jpg)
void bar() throws FooException, AckException {…}
void bar() throws Exception {…}
![Page 84: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/84.jpg)
arrays(fixed-sized, homogenous collection)
Apple ref
Apple ref
Apple ref
Apple ref
Apple[]
Apple object
Apple object
Fuji object
![Page 85: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/85.jpg)
Mammal[] m;m = new Mammal[3];m[2] = new Mammal();m[0] = new Cat();int i =
m.length; // 3
![Page 86: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/86.jpg)
Object
Lisa
Jerry
Natalie
Wyatt
Object[]
Lisa[]
Jerry[]
Natalie[]
Wyatt[]
![Page 87: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/87.jpg)
Cat[] c = new Cat[3];Mammal[] m = c;Object[] o1 = c;Object o2 = c;
![Page 88: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/88.jpg)
Cat[] c = new Cat[3];Mammal[] m = (Mammal []) c;Object[] o1 = (Object [])
c;Object o2 = (Object) c;c = (Cat[]) o2;
![Page 89: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/89.jpg)
Mammal[] m = new Cat[4];m[0] = new Mammal(); //
exception
![Page 90: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/90.jpg)
Mammal[] m = new Cat[4];m[0] = new Cat();Cat mittens = m[0]; // compile
error
![Page 91: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/91.jpg)
Mammal[] m = new Cat[4];m[0] = new Cat();Cat mittens = (Cat) m[0]; //
OK
![Page 92: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/92.jpg)
Object
Lisa
Jerry
Natalie
Wyatt
Object[][]
Lisa[][]
Jerry[][]
Natalie[][]
Wyatt[][]
![Page 93: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/93.jpg)
Apple[] ref
Apple[] ref
Apple[] ref
Apple[] ref
Apple[][]
Apple[] object
Fuji[] object
![Page 94: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/94.jpg)
Cat[][] c = new Cat[3][];c[1] = new Cat[5];c[1][0] = new Cat();
![Page 95: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/95.jpg)
Cat[][] c = new Cat[3][];c[1] = new Cat[5];(c[1])[0] = new Cat();
![Page 96: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/96.jpg)
Object
Lisa
Jerry
Natalie
Wyatt
Object[][][]
Lisa[][][]
Jerry[][][]
Natalie[][][]
Wyatt[][][]
![Page 97: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/97.jpg)
Apple[][] ref
Apple[][] ref
Apple[][] ref
Apple[][] ref
Apple[][][]
Apple[][] object
Fuji[][] object
![Page 98: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/98.jpg)
Object
Object[][][]Object[][]Object[] Object[][][][]
![Page 99: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/99.jpg)
int
int
int
int
int[]
![Page 100: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/100.jpg)
int[] ref
int[] ref
int[] ref
int[] ref
int[][]
int[] object
int[] object
![Page 101: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/101.jpg)
Object
char[][][]byte[][]int[] float[]
![Page 102: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/102.jpg)
public class HelloWorld {public static void main(String[] args)
{System.out.println(“Hello,
world!”);}
}
Hello, world!
![Page 103: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/103.jpg)
• inner classes• generics• abstract classes• wrapper classes• enumerations• annotations• assert• final
![Page 104: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/104.jpg)
![Page 105: Http://proglit.com/. the java language BY SA introduced in 1995 by Sun Microsystems.](https://reader030.fdocuments.us/reader030/viewer/2022032516/56649c745503460f94927f83/html5/thumbnails/105.jpg)
http://proglit.com/