From 2341099234dcc6c62e6b7adacfe4e98c41066f68 Mon Sep 17 00:00:00 2001 From: simon Date: Mon, 8 May 2023 13:02:49 -0600 Subject: [PATCH] Day 1 final --- Questions.txt | 61 ++++++++++++++++++++++ src/main/java/casting/Ex.java | 31 +++++++++++ src/main/java/casting/PatternOne.java | 28 ++++++++++ src/main/java/intern/UseInteger.java | 15 ++++++ src/main/java/unqualified/Identifiers.java | 31 +++++++++++ 5 files changed, 166 insertions(+) create mode 100644 src/main/java/casting/Ex.java create mode 100644 src/main/java/casting/PatternOne.java create mode 100644 src/main/java/intern/UseInteger.java create mode 100644 src/main/java/unqualified/Identifiers.java diff --git a/Questions.txt b/Questions.txt index 235d035..76d5a2a 100644 --- a/Questions.txt +++ b/Questions.txt @@ -209,3 +209,64 @@ B) s2 == s3 C) s1 == s3 D) X.h == s1 E) None of the above + +Q) Given +class Thing { + int x = 99; + + static void showIt() { + int x = 98; + System.out.println("x is " + x); + } +} + +What is the effect of: + +Thing.showIt(); + +A) Compilation fails +B) Exception +C) 99 +D) 98 + +Q) Given: + class Base { + void doBaseStuff() { System.out.println("doBaseStuff"); } + } +and: + class Sub extends Base { + void doBaseStuff() { System.out.println("doSubStuff"); } + void doOtherStuff() { System.out.println("doOtherStuff"); } + } +and: + Base b = new Sub(); + ((Runnable) b).run(); + +What is the result? +A) doSubStuff +B) doOtherStuff +C) doBaseStuff +D) Exception at runtime +E) Compilation failure + +Q) Given: + 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"); + +Which replace the if clause to produce the same output? +A) System.out.println(obj instanceof String str && str.length() > 3 ? str : "Nope"); +B) if (obj instanceof String str when str.length() > 3) System.out.println(str); + else System.out.println("Nope"); +C) if (obj instanceof String str if str.length() > 3) System.out.println(str); + else System.out.println("Nope"); +D) System.out.println(obj instanceof String.class str && str.length() > 3 ? str : "Nope"); +E) if (!(obj instanceof String str) || str.length() <= 3) System.out.println("Nope"); + else System.out.println(str); diff --git a/src/main/java/casting/Ex.java b/src/main/java/casting/Ex.java new file mode 100644 index 0000000..2301ddf --- /dev/null +++ b/src/main/java/casting/Ex.java @@ -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() { +// +// } +//} diff --git a/src/main/java/casting/PatternOne.java b/src/main/java/casting/PatternOne.java new file mode 100644 index 0000000..ff181b7 --- /dev/null +++ b/src/main/java/casting/PatternOne.java @@ -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"); + } +} diff --git a/src/main/java/intern/UseInteger.java b/src/main/java/intern/UseInteger.java new file mode 100644 index 0000000..b362eff --- /dev/null +++ b/src/main/java/intern/UseInteger.java @@ -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); + } +} diff --git a/src/main/java/unqualified/Identifiers.java b/src/main/java/unqualified/Identifiers.java new file mode 100644 index 0000000..e52f1eb --- /dev/null +++ b/src/main/java/unqualified/Identifiers.java @@ -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 + + */