Note that there are some explanatory texts on larger screens.

plurals
  1. PODifference between Long.valueOf(java.lang.String) and new Long(java.lang.String)?
    text
    copied!<p>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. </p> <p>Coder #1 has done this:</p> <pre><code>String strId = "12345678"; ... Long lId = new Long(strId); </code></pre> <p>While coder #2 has done this:</p> <pre><code>String strId = "12345678"; ... Long lId = Long.valueOf(strId); </code></pre> <p>Functionally, the code operates exactly the same. There's a try/catch block around each bit to handle any <code>NumberFormatException</code> that is thrown. The incoming string value is an 8 digit string that represents a decimal: <code>"12345678"</code> and in both cases it is correctly converted into <code>Long</code>.</p> <p>Is there any functional difference between passing the string in the constructor and using Long.valueOf()? I've checked the constructor doc here:</p> <p><a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html#Long%28java.lang.String%29" rel="nofollow noreferrer">Long(java.lang.String)</a></p> <p>and the docs for valueOf() here:</p> <p><a href="http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html#valueOf%28java.lang.String%29" rel="nofollow noreferrer">Long.valueOf(java.lang.String)</a></p> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload