Notizen aus Lernsessions
This commit is contained in:
7
.gitignore
vendored
7
.gitignore
vendored
@@ -70,3 +70,10 @@ crashlytics.properties
|
|||||||
crashlytics-build.properties
|
crashlytics-build.properties
|
||||||
fabric.properties
|
fabric.properties
|
||||||
|
|
||||||
|
/.idea/$CACHE_FILE$
|
||||||
|
/.idea/$PRODUCT_WORKSPACE_FILE$
|
||||||
|
/.idea/encodings.xml
|
||||||
|
/.idea/misc.xml
|
||||||
|
/.idea/modules.xml
|
||||||
|
/.idea/oca8-lernen.iml
|
||||||
|
/.idea/vcs.xml
|
||||||
|
|||||||
16
src/org/eidecker/oca8lernen/general/Deklaration.java
Normal file
16
src/org/eidecker/oca8lernen/general/Deklaration.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package org.eidecker.oca8lernen.general;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class Deklaration {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void zuweisung() {
|
||||||
|
int a , b, c = 1;
|
||||||
|
// int d = e = 1; // Nicht erlaubt
|
||||||
|
|
||||||
|
System.out.println(c);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
52
src/org/eidecker/oca8lernen/general/FinallyTest.java
Normal file
52
src/org/eidecker/oca8lernen/general/FinallyTest.java
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
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.assertThrows;
|
||||||
|
|
||||||
|
public class FinallyTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFinally() {
|
||||||
|
int a = divZero(5);
|
||||||
|
assertEquals(5, a);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private int divZero(int a) {
|
||||||
|
try {
|
||||||
|
return a/0;
|
||||||
|
} catch (ArithmeticException e) {
|
||||||
|
return sideEffect();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Exception vom Typ Exception gefangen");
|
||||||
|
} finally {
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void exceptionBloecke() {
|
||||||
|
|
||||||
|
assertThrows(Exception.class, () -> {
|
||||||
|
try {
|
||||||
|
int a = 1/0;
|
||||||
|
} catch (ArithmeticException e) {
|
||||||
|
System.out.println("ArithmeticException gefangen");
|
||||||
|
throw new Exception();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Exception vom Typ Exception gefangen");
|
||||||
|
} finally {
|
||||||
|
System.out.println("Finally");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private int sideEffect() {
|
||||||
|
System.out.println("Nur ein Seiteneffekt");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
41
src/org/eidecker/oca8lernen/general/InterfaceWithFields.java
Normal file
41
src/org/eidecker/oca8lernen/general/InterfaceWithFields.java
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package org.eidecker.oca8lernen.general;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class InterfaceWithFields implements T1, T2 {
|
||||||
|
|
||||||
|
public int getT1Value() {
|
||||||
|
return VALUE_T1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getValue() {
|
||||||
|
return T2.VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface T1 {
|
||||||
|
int VALUE = 99;
|
||||||
|
int VALUE_T1 = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface T2 {
|
||||||
|
int VALUE = 98;
|
||||||
|
int VALUE_T2 = 2;
|
||||||
|
|
||||||
|
default int getValue() {
|
||||||
|
return VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Tester {
|
||||||
|
|
||||||
|
public void interfaceTest() {
|
||||||
|
InterfaceWithFields interfaceWithFields = new InterfaceWithFields();
|
||||||
|
assertEquals(99, ((T1)interfaceWithFields).VALUE);
|
||||||
|
assertEquals(98, ((T2)interfaceWithFields).VALUE);
|
||||||
|
|
||||||
|
assertEquals(1, interfaceWithFields.VALUE_T1);
|
||||||
|
assertEquals(2, interfaceWithFields.VALUE_T2);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package org.eidecker.oca8lernen.general;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
|
||||||
|
public class PrimitiveUndWrapperTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void wrapperEquals() {
|
||||||
|
|
||||||
|
Integer aIntegerType = 1;
|
||||||
|
Long aLongType = 1L;
|
||||||
|
|
||||||
|
Long anotherLongType = 0b1L;
|
||||||
|
|
||||||
|
int aInt = 1;
|
||||||
|
long aLong = 1L;
|
||||||
|
|
||||||
|
assertFalse(aIntegerType.equals(aLongType));
|
||||||
|
assertTrue(aInt == aLong);
|
||||||
|
|
||||||
|
|
||||||
|
assertTrue(aIntegerType == aLong);
|
||||||
|
assertTrue(aIntegerType == aInt);
|
||||||
|
assertTrue(aLongType == aInt);
|
||||||
|
assertTrue(aLongType == aLong);
|
||||||
|
|
||||||
|
assertFalse(aIntegerType.equals(aLong));
|
||||||
|
assertTrue(aIntegerType.equals(aInt));
|
||||||
|
assertTrue(aLongType.equals(aLong));
|
||||||
|
assertFalse(aLongType.equals(aInt));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
53
src/org/eidecker/oca8lernen/general/StaticInitError.java
Normal file
53
src/org/eidecker/oca8lernen/general/StaticInitError.java
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
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.assertThrows;
|
||||||
|
|
||||||
|
public class StaticInitError {
|
||||||
|
|
||||||
|
public static int i = 0;
|
||||||
|
public static int j;
|
||||||
|
|
||||||
|
static {
|
||||||
|
j = 2 / i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class ConstructorInitError {
|
||||||
|
|
||||||
|
public ConstructorInitError() {
|
||||||
|
int a = 1/0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class TesterStatic {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInit() {
|
||||||
|
assertThrows(ExceptionInInitializerError.class, () -> {
|
||||||
|
StaticInitError staticInitError = new StaticInitError();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Unterschiedlicher Error beim zweiten Mal. Die Klasse scheint nicht mehr verwendet zu werden.
|
||||||
|
assertThrows(NoClassDefFoundError.class, () -> {
|
||||||
|
StaticInitError staticInitError = new StaticInitError();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConstructorInit() {
|
||||||
|
assertThrows(ArithmeticException.class, () -> {
|
||||||
|
ConstructorInitError constructorInitError = new ConstructorInitError();
|
||||||
|
});
|
||||||
|
|
||||||
|
assertThrows(ArithmeticException.class, () -> {
|
||||||
|
ConstructorInitError constructorInitError = new ConstructorInitError();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user