by the compiler to reference commonly needed methods public static void checkParameterIsNotNull(Object value, String pa if (value == null) { throwParameterIsNullException(paramName); } }
since the Kotlin compiler can guarantee null is never passed. class NonNullInPrivateMethod { private fun a(s: String) = s } public final class NonNullInPrivateMethod { private final String a(String s) { return s; } }
All the same fun ifExpression(b: Boolean): String { return if (b) "foo" else "buz" } @NotNull public static final String ifExpression(boolean b) { return b ? "foo" : "buz"; } fun ifMethodExpression(b: Boolean): String = if (b) "foo" else "buz" fun ifMethodExpressionWithoutReturnType(b: Boolean) = if (b) "foo" else "buz"
v) { String t; if (v instanceof String) { t = "String has length " + ((String)v).length(); System.out.println(t); } Object var10000 = v; if (!(v instanceof String)) { var10000 = null; } t = (String)var10000; if (v == null) { throw new TypeCastException("null cannot be cast to non-null } else { String u = (String)v; } }
b: String = "Hello", c: Boolean @JvmOverloads public static final int defaultArguments(int a, @NotNull String b) return defaultArguments$default(a, b, false, 4, (Object)null); } @JvmOverloads public static final int defaultArguments(int a) { return defaultArguments$default(a, (String)null, false, 6, (Obj } @JvmOverloads public static final int defaultArguments() { return defaultArguments$default(0, (String)null, false, 7, (Obj }
copy pastes method at compile time to call site. Absolutely no overhead at runtime @kotlin.internal.InlineOnly public inline fun require(value: Boolean): Unit = require(value) { "Failed requirement." } @kotlin.internal.InlineOnly public inline fun require(value: Boolean, lazyMessage: () -> Any): if (!value) { val message = lazyMessage() throw IllegalArgumentException(message.toString()) } }
factorial(n: Long): Long = if (n == 1L) 1 else n * factorial(n - 1) public static final long factorial(long n) { return n == 1L ? 1L : n * factorial(n - 1L); } fun factorial2(n: Long, total: Long = 1): Long = if (n == 1L) total else factorial2(n - 1, total * n) public static final long factorial2(long n, long total) { return n == 1L ? total : factorial2(n - 1L, total * n); }
Long = 1): Long = if (n == 1L) total else factorialTailrec(n - 1, total * n) public static final long factorialTailrec(long n, long total) { while(n != 1L) { long var10000 = n - 1L; total *= n; n = var10000; } return total; }
Int) = when { this < lo -> lo this > hi -> hi else -> this } public static final int clamp(int $receiver, int lo, int hi) { return $receiver < lo ? lo : ($receiver > hi ? hi : $receiver); } fun callClamp() { 6.clamp(5, 10) } public static final void callClamp() { clamp(6, 5, 10); }
= i * x nested(42) } public static final void nestedFunction(final int i) { <undefinedtype> nested$ = new Function1() { // $FF: synthetic method // $FF: bridge method public Object invoke(Object var1) { return this.invoke(((Number)var1).intValue()); } public final int invoke(int x) { return i * x; } }; nested$.invoke(42); }
a; @NotNull private final String b; public final int getA() { return this.a; } @NotNull public final String getB() { return this.b; } public D(int a, @NotNull String b) { Intrinsics.checkParameterIsNotNull(b, "b"); super(); this.a = a; this.b = b; boolean var3 = this.a > 0; if (!var3) {
of T is ${T::class}") } private static final void genericType() { StringBuilder var10000 = (new StringBuilder()).append("Type of Intrinsics.reifiedOperationMarker(4, "T"); String var1 = var10000.append(Reflection.getOrCreateKotlinClass System.out.println(var1); } public static void reifiedOperationMarker(int id, String typeParam throwUndefinedForReified(); } public static void throwUndefinedForReified() { throwUndefinedForReified( "This function has a reified type parameter and thus can o ); }
genericType<String>() } public static final void callGenericType() { String var0 = "Type of T is " + Reflection.getOrCreateKotlinClass(String.class); System.out.println(var0); }
with debugging tricky errors Most of the stuff is pretty straight forward Created byte code might look inefficient sometimes, but JIT takes care of that It's easy to look at bytecode in IntelliJ Tools -> Kotlin -> Show Kotlin Bytecode -> Decompile