Skip to content

Instantly share code, notes, and snippets.

Avatar

Myroslav Kolodii mirokolodii

  • Wroclaw, Poland
View GitHub Profile
@mirokolodii
mirokolodii / Test.kt
Created Oct 24, 2019
Switch to Kotlin 2.9
View Test.kt
val nonNullInt = 1
val nullInt: Int? = null
if (nullInt == nonNullInt) {
println("Both are equal")
} else {
println("Not equal") // Result is 'Not equal'
}
@mirokolodii
mirokolodii / Test.kt
Last active Oct 24, 2019
Switch to Kotlin #2.8
View Test.kt
var nonNullInt = 1
val nullInt: Int? = null
// 1. Error
nonNullInt = nullInt // Error: type mismatch
// 2. Valid
if (nullInt != null) {
/* Here is used a smart cast (one of Kotlin compiler's feature),
which casts Int? to Int automatically */
nonNullInt = nullInt
}
@mirokolodii
mirokolodii / Test.java
Last active Oct 24, 2019
Switch to Kotlin #2.7
View Test.java
Integer nullInt = null;
if (someInt.equals(23)) { // NullPointerException because of attempt to call
// a method on a null
// Rest of code
}
@mirokolodii
mirokolodii / Test.java
Created Oct 18, 2019
Switch to Kotlin #2.6
View Test.java
Integer intObj = null;
int primitiveInt;
primitiveInt = intObj; // NullPointerException during unboxing
@mirokolodii
mirokolodii / Test.kt
Created Oct 18, 2019
Switch to Kotlin #2.5
View Test.kt
var nullableStr: String? = null
var nonNullStr: String = "a string"
nullableStr = nonNullStr // valid
nonNullStr = nullableStr // error, should be checked for nullability first
If (nullableStr != null) {
nonNullStr = nullableStr // valid
}
@mirokolodii
mirokolodii / Test.kt
Last active Oct 18, 2019
Switch to Kotlin #2.4
View Test.kt
Class Test(val someInt: Int)
val test1: Test // warning: no default value, should be initialized
// with non-null value before first use
val test2: Test = null // error: non-nullable type can't hold null value
val test3: Test? = null // valid
@mirokolodii
mirokolodii / Test.kt
Last active Oct 24, 2019
Switch to Kotlin #2.3
View Test.kt
val someStr1: String? = null // valid statement, notice a question mark after 'String'
val someStr2: String? // warning: no default value assigned. Should be initialized
// with some (nullable or non-nullable) value before its first use
@mirokolodii
mirokolodii / Test.kt
Last active Oct 18, 2019
Switch to Kotlin #2.2
View Test.kt
val someStr1: String = "" // same as in Java, i.e initialized with empty string
val someStr2: String // warning: no default value assigned. Should be initialized
// with a non-null value before its first use
val someStr3: String = null // error: can't be null
@mirokolodii
mirokolodii / Test.java
Created Oct 18, 2019
Switch to Kotlin #2.1
View Test.java
String someStr1 = ""; // initialized with empty string
String someStr2; // by default value of someStr2 is null
View gist:fc816d262ac91c344659bde90c0c284e
@Test
public void FilterClickShouldChangeRecyclerViewCount() {
// Get items count before action
RecyclerView recyclerView = mActivityTestRule.getActivity().findViewById(R.id.recycler_view);
int countBefore = Objects.requireNonNull(recyclerView.getAdapter()).getItemCount();
Log.e("count", "Items count before: " + countBefore);
// Perform action
ViewInteraction actionMenuItemView = onView(
allOf(