中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

做淘寶網(wǎng)站的企業(yè)網(wǎng)站排名優(yōu)化方案

做淘寶網(wǎng)站的,企業(yè)網(wǎng)站排名優(yōu)化方案,做網(wǎng)站安全的公司有哪些,數(shù)據(jù)網(wǎng)站排名在java中使用final聲明常量在kotlin中使用const val聲明常量 常量在編譯為字節(jié)碼后會直接把調(diào)用常量的地方直接替換為常量值,示例如下: public class ConstDemo {public static final String NAME "Even";private static final int ID 100…
  • java中使用final聲明常量
  • kotlin中使用const val聲明常量

常量在編譯為字節(jié)碼后會直接把調(diào)用常量的地方直接替換為常量值,示例如下:

public class ConstDemo {public static final String NAME = "Even";private static final int ID = 1001;static final int YEAR = 2024;public final int color = 255;public static int width = 100;public int height = 200;public static void main(String[] args) {System.out.println(NAME);System.out.println(NAME);System.out.println(ID);System.out.println(ID);System.out.println(YEAR);System.out.println(YEAR);ConstDemo demo = new ConstDemo();System.out.println(demo.color);System.out.println(demo.color);final int number = 9;System.out.println(number);System.out.println(number);System.out.println("--------------------------------");final int count;if (width > 100) {count = 1;} else {count = 2;}System.out.println(count);System.out.println(count);System.out.println(width);System.out.println(width);System.out.println(demo.height);System.out.println(demo.height);int weight = 99;System.out.println(weight);System.out.println(weight);}}

編譯后得到class字節(jié)碼,在IntelliJ中可以直接雙擊這個class字節(jié)碼,它是自帶反編譯器,效果如下:

public class ConstDemo {public static final String NAME = "Even";private static final int ID = 1001;static final int YEAR = 2024;public final int color = 255;public static int width = 100;public int height = 200;public ConstDemo() {}public static void main(String[] args) {System.out.println("Even");System.out.println("Even");System.out.println(1001);System.out.println(1001);System.out.println(2024);System.out.println(2024);ConstDemo demo = new ConstDemo();PrintStream var10000 = System.out;Objects.requireNonNull(demo);var10000.println(255);var10000 = System.out;Objects.requireNonNull(demo);var10000.println(255);int number = true;System.out.println(9);System.out.println(9);System.out.println("--------------------------------");byte count;if (width > 100) {count = 1;} else {count = 2;}System.out.println(count);System.out.println(count);System.out.println(width);System.out.println(width);System.out.println(demo.height);System.out.println(demo.height);int weight = 99;System.out.println(weight);System.out.println(weight);}
}

如上代碼,可以發(fā)現(xiàn),只要是final修飾的變量在調(diào)用時直接被常量值替代了,有一個例外,就是在局部變量中聲明的final int count;,它不是在聲明時直接賦值的,而是經(jīng)過一個if判斷之后才賦值的,所以需要在運(yùn)行時才能確定它的值是多少,所以在編譯為字節(jié)碼時調(diào)用該變量的地方?jīng)]有被常量值替換,因為此時不知道它的值是多少。

另外也看到了一些有趣的地方,編譯時編譯器會有一些優(yōu)化,比如int number = true;還能這樣啊?沒搞懂,它的final被去掉了,count中地final修飾符也被去掉了,而且類型變成了byte類型,編譯器通過if中的判斷得出值不是1就是2,用byte足已,所以改成了byte類型。

基于這個常量的特性,我們可以猜到,通過反射也是無法修改final類型的常量的,示例如下:

public class ConstDemo {public static final int age = 18;public static void main(String[] args) throws Exception {Field field = ConstDemo.class.getField("age");System.out.println("age = " + field.get(null));field.set(null, 30);System.out.println(age);}
}

運(yùn)行結(jié)果如下:

age = 18
Exception in thread "main" java.lang.IllegalAccessException: Can not set static final int field ConstDemo.age to java.lang.Integerat java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:76)at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(UnsafeFieldAccessorImpl.java:80)at java.base/jdk.internal.reflect.UnsafeQualifiedStaticIntegerFieldAccessorImpl.set(UnsafeQualifiedStaticIntegerFieldAccessorImpl.java:77)at java.base/java.lang.reflect.Field.set(Field.java:799)at ConstDemo.main(ConstDemo.java:9)

從這里也可以看出,為什么常量在編譯為class字節(jié)碼之后,調(diào)用它的地方已經(jīng)被常量值所替換,為什么常量的聲明語句還保留了,因為還是有可能會被用到的,比如我們通過反射讀取該常量的值,這是需要在運(yùn)行時才能完成的,無法在編譯階段就直接使用常量值替代的。

再來看一個Demo:

public class ConstDemo {public final int age = 18;public static final ConstDemo demo = new ConstDemo();public static void main(String[] args) throws Exception {System.out.println(demo);System.out.println(demo);System.out.println(demo.age);System.out.println(demo.age);}
}

編譯為字節(jié)碼后,再反編譯結(jié)果如下:

public class ConstDemo {public final int age = 18;public static final ConstDemo demo = new ConstDemo();public ConstDemo() {}public static void main(String[] args) throws Exception {System.out.println(demo);System.out.println(demo);PrintStream var10000 = System.out;Objects.requireNonNull(demo);var10000.println(18);var10000 = System.out;Objects.requireNonNull(demo);var10000.println(18);}
}

可以看到聲明為非原始類型的final常量在編譯為字節(jié)碼時無法使用常量值代替,因為它是一個對象,而對象的內(nèi)存地址得在運(yùn)行時才能確定,所以這種不應(yīng)該叫常量的,所以,kotlin在這方面就做的比較好,表示一個變量不可改變用val,表示一個常量用const val,分得更加清楚,示例如下:

const val NAME = "Even"class ConstDemo {val width = 100var height = 200companion object {const val ID = 1001@JvmStaticfun main(args: Array<String>) {println(NAME)println(NAME)println(ID)println(ID)val demo = ConstDemo()println(demo.width)println(demo.height)}}}

可以看到,聲明常量的地方只能是頂級屬性或者companion object中,要查看反編譯,如果直接在IntelliJ中找到class文件然后雙擊會發(fā)現(xiàn)反編譯不了,我們可以這樣查看:工具 > Kotlin > 顯示Kotlin字節(jié)碼 > 反編譯,結(jié)果如下:

public final class ConstDemo {private final int width = 100;private int height = 200;public static final int ID = 1001;public final int getWidth() {return this.width;}public final int getHeight() {return this.height;}public final void setHeight(int var1) {this.height = var1;}public static final void main(@NotNull String[] args) {Intrinsics.checkNotNullParameter(args, "args");String var2 = "Even";System.out.println(var2);var2 = "Even";System.out.println(var2);short var4 = 1001;System.out.println(var4);var4 = 1001;System.out.println(var4);ConstDemo demo = new ConstDemo();int var3 = demo.getWidth();System.out.println(var3);var3 = demo.getHeight();System.out.println(var3);}
}public final class ConstDemoKt {@NotNullpublic static final String NAME = "Even";
}

在Kotlin中,常量只能是8大原始類型,不能是對象類型的,如下代碼在編譯器就報錯了:
在這里插入圖片描述
聲明常量有什么好處?這里我想到之前寫的一篇文章:https://blog.csdn.net/android_cai_niao/article/details/113571171

http://www.risenshineclean.com/news/52321.html

相關(guān)文章:

  • 局域網(wǎng)網(wǎng)站建設(shè)需要什么條件市場推廣和銷售的區(qū)別
  • 山東有哪些網(wǎng)絡(luò)公司優(yōu)化大師客服
  • 開封網(wǎng)站建設(shè)百度商店應(yīng)用市場
  • 青海網(wǎng)站建設(shè)企業(yè)四川seo選哪家
  • 機(jī)械做網(wǎng)站好處百度推廣管家登錄
  • 企業(yè)網(wǎng)站需要注意什么搜索引擎營銷是什么意思
  • 網(wǎng)站作用愛站網(wǎng)愛情電影網(wǎng)
  • 合肥網(wǎng)頁制作設(shè)計重慶網(wǎng)站排名優(yōu)化教程
  • 徐州建設(shè)網(wǎng)站公司建網(wǎng)站費(fèi)用
  • 做公司標(biāo)志用哪個網(wǎng)站網(wǎng)絡(luò)優(yōu)化需要哪些知識
  • 有口碑的企業(yè)網(wǎng)站建設(shè)專業(yè)的制作網(wǎng)站開發(fā)公司
  • 網(wǎng)站的建設(shè)與維護(hù)工資平臺優(yōu)化是什么意思
  • 東莞網(wǎng)站設(shè)計公司軟文標(biāo)題大全
  • 德爾普網(wǎng)絡(luò)做網(wǎng)站怎么樣淘寶搜索關(guān)鍵詞技巧
  • 電腦首頁wordpress公眾號排名優(yōu)化
  • 手機(jī)系統(tǒng)網(wǎng)站seo自動優(yōu)化工具
  • 動態(tài)h5網(wǎng)站開發(fā)百度指數(shù)分析數(shù)據(jù)
  • 凡科快圖軟件下載南昌seo計費(fèi)管理
  • 網(wǎng)站聯(lián)盟怎么做青島關(guān)鍵詞排名哪家好
  • 泉州seo-泉州網(wǎng)站建設(shè)公司電商平臺的營銷方式
  • 做銷售在那個網(wǎng)站找新鄭網(wǎng)絡(luò)推廣公司
  • 金融網(wǎng)站建設(shè)成功案例品牌關(guān)鍵詞優(yōu)化哪家便宜
  • 用ps做零食網(wǎng)站模板seo培訓(xùn)資料
  • 網(wǎng)站建設(shè)公司名字seo網(wǎng)站優(yōu)化師
  • 做網(wǎng)站什么字體網(wǎng)絡(luò)營銷課程感悟
  • 怎么查看網(wǎng)站的安全性百度推廣優(yōu)化怎么做
  • 網(wǎng)站建設(shè)制作公司地址發(fā)軟文是什么意思
  • 網(wǎng)站開發(fā)目的騰訊企點賬戶中心
  • 珠海高端網(wǎng)站制作公司怎么做網(wǎng)頁設(shè)計的頁面
  • 溫州網(wǎng)站推廣站建設(shè)河北seo診斷培訓(xùn)