使用静态变量字符串 [英] Using static variables for Strings

查看:159
本文介绍了使用静态变量字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的内容摘自
<一href=\"http://docs.blackberry.com/en/developers/deliverables/5580/Using_static_vars_for_strings_447038_11.jsp\">Best做法:编写高效的code
但我不明白为什么

 私有静态字符串x =榜样;

 私有静态最终字符串x =榜样;

有人能解释这个问题。


  

使用静态变量弦乐


  
  

当你定义静态字段(也
  称为类字段)String类型的,
  您可以通过提高应用程序的速度
  使用静态变量(不是最终的)
  而不是常量(final)。该
  相反的是原始数据真实
  类型,如int。


  
  

例如,您可以创建一个字符串
  对象如下:

 私有静态最终字符串x =榜样;

有关此静态常量(记为
  final关键字),每次你
  使用常量,临时字符串
  实例被创建。编译器
  消除了×,并与替换它
  在字节code字符串榜样,
  从而使为BlackBerry®Java®虚拟
  机执行哈希表查找
  每次您引用的X。

时间
  
  

在相反,对于一个静态变量(无
  final关键字),创建字符串
  一旦。黑莓JVM执行
  只有当它的哈希表查找
  初始化X,因此访问速度更快。

 私有静态字符串x =榜样;

您可以使用公共常量(即
  final字段),但你必须标记
  变量为私有。



解决方案

我不知道这一点,但对我来说很有意义:

JVM有一个内部字符串文字缓存。每次你用文字创造一个字符串时,JVM不得不寻找它在缓存中,如果它不存在,存储。

现在编译器可以内联与一个字符串文字最后的变量,因为它是在编译时已知,这似乎是一个性能不错的主意。

所以你的code:

 静态最后弦乐CONST =MYCONST;
...
如果(CONST.equals(AVAR))
...
案例CONST
...

是由编译器重写为:

 静态最后弦乐CONST =MYCONST;
...
如果(MYCONST.equals(AVAR))
...
案MYCONST
...

如果JVM的实现是不够聪明,它需要查找MYCONST三次在这个例子中。

在没有标记的const最终,编译器不能优化它,因为该变量可以在运行时改变。您code会被编译1:1和JVM只需要看看在该变量的对象

BTW:坏JVM实现不应该定义你的编码风格。 最终给出了大量的安全性,所以只要它并没有真正达到效果:不要在乎它是否增加或减少你的速度 - 它的下一个不同的JVM无论如何

below content is taken from Best practice: Writing efficient code but i didn't understand why

private static String x = "example";

faster than

private static final String x ="example";

Can anybody explain this.

Using static variables for Strings

When you define static fields (also called class fields) of type String, you can increase application speed by using static variables (not final) instead of constants (final). The opposite is true for primitive data types, such as int.

For example, you might create a String object as follows:

private static final String x = "example";

For this static constant (denoted by the final keyword), each time that you use the constant, a temporary String instance is created. The compiler eliminates "x" and replaces it with the string "example" in the bytecode, so that the BlackBerry® Java® Virtual Machine performs a hash table lookup each time that you reference "x".

In contrast, for a static variable (no final keyword), the String is created once. The BlackBerry JVM performs the hash table lookup only when it initializes "x", so access is faster.

private static String x = "example";

You can use public constants (that is, final fields), but you must mark variables as private.

解决方案

I wasn't aware of this, but it makes sense to me:

The JVM has an internal String Literal Cache. Everytime you create a String by using a literal, the JVM has to look for it in the cache and if it isn't there, store it.

Now a compiler can inline a final variable with a String literal, because it is known at compile time and it seems to be a good idea for performance.

So your code:

static final String CONST = "myconst";
...
if (CONST.equals(aVar))
...
case CONST
...

is rewritten by the compiler to:

static final String CONST = "myconst";
...
if ("myconst".equals(aVar))
...
case "myconst"
...

If the JVM implementation isn't clever enough, it needs to look up "myconst" three times in this example.

When you don't mark CONST as "final", the compiler can't "optimize" it since the variable can change at runtime. Your code would be compiled 1:1 and the JVM only needs to look for the Object at the variable.

btw: Bad JVM implementations shouldn't define your coding style. "final" gives a lot of safety, so as long as it doesn't really hit your performance: Don't care about if it increase or decrease you speed - its different for the next JVM anyhow

这篇关于使用静态变量字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆