Friday, November 8, 2019

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

Suppose you lot receive got an int variable but the business office 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 besides involve a Long object, non simply the long primitive value. Now, the work is reduced to converting a long primitive to Long object, which is non actually a work if you lot are running on JRE version higher than Java 5. But, sometimes autoboxing is non efficient e.g. when you lot receive got 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 yesteryear using static manufacturing flora methods similar Long.valueOf(long l), which was originally meant to convert long primitive values to a Long object, prior to Java 5, but, besides plant fine alongside int primitive.

So, if you lot receive got got an int primitive value, simply top it to Long.valueOf() as well as you lot volition acquire a Long object alongside same value.

Btw, This entirely plant good if the method is expecting a long primitive value, if it's expecting a Long object as well as then you lot get-go involve to cast an int to long, thence that autoboxing tin reach the axe kick-in (see NullPointerException while converting a zippo Long object to long primitive. So you lot should beware of that. Now that you lot know around concepts, let's come across this conversion inwards action.




int to Long - using autoboxing

In lodge 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 iii = (long) 3; // plant fine

Same is truthful for a method which is expecting a Long object. You cannot top 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, as well as then auto-boxing volition convey help 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 role 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 affair to respect inwards the inwards a higher house code is that nosotros are using auto-boxing inwards the loop, which tin reach the axe drive functioning effect because a lot of Long objects volition last created as well as discarded.

In our case, it is nonetheless ok because the loop is running entirely ten times, but if receive got to do this conversion country a 1000000 times, this approach volition drive a problem, equally explained yesteryear Joshua Bloch inwards his classic book, Effective Java tertiary Edition. Influenza A virus subtype H5N1 must-read for whatever Java developer.




int to Long - using Long.valueOf()

One of the ameliorate ways to convert an int primitive to the Long object is yesteryear using the valueOf() method of the Long class. If you lot role Long.valueOf() as well as then you lot don't involve to explicitly cast an int to long, Why? because you lot tin reach the axe top an int value to a method which is accepting primitive long.

Btw,  if you lot are non familiar alongside such key rules of Java programming, I advise taking the  Long is besides 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 convey payoff of caching it provides equally good equally to avoid casting.

Also, yell upwards that auto-boxing volition throw NPE if you lot desire to convert a Long object dorsum to long primitive value as well as it happened to last null. This sort of NPE is sometimes non obvious as well as takes a long fourth dimension to divulge itself.


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


If you lot similar this tutorial as well as interested inwards learning nearly information type conversion inwards Java, banking corporation 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 position out 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 as well as then delight percentage alongside your friends as well as colleagues, it makes a lot of difference.

No comments:

Post a Comment