This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin Cookbook and have Ken Kousen on-line!
See this thread for details.
Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Whats the difference between IntFunction and Function<Integer,Integer>

 
Ranch Hand
Posts: 133
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am practising questions in enthuware for IZO 809 . In that he says that the use of IntFunction over Function is in regard to cost associated with auto-boxing. Could any one please help me in showing an example .

Many Thanks,

Swapna
 
Marshal
Posts: 67036
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can find an actual use for that sort of code, do tell us If it takes more than three weeks to run, reduce the 0x0000_0001_0000_0000L by a few log units.
 
Sheriff
Posts: 9657
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swapna,

As you might already be aware of, Java has primitive types which are not objects. Due to that primitives cannot be used in things like Generics. Since Java has auto-boxing, you might not notice when your primitive values are converted into the wrapper classes like Integer. For example:


In the above code the 100 value will be auto-boxed to Integer to put it into the Map. The above code is the same as writing this:

Just like a Map, when you are using a Function, there will be auto-boxing and unboxing even when you want to use primitive datatypes. So for example if you have a function which takes a number and returns it as a String, you may use it something like this:

As you might've guessed, when you call this asString function with val, the val gets converted to Integer object and is passed to the function. So the code actually works something like this:

To avoid this boxing (and in some cases unboxing), Java has Function classes which support primitive types. There are primitive Functions, Operators, Suppliers and Consumers to avoid this boxing/unboxing. So instead of using a Function, if I use an IntFunction, I can avoid this boxing:

There will be no auto-boxing in this case as the IntFunction interface's apply method actually takes primitive int.

As a side note, there are collections to avoid the auto-boxing/unboxing which we saw in the case of the HashMap. For example Object2IntOpenHashMap takes and returns the value as primitive int to avoid boxing/unboxing.
 
Swapna latha
Ranch Hand
Posts: 133
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ankit,

Best Regards,

Swapna

Ankit Garg wrote:Hi Swapna,

As you might already be aware of, Java has primitive types which are not objects. Due to that primitives cannot be used in things like Generics. Since Java has auto-boxing, you might not notice when your primitive values are converted into the wrapper classes like Integer. For example:


In the above code the 100 value will be auto-boxed to Integer to put it into the Map. The above code is the same as writing this:

Just like a Map, when you are using a Function, there will be auto-boxing and unboxing even when you want to use primitive datatypes. So for example if you have a function which takes a number and returns it as a String, you may use it something like this:

As you might've guessed, when you call this square function with val, it gets converted to Integer object, the function is called, and then the Integer value is unboxed to int. So the code actually works something like this:

To avoid this boxing (and in some cases unboxing), Java has Function classes which support primitive types. There are primitive Functions, Operators, Suppliers and Consumers to avoid this boxing/unboxing. So instead of using a Function, if I use an IntFunction, I can avoid this boxing:

There will be no auto-boxing in this case as the IntFunction interface's apply method actually takes primitive int.

As a side note, there are collections to avoid the auto-boxing/unboxing which we saw in the case of the HashMap. For example Object2IntOpenHashMap takes and returns the value as primitive int to avoid boxing/unboxing.

 
Everybody's invited. Even this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!