Long.valueOf(java.lang.String)和new Long(java.lang.String)之间的区别?

时间:2022-09-15 19:03:25

I'm consolidating code written by two different people and notice that casting a String value into a Long has been done in two different ways.

我正在整合由两个不同的人编写的代码,并注意到将一个String值转换为Long已经以两种不同的方式完成。

Coder #1 has done this:

编码器#1做到了这一点:

String strId = "12345678";
...
Long lId = new Long(strId);

While coder #2 has done this:

编码器#2做到了这一点:

String strId = "12345678";
...
Long lId = Long.valueOf(strId);

Functionally, the code operates exactly the same. There's a try/catch block around each bit to handle any NumberFormatException that is thrown. The incoming string value is an 8 digit string that represents a decimal: "12345678" and in both cases it is correctly converted into Long.

在功能上,代码操作完全相同。每个位周围都有一个try / catch块来处理抛出的任何NumberFormatException。传入的字符串值是一个8位数的字符串,表示一个小数:“12345678”,在这两种情况下都正确地转换为Long。

Is there any functional difference between passing the string in the constructor and using Long.valueOf()? I've checked the constructor doc here:

在构造函数中传递字符串和使用Long.valueOf()之间是否存在任何功能差异?我在这里检查了构造函数doc:

Long(java.lang.String)

and the docs for valueOf() here:

以及valueOf()的文档:

Long.valueOf(java.lang.String)

As far as I can tell, they both call parseLong() so it doesn't matter which is used. I just want to make sure I'm not setting myself up for some strange behavior further down the road. Also, is either style more "correct" (haha) than the other?

据我所知,他们都调用parseLong()所以使用哪个并不重要。我只是想确保自己不会在未来的某些奇怪行为中做好准备。另外,哪种风格比其他风格更“正确”(哈哈)?

6 个解决方案

#1


25  

The difference is that using new Long() you will always create a new object, while using Long.valueOf(), may return you the cached value of long if the value is between [-128 to 127].

不同之处在于使用新的Long()将始终创建一个新对象,而使用Long.valueOf()时,如果值介于[-128到127]之间,则可能会返回long的缓存值。

So, you should prefer Long.valueOf method, because it may save you some memory.

所以,你应该更喜欢Long.valueOf方法,因为它可以为你节省一些内存。

If you see the source code for Long.valueOf(String), it internally invokes Long.valueOf(long), whose source code I have posted below: -

如果您看到Long.valueOf(String)的源代码,它会在内部调用Long.valueOf(long),其源代码我在下面发布: -

public static Long valueOf(String s) throws NumberFormatException
{
    return Long.valueOf(parseLong(s, 10));
}

public static Long valueOf(long l) {
    final int offset = 128;
    if (l >= -128 && l <= 127) { // will cache 
        return LongCache.cache[(int)l + offset];
    }
    return new Long(l);
}

#2


8  

Long.valueOf() should be preferred: it returns cached values of Long for some often-used values instead of constructing a new instance as the constructor does.

Long.valueOf()应该是首选:它为一些常用值返回Long的缓存值,而不是像构造函数那样构造新实例。

Even if some Java versions don't use a cache, using valueOf() makes it possible in future versions, whereas the constructor will always create a new instance.

即使某些Java版本不使用缓存,使用valueOf()也可以在将来的版本中使用,而构造函数将始终创建一个新实例。

#3


3  

They mean the same

他们的意思是一样的

public static Long valueOf(String s) throws NumberFormatException{
        return new Long(parseLong(s, 10));
}

public Long(String s) throws NumberFormatException {
    this.value = parseLong(s, 10);
}

Source JDK 6.0

来源JDK 6.0

#4


3  

Both do parseLong(String, int) internally (int being radix with value as 10), but valueOf has advantage as documented below:

两者都在内部执行parseLong(String,int)(int是基数,值为10),但valueOf具有以下记录的优势:

If a new Long instance is not required, this method should generally be used in preference to the constructor Long(long), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

如果不需要新的Long实例,则通常应优先使用此方法,而不是构造函数Long(long),因为此方法可能通过缓存频繁请求的值来显着提高空间和时间性能。

#5


0  

This is the PMD plugin out put which is run on eclipse

这是在eclipse上运行的PMD插件输出

Code I checked is

我检查的代码是

Long l = new Long("123456");

In JDK 1.5, calling new Long() causes memory allocation. Long.valueOf() is more memory friendly.

在JDK 1.5中,调用new Long()会导致内存分配。 Long.valueOf()更加内存友好。

#6


0  

i am thinking how to change range and size of the cache for our application, overloaded with Longs;

我正在考虑如何为我们的应用程序更改缓存的范围和大小,重载Longs;

such change is not supported by j2se api One way is to change loaded java byte code with ClassLoader or even with JVMTI (it allows to keep such trick out of the project, like external tuning)

j2se api不支持这种更改。一种方法是使用ClassLoader或甚至使用JVMTI更改加载的Java字节代码(它允许将这样的技巧保留在项目之外,如外部调优)

or, may be, to create external cache and own static cachedValueOf() which is straight forward, but the code depending on some not applicational needs is not nice

或者,可能是,创建外部缓存和自己的静态cachedValueOf()这是直截了当的,但代码取决于一些不适用的需求是不好的

#1


25  

The difference is that using new Long() you will always create a new object, while using Long.valueOf(), may return you the cached value of long if the value is between [-128 to 127].

不同之处在于使用新的Long()将始终创建一个新对象,而使用Long.valueOf()时,如果值介于[-128到127]之间,则可能会返回long的缓存值。

So, you should prefer Long.valueOf method, because it may save you some memory.

所以,你应该更喜欢Long.valueOf方法,因为它可以为你节省一些内存。

If you see the source code for Long.valueOf(String), it internally invokes Long.valueOf(long), whose source code I have posted below: -

如果您看到Long.valueOf(String)的源代码,它会在内部调用Long.valueOf(long),其源代码我在下面发布: -

public static Long valueOf(String s) throws NumberFormatException
{
    return Long.valueOf(parseLong(s, 10));
}

public static Long valueOf(long l) {
    final int offset = 128;
    if (l >= -128 && l <= 127) { // will cache 
        return LongCache.cache[(int)l + offset];
    }
    return new Long(l);
}

#2


8  

Long.valueOf() should be preferred: it returns cached values of Long for some often-used values instead of constructing a new instance as the constructor does.

Long.valueOf()应该是首选:它为一些常用值返回Long的缓存值,而不是像构造函数那样构造新实例。

Even if some Java versions don't use a cache, using valueOf() makes it possible in future versions, whereas the constructor will always create a new instance.

即使某些Java版本不使用缓存,使用valueOf()也可以在将来的版本中使用,而构造函数将始终创建一个新实例。

#3


3  

They mean the same

他们的意思是一样的

public static Long valueOf(String s) throws NumberFormatException{
        return new Long(parseLong(s, 10));
}

public Long(String s) throws NumberFormatException {
    this.value = parseLong(s, 10);
}

Source JDK 6.0

来源JDK 6.0

#4


3  

Both do parseLong(String, int) internally (int being radix with value as 10), but valueOf has advantage as documented below:

两者都在内部执行parseLong(String,int)(int是基数,值为10),但valueOf具有以下记录的优势:

If a new Long instance is not required, this method should generally be used in preference to the constructor Long(long), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

如果不需要新的Long实例,则通常应优先使用此方法,而不是构造函数Long(long),因为此方法可能通过缓存频繁请求的值来显着提高空间和时间性能。

#5


0  

This is the PMD plugin out put which is run on eclipse

这是在eclipse上运行的PMD插件输出

Code I checked is

我检查的代码是

Long l = new Long("123456");

In JDK 1.5, calling new Long() causes memory allocation. Long.valueOf() is more memory friendly.

在JDK 1.5中,调用new Long()会导致内存分配。 Long.valueOf()更加内存友好。

#6


0  

i am thinking how to change range and size of the cache for our application, overloaded with Longs;

我正在考虑如何为我们的应用程序更改缓存的范围和大小,重载Longs;

such change is not supported by j2se api One way is to change loaded java byte code with ClassLoader or even with JVMTI (it allows to keep such trick out of the project, like external tuning)

j2se api不支持这种更改。一种方法是使用ClassLoader或甚至使用JVMTI更改加载的Java字节代码(它允许将这样的技巧保留在项目之外,如外部调优)

or, may be, to create external cache and own static cachedValueOf() which is straight forward, but the code depending on some not applicational needs is not nice

或者,可能是,创建外部缓存和自己的静态cachedValueOf()这是直截了当的,但代码取决于一些不适用的需求是不好的