How To Convert Primitive Int Value To A Long Object Inwards Java?

Suppose you lot guide maintain an int variable but the utilisation of the application is expecting a Long object, how do you lot convert a primitive int to a Long object inwards Java? It shouldn't last a problem, right? afterward all long is a bigger information type than int, thence all int values are acceptable equally long, but nosotros too take away a Long object, non only the long primitive value. Now, the occupation is reduced to converting a long primitive to Long object, which is non actually a occupation if you lot are running on JRE version higher than Java 5. But, sometimes autoboxing is non efficient e.g. when you lot guide maintain to convert multiple long values into the Long object inwards a loop. Influenza A virus subtype H5N1 ameliorate agency to convert your long primitive to the Long object is past times using static mill methods similar Long.valueOf(long l), which was originally meant to convert long primitive values to a Long object, prior to Java 5, but, too plant fine amongst int primitive.

So, if you lot guide maintain got an int primitive value, only overstep it to Long.valueOf() in addition to you lot volition acquire a Long object amongst same value.

Btw, This solely plant good if the method is expecting a long primitive value, if it's expecting a Long object thence you lot commencement take away to cast an int to long, thence that autoboxing tin kick-in (see NullPointerException while converting a zilch Long object to long primitive. So you lot should beware of that. Now that you lot know approximately concepts, let's run into this conversion inwards action.




int to Long - using autoboxing

In gild to leverage autoboxing, you lot must typecast an int variable to long, otherwise, the compiler volition complain nearly mismatch type, equally shown below :

Long 1 = 10; // Type mismatch : cannot convert from int to Long
Long 3 = (long) 3; // plant fine

Same is truthful for a method which is expecting a Long object. You cannot overstep an int to them because auto-boxing volition non last able to convert an int to Long (see The Complete Java Masterclass - Update for Java 11), but, if you lot cast an int to long, thence auto-boxing volition bring attention of converting it to Long object, equally shown below :

List<Long> listOfLong = novel ArrayList<Long>();

for(int i=0; i< 10; i++){
    listOfLong.add((long)i);
}

So, if you lot desire to utilisation autoboxing, brand certain you lot cast the int value to long first, don't worry though, if you lot forget compiler volition ensure this :)

Another matter to discover inwards the higher upward code is that nosotros are using auto-boxing inwards the loop, which tin drive functioning outcome because a lot of Long objects volition last created in addition to discarded.

In our case, it is yet ok because the loop is running solely 10 times, but if guide maintain to do this conversion nation a 1 chiliad one thousand times, this approach volition drive a problem, equally explained past times Joshua Bloch inwards his classic book, Effective Java tertiary Edition. Influenza A virus subtype H5N1 must-read for whatsoever Java developer.




int to Long - using Long.valueOf()

One of the ameliorate ways to convert an int primitive to the Long object is past times using the valueOf() method of the Long class. If you lot utilisation Long.valueOf() thence you lot don't take away to explicitly cast an int to long, Why? because you lot tin overstep an int value to a method which is accepting primitive long.

Btw,  if you lot are non familiar amongst such key rules of Java programming, I propose taking the  Long is too Immutable inwards Java.  So, it's rubber to percentage the same instance of the Long object betwixt multiple clients.

That's all nearly how to convert a primitive int value to a Long object inwards Java. It's slowly but worth knowing the subtleties involved. Prefer Long.valueOf() to bring payoff of caching it provides equally good equally to avoid casting.

Also, scream upward that auto-boxing volition throw NPE if you lot desire to convert a Long object dorsum to long primitive value in addition to it happened to last null. This variety of NPE is sometimes non obvious in addition to takes a long fourth dimension to discover itself.


Further Learning
The Complete Java Masterclass - Update for Java 11
Top five Java Programming courses for beginners
3 books to acquire Java from scratch inwards 2018


If you lot similar this tutorial in addition to interested inwards learning nearly information type conversion inwards Java, depository fiscal establishment check out these amazing tutorials :
  1. How to convert Map to List inwards Java (tutorial)
  2. How to convert Double to Long inwards Java? (tutorial)
  3. How to convert Array to String inwards Java? (tutorial)
  4. 5 Ways to convert Java 8 Stream to List? (solution)
  5. How to convert a binary release to decimal inwards Java? (tutorial)
  6. 5 examples of converting InputStream to String inwards Java? (tutorial)
  7. How to convert Character to String inwards Java? (tutorial

Thanks for reading this tutorial, if you lot similar this article thence delight percentage amongst your friends in addition to colleagues, it makes a lot of difference.

0 Response to "How To Convert Primitive Int Value To A Long Object Inwards Java?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel