System.getProperty(“fast”) 是如何工作的?
在我在 Java 中搜索 #ifdef 等效项时,我发现了这个很棒的线程,它描述了一种执行条件编译的方法: #ifdef #ifndef in Java
我不明白的是它是如何工作的:
- 为什么是第二种形式 (System.getProperty) 比 第一个(假/真)?
- “快”不是 Java 的特点之一 预定义属性。这 可能意味着我必须定义 它在我的代码中的某个地方。什么是 在 Android 中执行此操作的最佳位置 应用程序? onCreate() 是个好地方吗?
In my search for a #ifdef-equivalent in Java, I found this great thread that describes a way to do conditional compilation: #ifdef #ifndef in Java
What I don't understand is how this really works:
- Why is the 2nd form
(System.getProperty) better than the
1st one (false/true)? - "fast" is not one of Java's
predefined properties. This
probably means that I have to define
it in my code somewhere. What is the
best place to do this in an Android
app? Is onCreate() a good place?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1)第一个将被编译器优化。也就是说,您将布尔属性设置为“true”,编译器将丢弃条件的 else 分支。如果将其设置为 false,则 then 分支将被丢弃。如果您想将快切换为慢,反之亦然,则必须重新编译代码。
2)启动应用程序时,您必须定义系统属性。设置 VM 范围的属性是一项特权操作。我不是 Android 专家,但我怀疑您是否可以在 Android 应用程序的 onCreate-event 中设置这些。 System.getProperty 背后的核心思想是,当您启动虚拟机时,通过传递额外的命令行参数(例如 -Dfast=true),您将能够切换应用程序模式。
1) The first one will be optimized by the compiler. That is, you'll set the boolean property to 'true' and the compiler will discard the else-branch of conditionals. If you set it to false, the then-branch will be discarded. You have to recompile your code if you want to switch fast to slow or vice versa.
2) You'll have to define the system property when you launch your app. Setting VM-wide properties is a previleged operation. I'm not an android expert but I doubt that you can set these in the onCreate-event of an android app. The core idea behind System.getProperty is, that you'll be able to switch the application mode when you start the virtual machine by passing an additional command line argument like -Dfast=true.
第二个更灵活,因为您可以决定在启动时打开或关闭某些内容。第一个示例将被优化掉,因此您需要重新编译以更改值。
系统属性由 -Dproperty=value 设置(注意:在传递给 main 方法的命令行参数之前)
The 2nd is more flexible, since you could decide to switch something on or off while starting. The 1st example will be optimized away, so you need to recompile to change the value.
A Systemproperty is set by -Dproperty=value (Note: before command line arguments which are passed to the main method)