forked from Hesenius/SafariJava17CertStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package casting; | ||
|
||
public class Ex { | ||
public static void main(String[] args) { | ||
// Base b = new Sub(); | ||
Base b = new Base(); | ||
// compiler allows methods know to be valid based on | ||
// COMPILE-TIME type of the reference... | ||
// but BEHAVIOR is based on RUNTIME type of the object | ||
b.doBaseStuff(); | ||
// ((Sub)b).doOtherStuff(); | ||
// impossible casts are rejected by the compiler | ||
// ((String)b).length(); | ||
|
||
((Runnable) b).run(); | ||
} | ||
} | ||
|
||
/*final*/ class Base { | ||
void doBaseStuff() { System.out.println("doBaseStuff"); } | ||
} | ||
|
||
//class Sub extends Base implements Runnable { | ||
// void doBaseStuff() { System.out.println("doSubStuff"); } | ||
// void doOtherStuff() { System.out.println("doOtherStuff"); } | ||
// | ||
// @Override | ||
// public void run() { | ||
// | ||
// } | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package casting; | ||
|
||
public class PatternOne { | ||
public static void main(String[] args) { | ||
Object obj = "Hello"; | ||
boolean answered = false; | ||
if (obj instanceof String) { | ||
String str = (String)obj; | ||
if (str.length() > 3) { | ||
System.out.println(str); | ||
answered = true; | ||
} | ||
} | ||
if (!answered) System.out.println("Nope"); | ||
|
||
System.out.println("------------"); | ||
|
||
// this is still a boolean expression!!! | ||
// Look into this Tuesday :) | ||
if (obj instanceof String str) { | ||
if (str.length() > 3) { | ||
System.out.println(str); | ||
answered = true; | ||
} | ||
} | ||
if (!answered) System.out.println("Nope"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package intern; | ||
|
||
public class UseInteger { | ||
public static void main(String[] args) { | ||
// autoboxed Integer in range of byte (-128 +127) are pooled | ||
// Integer i1 = 99; | ||
// Integer i2 = 99; | ||
Integer i1 = Integer.valueOf(99); // this is what autoboxing creates | ||
// Integer i1 = new Integer(99); // this is deprecated DON'T use constructor | ||
Integer i2 = 99; | ||
Integer i3 = 100; | ||
System.out.println(i1 == i2); | ||
System.out.println(i1 == i3); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package unqualified; | ||
|
||
public class Identifiers { | ||
public static void main(String[] args) { | ||
// Thing.showIt(); | ||
} | ||
} | ||
class PThing { | ||
static int x = 99; | ||
} | ||
class Thing extends PThing { | ||
int x = 99; | ||
// static int x = 99; | ||
|
||
void showIt() { | ||
// int x = 98; | ||
// Thing t = new Thing(); | ||
System.out.println("x is " + x); | ||
} | ||
} | ||
|
||
/* | ||
Unqualified resolution: | ||
- local item | ||
- (if no local) | ||
- for each level of class hierarchy until successful | ||
-- if instance scope (i.e. there is a "this" available) | ||
-- try implied this. prefix | ||
-- try THIS classname implied prefix | ||
*/ |