Skip to content

Commit

Permalink
Day 1 final
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonHGR committed May 8, 2023
1 parent fda0ec2 commit 2341099
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
31 changes: 31 additions & 0 deletions src/main/java/casting/Ex.java
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() {
//
// }
//}
28 changes: 28 additions & 0 deletions src/main/java/casting/PatternOne.java
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");
}
}
15 changes: 15 additions & 0 deletions src/main/java/intern/UseInteger.java
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);
}
}
31 changes: 31 additions & 0 deletions src/main/java/unqualified/Identifiers.java
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
*/

0 comments on commit 2341099

Please sign in to comment.