Lernen am Samstag
This commit is contained in:
@@ -2,6 +2,12 @@ package org.eidecker.oca8lernen.chapter01;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @author seidecke.
|
||||
*/
|
||||
@@ -21,9 +27,14 @@ class TestClass {
|
||||
for (B item : B.values()) {
|
||||
System.out.println(item);
|
||||
}
|
||||
|
||||
int i = 1;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private enum Two {
|
||||
|
||||
EINS, ZWEI;
|
||||
|
||||
@@ -4,6 +4,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.AccessDeniedException;
|
||||
import java.util.List;
|
||||
import java.util.zip.DataFormatException;
|
||||
|
||||
/**
|
||||
* @author seidecke.
|
||||
*/
|
||||
@@ -30,17 +35,37 @@ public class CastExceptions {
|
||||
// Compilefehler, da Versuch des Casts außerhalb der Typenhierarchie
|
||||
// UnterCast unter2 = (UnterCast) außerhalb;
|
||||
|
||||
try {
|
||||
UnterCast unterCast = new UnterCast();
|
||||
unter.method();
|
||||
} catch (RuntimeException e) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class OberCast {
|
||||
|
||||
public void method() throws IOException{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class UnterCast extends OberCast {
|
||||
|
||||
@Override
|
||||
public void method() {
|
||||
System.out.println("method in Untercast");
|
||||
}
|
||||
}
|
||||
|
||||
class Außerhalb {
|
||||
|
||||
}
|
||||
|
||||
class MyCheckedException extends Exception {
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package org.eidecker.oca8lernen.general;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ConditionalInitialisation {
|
||||
|
||||
final String s2;
|
||||
final String s3;
|
||||
// final String s4;
|
||||
|
||||
public ConditionalInitialisation() {
|
||||
boolean b = true;
|
||||
if (b) {
|
||||
s2 = "Hallo";
|
||||
} else {
|
||||
s2 = "Welt";
|
||||
}
|
||||
|
||||
// Keine doppelte Zuweisung, da Compiler auflösen kann
|
||||
if (true) s3= "Hallo";
|
||||
if (false) s3 = "Welt";
|
||||
|
||||
String s6;
|
||||
final boolean c = false;
|
||||
if (c) s6 = "Hallo";
|
||||
if (!c) s6 = "Welt";
|
||||
System.out.println(s6);
|
||||
|
||||
// Geht nicht, damit sowohl Verletzung final-Zuweisung als auch Nicht-Initialisierung
|
||||
// if (b) s4 = "Hallo";
|
||||
// if (!b) s4 = "Welt";
|
||||
|
||||
try {
|
||||
// s4 = fillMe();
|
||||
} catch (Exception e) {
|
||||
// s4 = fillMeSilently();
|
||||
}
|
||||
|
||||
String s5;
|
||||
try {
|
||||
s5 = fillMe();
|
||||
} catch (Exception e) {
|
||||
s5 = fillMeSilently();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String fillMe() throws Exception {
|
||||
return "Hallo";
|
||||
}
|
||||
|
||||
private String fillMeSilently() throws RuntimeException {
|
||||
return "Welt";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConditionalInitialisation() {
|
||||
|
||||
boolean b = true;
|
||||
|
||||
final String s;
|
||||
if (b) {
|
||||
s = "Hallo";
|
||||
} else {
|
||||
s = "Welt";
|
||||
}
|
||||
|
||||
System.out.println(s);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package org.eidecker.oca8lernen.general;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.AccessDeniedException;
|
||||
import java.util.zip.DataFormatException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
@@ -11,28 +13,120 @@ public class DoubleImplementsClash {
|
||||
|
||||
@Test
|
||||
public void testSomeTHings() {
|
||||
|
||||
InterfaceImplementer implementer = new InterfaceImplementer();
|
||||
// implementer.bothStatic() // Nicht vorhanden, nur im Interface
|
||||
implementer.bothDefault();
|
||||
implementer.singleDefault();
|
||||
implementer.abstractClassAndInterfaceDefault();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class WorkingClass extends AbstractClass implements AnInterface {
|
||||
class InterfaceImplementer extends AbstractClass implements SuperInterface1, SuperInterface2{
|
||||
|
||||
|
||||
public void methode (int ...name ) {
|
||||
// methode2(1,3,4); // Geht nicht, muss Array sein
|
||||
}
|
||||
|
||||
public void methode2 (int[] name) {
|
||||
methode(1, 2, 3);
|
||||
int [] ints = {1,2,3};
|
||||
methode(ints);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSomething() {
|
||||
SuperInterface1.bothStatic();
|
||||
this.bothAbstract();
|
||||
SuperInterface1.super.singleDefault();
|
||||
this.singleDefault();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void method1() throws AccessDeniedException {
|
||||
public void bothAbstract() {
|
||||
|
||||
}
|
||||
|
||||
public void bothDefault() {
|
||||
SuperInterface1.super.bothDefault();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void oneStaticOneAbstract() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
abstract class AbstractClass implements AnInterface {
|
||||
class AnotherImplementer implements SuperInterface1 {
|
||||
|
||||
@Override
|
||||
public abstract void method1() throws AccessDeniedException;
|
||||
}
|
||||
public void bothAbstract() {
|
||||
|
||||
interface AnInterface {
|
||||
default void method1() throws IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void oneStaticOneAbstract() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
interface SuperInterface1 {
|
||||
static void bothStatic() {
|
||||
}
|
||||
|
||||
default void bothDefault() {
|
||||
}
|
||||
|
||||
default void singleDefault() {
|
||||
|
||||
}
|
||||
|
||||
void bothAbstract();
|
||||
|
||||
void oneStaticOneAbstract();
|
||||
}
|
||||
|
||||
interface SuperInterface2 {
|
||||
|
||||
static void bothStatic() {
|
||||
}
|
||||
|
||||
default void bothDefault() {
|
||||
}
|
||||
|
||||
void bothAbstract();
|
||||
|
||||
static void oneStaticOneAbstract() {
|
||||
|
||||
}
|
||||
|
||||
default void abstractClassAndInterfaceDefault() {
|
||||
System.out.println("SuperInterface2 abstractClassAndInterfaceDefault");
|
||||
"Halli".length();
|
||||
}
|
||||
}
|
||||
|
||||
abstract class AbstractClass {
|
||||
|
||||
|
||||
public void abstractClassAndInterfaceDefault() {
|
||||
System.out.println("AbstractClass abstractClassAndInterfaceDefault");
|
||||
}
|
||||
|
||||
public void throwsException() throws IOException {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ErbendeKlasse extends AbstractClass {
|
||||
|
||||
@Override
|
||||
public void throwsException() throws AccessDeniedException {
|
||||
new ErbendeKlasse();
|
||||
}
|
||||
}
|
||||
|
||||
108
src/org/eidecker/oca8lernen/general/Primitive.java
Normal file
108
src/org/eidecker/oca8lernen/general/Primitive.java
Normal file
@@ -0,0 +1,108 @@
|
||||
package org.eidecker.oca8lernen.general;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class Primitive {
|
||||
|
||||
|
||||
@Test
|
||||
public void testSomething() {
|
||||
|
||||
// Ein paar Zuweisungen
|
||||
|
||||
|
||||
|
||||
|
||||
double d = 3.9;
|
||||
int i = (int) d;
|
||||
|
||||
System.out.println(i);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnderscores() {
|
||||
int iDec = 1_1__1;
|
||||
assertEquals(111, iDec);
|
||||
|
||||
int iBinary = 0b10__0_0101_0000;
|
||||
int iHex = 0xf2_d8;
|
||||
int iOctal = 0111;
|
||||
|
||||
int iOctal2 = new Integer(011);
|
||||
int iOctal3 = new Integer("011"); // Das wird wohl doch dezimal behandelt
|
||||
int iOctal4 = new Integer("0b11");
|
||||
assertFalse(iOctal2 == iOctal3);
|
||||
|
||||
assertFalse(new Integer("1").equals((long)1));
|
||||
assertFalse(new Long(1).equals(1));
|
||||
|
||||
|
||||
assertNotEquals(111, iOctal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZuweisungLiterale() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImplicitCast() {
|
||||
float f3;
|
||||
float f = 1.0f;
|
||||
float f2 = 1L;
|
||||
|
||||
f3 = f + f2;
|
||||
|
||||
byte b1 = (byte)1f;
|
||||
|
||||
char c1 = (char) -1;
|
||||
System.out.println("c1: " + c1);
|
||||
|
||||
short s1 = Short.MAX_VALUE;
|
||||
short s2 = (short) (s1 + 1);
|
||||
System.out.println(s2);
|
||||
|
||||
short s3 = (short)189898989898989898l;
|
||||
System.out.println("s3: " + s3);
|
||||
|
||||
long l1 = Long.MAX_VALUE;
|
||||
System.out.println("L1: " + (l1 + 12));
|
||||
|
||||
char c = 1, d = 3;
|
||||
System.out.println(c + d);
|
||||
|
||||
int i5 = 1212;
|
||||
long l4;
|
||||
l4 = i5;
|
||||
|
||||
char c5;
|
||||
byte b9 = 2;
|
||||
|
||||
float f9 = Long.MAX_VALUE;
|
||||
System.out.println("f9: " + f9);
|
||||
|
||||
char c10 = (char)123.333d;
|
||||
System.out.println("c10: " + c10);
|
||||
|
||||
Integer I1 = 0b0101;
|
||||
// Float F1 = (int)12; Float, Double können keine Ganzzahl-Literale zugewiesen werden
|
||||
Double D1 = (double)12;
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExplicitCast() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
35
src/org/eidecker/oca8lernen/general/Spielen.java
Normal file
35
src/org/eidecker/oca8lernen/general/Spielen.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package org.eidecker.oca8lernen.general;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class Spielen {
|
||||
|
||||
public Spielen() {
|
||||
|
||||
}
|
||||
|
||||
public Spielen(int n) {
|
||||
this();
|
||||
|
||||
int i = new Integer("2");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void spielen() {
|
||||
int i = 1;
|
||||
printInt(i++ + i);
|
||||
System.out.println(i);
|
||||
|
||||
int k = 1;
|
||||
printInt(k + k++);
|
||||
|
||||
int j = 1;
|
||||
printInt(j+= 3);
|
||||
}
|
||||
|
||||
|
||||
private void printInt(int i) {
|
||||
System.out.println(i);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,11 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SwitchSyntax {
|
||||
|
||||
final int k = 0;
|
||||
|
||||
public SwitchSyntax() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void switchSyntax() {
|
||||
|
||||
@@ -48,6 +53,22 @@ public class SwitchSyntax {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFinal() {
|
||||
|
||||
final int i = 1;
|
||||
final int j;
|
||||
j = 2;
|
||||
|
||||
switch(getSwitchBEdingung()) {
|
||||
case i:
|
||||
System.out.println("i");
|
||||
case k:
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Integer getSwitchBEdingung() {
|
||||
return new Integer(1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user