<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                #### **可空性的實現原理** ~~~ /** * 研究可空性的實現原理 */ fun main(args: Array<String>) { } fun testNullable1(x:String,y:String?):Int{ return x.length } fun testNullable2(x:String,y: String?):Int?{ return y?.length } fun testNullbale3(x: String,y: String?):Int?{ return y!!.length } ~~~ 打開IDEA的 Tools > Kotlin > Show Kotlin Bytecode,然后,點擊 Decompile , 我們可以得到反編譯的Java代碼 ~~~ package A基礎; import kotlin.Metadata; import kotlin.jvm.internal.Intrinsics; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @Metadata( mv = {1, 1, 13}, bv = {1, 0, 3}, k = 2, d1 = {"\u0000\u001c\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0010\u0011\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0010\b\n\u0002\b\u0006\u001a\u0019\u0010\u0000\u001a\u00020\u00012\f\u0010\u0002\u001a\b\u0012\u0004\u0012\u00020\u00040\u0003¢\u0006\u0002\u0010\u0005\u001a\u0018\u0010\u0006\u001a\u00020\u00072\u0006\u0010\b\u001a\u00020\u00042\b\u0010\t\u001a\u0004\u0018\u00010\u0004\u001a\u001f\u0010\n\u001a\u0004\u0018\u00010\u00072\u0006\u0010\b\u001a\u00020\u00042\b\u0010\t\u001a\u0004\u0018\u00010\u0004¢\u0006\u0002\u0010\u000b\u001a\u001f\u0010\f\u001a\u0004\u0018\u00010\u00072\u0006\u0010\b\u001a\u00020\u00042\b\u0010\t\u001a\u0004\u0018\u00010\u0004¢\u0006\u0002\u0010\u000b¨\u0006\r"}, d2 = {"main", "", "args", "", "", "([Ljava/lang/String;)V", "testNullable1", "", "x", "y", "testNullable2", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Integer;", "testNullbale3", "helloKotlin"} ) public final class NullableTypesKt { public static final void main(@NotNull String[] args) { Intrinsics.checkParameterIsNotNull(args, "args"); } public static final int testNullable1(@NotNull String x, @Nullable String y) { Intrinsics.checkParameterIsNotNull(x, "x"); return x.length(); } @Nullable public static final Integer testNullable2(@NotNull String x, @Nullable String y) { Intrinsics.checkParameterIsNotNull(x, "x"); return y != null ? y.length() : null; } @Nullable public static final Integer testNullbale3(@NotNull String x, @Nullable String y) { Intrinsics.checkParameterIsNotNull(x, "x"); if (y == null) { Intrinsics.throwNpe(); } return y.length(); } } ~~~ 在不可空變量調用函數之前,都檢查了是否為空, 使用的是 kotlin.jvm.internal.Intrinsics 這個Java類里面的checkParameterIsNotNull 方法。如果是 null 就拋出異常: ~~~ public static void checkParameterIsNotNull(Object value, String paramName) { if (value == null) { throwParameterIsNullException(paramName); } } ~~~ 同時,我們可以看出在Kotlin中函數的入參聲明 ``` fun testNullable1(x: String, y: String?) ``` 反編譯成等價的Java代碼是 ~~~ public static final int testNullable1(@NotNull String x, @Nullable String y) ~~~ 可以看出,這里**使用注解 @NotNull 標注不可空的變量,使用注解 @Nullable 標注一個變量可空**。 可空變量的安全調用符 y?.length 等價的Java代碼就是 ~~~ y != null ? y.length() : null; ~~~ 可空變量的斷言調用 y!!.length 等價的Java代碼是: ~~~ if (y == null) { Intrinsics.throwNpe(); } return y.length(); ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看