Long valueOf instead of new Long

Paul Sandoz paul.sandoz at oracle.com
Thu Jun 26 13:59:35 UTC 2014


On Jun 26, 2014, at 3:29 PM, Andrej Golovnin <andrej.golovnin at gmail.com> wrote:

> Hi Paul,
> 
> Seems fine to me (pending a full test run).
> 
> Just a syntax niggle in the following, too many brackets:
> 
> +++ new/src/share/classes/javax/management/modelmbean/RequiredModelMBean.java   2014-06-14 10:16:02.486298421 -0300
> @@ -544,7 +544,7 @@
>              }
> 
>              // convert seconds to milliseconds for time comparison
> -            currencyPeriod = ((new Long(expTime)).longValue()) * 1000;
> +            currencyPeriod = ((Long.valueOf(expTime)).longValue()) * 1000;
>              if (currencyPeriod < 0) {
>                  /* if currencyTimeLimit is -1 then value is never cached */
>                  returnCachedValue = false;
> @@ -580,7 +580,7 @@
>                  if (tStamp == null)
>                      tStamp = "0";
> 
> -                long lastTime = (new Long(tStamp)).longValue();
> +                long lastTime = (Long.valueOf(tStamp)).longValue();
> 
> 
> Paul.
> 
> 
> Wouldn't it be better to use here Long.parseLong(String)? Long.valueOf(String) would always create a new object here, as expTime and tStamp represents the time and they are never in the range [-128;127].
> 

Well spotted, there is no need to box at all.

There are a couple of other cases too (and probably more, need to look more carefully):

--- old/src/share/classes/com/sun/security/auth/UnixNumericUserPrincipal.java	2014-06-14 10:15:58.646298533 -0300
+++ new/src/share/classes/com/sun/security/auth/UnixNumericUserPrincipal.java	2014-06-14 10:15:58.514298537 -0300
@@ -87,7 +87,7 @@
      *                  represented as a long.
      */
     public UnixNumericUserPrincipal(long name) {
-        this.name = (new Long(name)).toString();
+        this.name = Long.valueOf(name).toString();   <--- this.name = Long.toString(name);
     }
 
     /**
@@ -113,7 +113,7 @@
      *          <code>UnixNumericUserPrincipal</code> as a long.
      */
     public long longValue() {
-        return ((new Long(name)).longValue());
+        return Long.valueOf(name);  <--- Long.parseLong(name);
     }

Paul.



More information about the core-libs-dev mailing list