Presentation is loading. Please wait.

Presentation is loading. Please wait.

the java language BY SA introduced in 1995 by Sun Microsystems.

Similar presentations


Presentation on theme: "the java language BY SA introduced in 1995 by Sun Microsystems."— Presentation transcript:

1 http://proglit.com/

2 the java language

3 BY SA

4

5 introduced in 1995 by Sun Microsystems

6 imperative, object-oriented C-style syntax statically-typed (mostly) no pointers JVM (Java Virtual Machine) garbage collection exceptions

7 SE (standard edition) EE (enterprise edition) ME (micro edition)

8 Java 6(2006) Java 7(2010?)

9 class (data type definition) field (data member) method (function member) object / instance (piece of data)

10 class Moose { Rat r; Hamster h; void foo() {…} }

11 encapsulation (methods act as “interface” to object’s fields)

12 object.field object.method(args) apple.banana apple.banana.orange nadine.ed() nadine.ed().laura nadine.ed().laura.dale()

13 object.field object.method(args) apple.banana (apple.banana).orange nadine.ed() (nadine.ed()).laura ((nadine.ed()).laura).dale ()

14 type name; new type(args) Moose m; // null m = new Moose(); Moose m = new Moose();

15 class Moose { Rat r; Hamster h; Moose() {…} // constructor void foo() {…} }

16 Moose() { this.r = new Rat(); this.h = new Hamster(); }

17 Moose(Hamster h, Rat r) { this.r = r; this.h = h; } new Moose(new Hamster(), new Rat())

18 inheritance AC DE F G B DE CB A CB A

19 Ted Kate Milton illegal

20 Object Jack Samantha Brad Ted Kate LisaMilton Oliver

21 class Terry {…} // extends Object class John extends Ben {…}

22 Jack Ted Kate Milton illegal

23 is-a vs. has-a Think before using inheritance.

24 Object Jack Ted Kate Lisa Mike Hugh Olivia Uri Amber (probably) bad

25 Hamster h = new Hamster(); Mammal m = h; h.runOnWheel(); // OK m.runOnWheel(); // illegal

26 void bar(Mammal m) {…} x.bar(new Mammal()) x.bar(new Hamster())

27 Mammal foo() { if (…) { return new Mammal(); } else { return new Hamster(); }

28 (type) expression Mammal m = (Mammal) new Hamster(); // upcast

29 (type) expression Mammal m = new Hamster(); // implicit upcast Hamster h = (Hamster) m; // downcast Hamster h = m; // illegal

30 Lamp l = (Lamp) new Duck (); // illegal

31 Object Jack Ted Kate Lisa Mike Ted t; … t.foo(); Kate k; … k.foo(); void foo() overriding (redefining an inherited method)

32 Nick Diane Object void foo()

33 Nick Diane void foo() Object

34 interface Philip { void foo(); } class Diane implements Philip {…} class Nick implements Philip {…}

35 Philip x; if (…) { x = new Nick(); } else { x = new Diane(); } x.foo(); // legal Nick Diane Object void foo()

36 Philip x = new Ian(); Nick Diane Object Ian void foo()

37 1.Compiler checks compile time type. 2.Method invoked depends upon runtime type… 3.…except this always invokes version of own class.

38 void bar() { this.foo(); } Mary void foo() Leo void foo() void bar() Leo l = new Leo(); l.bar();

39 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.

40 void ack() { super.foo(); } Ryan void foo() void ack() Heather void foo() Heather h = new Heather(); h.ack();

41 void foo() { super.foo(); … } Ryan void foo() void ack() Heather void foo()

42 void foo(Rat r) { r.bar(this); }

43 void foo(Rat r) { r.bar(super); // illegal }

44 primitive types byte 1-byte signed integer short 2-byte signed integer int 4-byte signed integer long 8-byte signed integer char 2-byte unsigned integer boolean true and false float single-precision floating-point double double-precision floating-point

45 BigInteger BigDecimal (arbitrary-precision integer) (arbitrary-precision decimal)

46 primitive types: variable holds a value == tests equality cast produces a new value reference types: variable holds a reference == tests identity cast appeases compiler

47 int i = 60; float f = 5.4; i = (int)f; f = (float)i;

48 int i = 5; byte b = 3; b = (byte)i; i = b; // implicit cast

49 static member (a member which is not a member) class Ian { static Fran f; // Ian.f static void foo() {…} // Ian.foo() … }

50 class Ian { static Fran f; // Ian.f static void foo() {…} // Ian.foo() … } Ian i = new Ian(); i.foo(); i.f = new Fran();

51 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.

52 package name; package shark; package pig.tiger;

53 shark.Ant h = new shark.Ant();

54 package shark; Ant h = new Ant(); pig.tiger.Cow h = new pig.tiger.Cow();

55 package shark; import pig.tiger.Cow; import pig.tiger.Lemur; Lemur l = new Lemur(); Cow c = new Cow();

56 java.lang java.lang.Object

57 java.lang.String String s = “hello”; int l = s.length(); // 5 s = s.toUpperCase(); // “HELLO” “Hello, ” + “Ron”  “Hello, Ron” 5 + “ golden rings”  “5 golden rings”

58 public (everywhere) protected * (same package + subclasses) default (same package) private * (same class) * applicable only to members visibility

59 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

60 overloading (same name, different method) void foo() void foo(Mammal m, Lamp l) byte foo(int a) // conflict void foo(Lamp l, Mammal m) float foo(int b) // conflict Sean s = new Sean(); s.foo(35); // ambiguous Sean

61 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

62 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

63

64 void foo(int a) (overload) void foo() (override) void foo() Jill Donald

65 Jill void foo(int a) (overload) char foo() (illegal) Donald void foo()

66 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())

67 Moose(Hamster h, Rat r) { this.r = r; this.h = h; } Moose() { this(new Hamster(), new Rat()); }

68 Moose(Hamster h, Rat r) { this.r = r; this.h = h; } Moose() { // not what we want new Moose(new Hamster(), new Rat()); }

69 Moose(Hamster h, Rat r) { this.r = r; this.h = h; } Moose() { // sensical, but not legal Java Moose(new Hamster(), new Rat()); }

70 Moose(Hamster h, Rat r) { this.r = r; this.h = h; } Moose() { this(new Hamster(), new Rat()); }

71 Moose(Hamster h, Rat r) { super(); // invoke constructor of Moose’s parent this.r = r; this.h = h; } Moose() { this(new Hamster(), new Rat()); }

72 Object Jack Ted Kate new Kate()

73 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

74 // infinite recursion Moose(Hamster h, Rat r) { this(); } Moose() { this(new Hamster(), new Rat()); }

75 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

76 Object Throwable Exception RuntimeException Error exceptions

77 Cat c; … c.meow(); // exception if c is null

78 Cat c; … try { c.meow(); // exception if c is null } catch (NullPointerException e) { … }

79 Object Throwable Exception RuntimeException* Error* *unchecked

80 // illegal void bar() { if (…) { throw new Leo(); // checked exception }

81 // OK void bar() throws Leo { if (…) { throw new Leo(); // checked exception }

82 // OK void bar() { try { if (…) { throw new Leo(); // checked exception } } catch (Leo e) { … }

83 void bar() throws FooException, AckException {…} void bar() throws Exception {…}

84 arrays (fixed-sized, homogenous collection) Apple ref Apple[] Apple object Fuji object

85 Mammal[] m; m = new Mammal[3]; m[2] = new Mammal(); m[0] = new Cat(); int i = m.length; // 3

86 Object Lisa Jerry Natalie Wyatt Object[] Lisa[] Jerry[] Natalie[] Wyatt[]

87 Cat[] c = new Cat[3]; Mammal[] m = c; Object[] o1 = c; Object o2 = c;

88

89 Mammal[] m = new Cat[4]; m[0] = new Mammal(); // exception

90 Mammal[] m = new Cat[4]; m[0] = new Cat(); Cat mittens = m[0]; // compile error

91 Mammal[] m = new Cat[4]; m[0] = new Cat(); Cat mittens = (Cat) m[0]; // OK

92 Object Lisa Jerry Natalie Wyatt Object[][] Lisa[][] Jerry[][] Natalie[][] Wyatt[][]

93 Apple[] ref Apple[][] Apple[] object Fuji[] object

94 Cat[][] c = new Cat[3][]; c[1] = new Cat[5]; c[1][0] = new Cat();

95 Cat[][] c = new Cat[3][]; c[1] = new Cat[5]; (c[1])[0] = new Cat();

96 Object Lisa Jerry Natalie Wyatt Object[][][] Lisa[][][] Jerry[][][] Natalie[][][] Wyatt[][][]

97 Apple[][] ref Apple[][][] Apple[][] object Fuji[][] object

98 Object Object[][][]Object[][] Object[] Object[][][][]

99 int int[]

100 int[] ref int[][] int[] object

101 Object char[][][]byte[][]int[]float[]

102 public class HelloWorld { public static void main(String[] args) { System.out.println(“Hello, world!”); } Hello, world!

103 inner classes generics abstract classes wrapper classes enumerations annotations assert final

104

105 http://proglit.com/


Download ppt "the java language BY SA introduced in 1995 by Sun Microsystems."

Similar presentations


Ads by Google