Skip to content

Commit

Permalink
Day 2 Break 1
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonHGR committed May 9, 2023
1 parent 2341099 commit 6cf3c68
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 3 deletions.
34 changes: 34 additions & 0 deletions Questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,37 @@ C) if (obj instanceof String str if str.length() > 3) System.out.println(str)
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);

Q) Given:
String s = "Hello";
switch (s) {
case "Hello" ->
System.out.print("Bonjour "); // line n1
System.out.print("Guten Tag ");
default -> System.out.print("Bye");
}

Which is true?
A) The code prints: Bonjour Guten Tag Bye
B) The code prints: Bonjour Guten Tag
C) If line n1 is removed the code prints: Guten Tag Bye
D) If line n1 is removed the code prints: Guten Tag
E) The code fails to compile

Q)
int x = 1;
// line n1
switch(x) {
case 1: LocalDate.of(2023, x, 1);
default: LocalDate.of(2022, 12, 1);
};

Which are true?
A) The code compiles
B) The code throws an exception at runtime
C) The code compiles if line n1 is replaced with
var d =
D) The code compiles if the word "yield" is inserted
after the colon in both the case and default
E) The code compiles if the changes of both C and D
are applied
94 changes: 94 additions & 0 deletions src/main/java/arrowswitch/Examples.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package arrowswitch;

import java.sql.SQLOutput;
import java.time.LocalDate;
import java.util.List;

public class Examples {
public static void main(String[] args) {
String s = "Hello";
switch (s) {
case "Hello":
case "Goodbye":
System.out.println("hello");
System.out.println("really!");
break;
default:
System.out.println("hmm");
break;
}
System.out.println("---------------");
switch (s) {
// case "Hello", "Goodbye": // comma works with colon too now :)
case "Hello", "Goodbye" -> {
System.out.println("hello");
System.out.println("really!");
if (Math.random() > 0.5) break;
System.out.println("Still here");
}
default -> System.out.println("hmm");
}

System.out.println("----------------");
switch (s) {
case "Hello" -> System.out.println("really!");
default -> System.out.println("hmm");
}

}
}

class MoreExamples {
public static void main(String[] args) {

int x = 1;
// line n1
// use a switch in a place where an expression is needed...
// it becomes an expression!!!
// it MUST cover all input values
// right side of -> must now be either single *expression*
// or we yield (NOT RETURN!!!!)
// CAN do this with colon form, but must use yield always.
// Take care... Colon form STILL falls through!
var y = switch(x) {
case 1 -> {
int year = 2023;
// if (Math.random() > 0.5) return; // NOPE!!!!
yield LocalDate.of(year, x, 1);
}
// expressions must not be void (i.e. must not be statements)
// default -> System.out.println(LocalDate.of(2022, 12, 1));
default -> LocalDate.of(2022, 12, 1);
};
System.out.println("y is " + y);

// "Statement-expression" expression that is known to have potential
// side effects
// boolean expression! java allows us to ignore result of function calls
// so we can treat it as a statement.
// statement expressions generally are any expression with side effects
// function calls
// constructor calls
// increment/decrement operators
// assignment expressions
// can use these in for loops
// boolean f = false;
// for (System.out.println("Hello"), System.out.println("goodbye");
// f;
// System.out.println("Loop increment"))
// ;

// List<String> ls = null;
// ls.add("");
int a = 3, b = 2;
// a + b; // NOT legal in isolation


// line n1
var r2 = switch(x) {
case 1: yield LocalDate.of(2023, x, 1);
default: yield LocalDate.of(2022, 12, 1);
};
}

}
25 changes: 25 additions & 0 deletions src/main/java/casting/Pattern2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package casting;

class Pair<E> {
private E left;
private E right;

public Pair(E left, E right) {
this.left = left;
this.right = right;
}

public boolean equals(Object other) {
// if (other instanceof Pair otherP) {
// return this.left == otherP.left &&
// this.right == otherP.right;
// }
// else return false;
return other instanceof Pair otherP &&
this.left == otherP.left &&
this.right == otherP.right;
}
}

public class Pattern2 {
}
14 changes: 11 additions & 3 deletions src/main/java/casting/PatternOne.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package casting;

import java.util.Collection;
import java.util.List;

public class PatternOne {
public static void main(String[] args) {
Object obj = "Hello";
Expand All @@ -17,12 +20,17 @@ public static void main(String[] args) {

// this is still a boolean expression!!!
// Look into this Tuesday :)
if (obj instanceof String str) {
if (str.length() > 3) {
// if (obj instanceof String str) {
// if (str.length() > 3) {
if (obj instanceof String str && str.length() > 3) {
System.out.println(str);
answered = true;
}
}
if (!answered) System.out.println("Nope");

// Object obj2 = List.of("X", "Y");
Collection<String> obj2 = List.of("X", "Y");
if (obj2 instanceof List<String> ls)
System.out.println("yes");
}
}
14 changes: 14 additions & 0 deletions src/main/java/casting/Q.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package casting;

public class Q {
public static void main(String[] args) {
Object obj = "Hel";
System.out.println(obj instanceof String str && str.length() > 3 ?
str : "Nope");

if (!(obj instanceof String str) || str.length() <= 3)
System.out.println("Nope");
else
System.out.println(str);
}
}

0 comments on commit 6cf3c68

Please sign in to comment.