当前位置: 首页 > news >正文

个人网站建设的小清新图片汇编语言做网站

个人网站建设的小清新图片,汇编语言做网站,大学生网页设计作业,字节跳动直播开放平台在java中使用final声明常量在kotlin中使用const val声明常量 常量在编译为字节码后会直接把调用常量的地方直接替换为常量值#xff0c;示例如下#xff1a; public class ConstDemo {public static final String NAME Even;private static final int ID 100…在java中使用final声明常量在kotlin中使用const val声明常量 常量在编译为字节码后会直接把调用常量的地方直接替换为常量值示例如下 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字节码在IntelliJ中可以直接双击这个class字节码它是自带反编译器效果如下 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);} }如上代码可以发现只要是final修饰的变量在调用时直接被常量值替代了有一个例外就是在局部变量中声明的final int count;它不是在声明时直接赋值的而是经过一个if判断之后才赋值的所以需要在运行时才能确定它的值是多少所以在编译为字节码时调用该变量的地方没有被常量值替换因为此时不知道它的值是多少。 另外也看到了一些有趣的地方编译时编译器会有一些优化比如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);} }运行结果如下 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字节码之后调用它的地方已经被常量值所替换为什么常量的声明语句还保留了因为还是有可能会被用到的比如我们通过反射读取该常量的值这是需要在运行时才能完成的无法在编译阶段就直接使用常量值替代的。 再来看一个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);} }编译为字节码后再反编译结果如下 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常量在编译为字节码时无法使用常量值代替因为它是一个对象而对象的内存地址得在运行时才能确定所以这种不应该叫常量的所以kotlin在这方面就做的比较好表示一个变量不可改变用val表示一个常量用const val分得更加清楚示例如下 const val NAME Evenclass ConstDemo {val width 100var height 200companion object {const val ID 1001JvmStaticfun main(args: ArrayString) {println(NAME)println(NAME)println(ID)println(ID)val demo ConstDemo()println(demo.width)println(demo.height)}}}可以看到声明常量的地方只能是顶级属性或者companion object中要查看反编译如果直接在IntelliJ中找到class文件然后双击会发现反编译不了我们可以这样查看工具 Kotlin 显示Kotlin字节码 反编译结果如下 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.zqtcl.cn/news/321220/

相关文章:

  • 网站免费空间购买wordpress支持页面模版
  • 腾讯建设网站视频宁波城乡住房建设厅网站
  • 乐清网站开发公司个人网站建设工作室
  • 网站空间升级通知手机端怎么看世界杯
  • 广西南宁网站推广建设网站视频教程
  • 福州专业网站建设推广费用nas可做网站服务器吗
  • 齐鲁建设网站福建省高速公路建设管理网站
  • 比格设计网站官网收录网站查询
  • 国外做直播网站淘宝电商网站怎么做的
  • 国外私人网站网站由那些组成
  • 网站备案多久通过机械设备网站
  • 企业自建站案例网站基础知识域名5个点
  • 咸宁建设网站海口市网站建设
  • 认识电子商务网站建设技术网站交换链接怎么做?
  • 定制商城网站建设全球搜索引擎排名2021
  • 徐州百度网站快速优化做网站视频图片加载不出来
  • 网站被host重定向处理浙江网新股吧
  • asp国外网站什么页游好玩
  • 高端简约30平米办公室装修广州搜索seo网站优化
  • 海口的网站建设公司wordpress二次元极简主题
  • 南京快速建站公司国家网站域名
  • 兰州装修公司哪家好网站seo推广员招聘
  • 郑州网站推广 汉狮网络易企秀类似的软件
  • 做外单网站成都网页制作公司排名
  • 成都优化网站关键词搜索引擎有哪些平台
  • 福建百川建设有限公司网站郑州手机软件开发公司
  • 盐城企业做网站多少钱88建网站
  • 南京网站制作报价wordpress主题 yusi
  • 北京建网站已备案网站新增接入
  • 做搬家服务网站问卷调查的目的房产网签是什么意思