About Question enthuware.ocpjp.v8.2.1860 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
mrmuiz
Posts: 49
Joined: Mon Jul 27, 2015 4:34 am
Contact:

About Question enthuware.ocpjp.v8.2.1860 :

Post by mrmuiz »

I do not agree with this answer: IntFunction does not "avoid additional cost associated with auto-boxing/unboxing" by virtue of its own nature. It's developer's responsability making a good use of Function or IntFunction, both of them can cause auto-boxing/unboxing if misused

Code: Select all

		// bad use
		
		new IntFunction<String>(){
			@Override
			public String apply(int value) {
				return Integer.toString(value);
			}
		}.apply(new Integer(1));
		
		new Function<Integer,String>(){
			@Override
			public String apply(Integer value) {
				return value.toString();
			}
		}.apply(1);
or avoid it if properly used

Code: Select all

		// good use
		
		new IntFunction<String>(){
			@Override
			public String apply(int value) {
				return Integer.toString(value);
			}
		}.apply(1);
		
		new Function<Integer,String>(){
			@Override
			public String apply(Integer value) {
				return value.toString();
			}
		}.apply(new Integer(1));
I'd simply say that IntFunction accepts primitive integers.

admin
Site Admin
Posts: 10402
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1860 :

Post by admin »

Yes, it is possible to write bad code while using IntFunction as well, but the whole reason it exists is to avoid boxing/unboxing cost when dealing with primitives. Otherwise, Function was enough.

The option has been updated to "It avoids additional cost associated with auto-boxing/unboxing while dealing with primitive ints."

I hope that makes it more clear.

thank you for your feedback!
Paul.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests