流在打印整数数组值时是否使用装箱和拆箱?

huangapple go评论68阅读模式
英文:

Does streams use Boxing and unboxing when print array of int values

问题

以下是您要翻译的内容:

如果我运行这行代码:

Arrays.stream(new int[] {2, 4, 6, 8, 10}).forEach(x -> System.out.print(x + " "));

这些值是否会发生装箱或拆箱?

英文:

if i run this line of code :

Arrays.stream(new int[] {2, 4, 6, 8, 10}).forEach(x->System.out.print(x+ " " ));

Is there any boxing or unboxign happens on the values?

答案1

得分: 2

不,这里没有发生自动装箱/拆箱。你调用了处理int[]intIntStream stream(int[] array)方法,没有涉及任何IntegerforEach使用了处理intIntConsumer。而且字符串连接似乎也没有涉及装箱(https://stackoverflow.com/questions/12445064/string-concatenation-and-autoboxing-in-java),因为使用了StringBuilder.append(int)

英文:

No, there is no auto-boxing / auto-unboxing happening. You invoke IntStream stream(int[] array) which deals with int[] and int without having any Integer involved. The forEach gets an IntConsumer which deals with int only. And the string concatenation does not appear to involve boxing either (https://stackoverflow.com/questions/12445064/string-concatenation-and-autoboxing-in-java) since StringBuilder.append(int) is used.

huangapple
  • 本文由 发表于 2020年9月20日 18:02:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63977722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定