英文:
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[]
和int
的IntStream stream(int[] array)
方法,没有涉及任何Integer
。forEach
使用了处理int
的IntConsumer
。而且字符串连接似乎也没有涉及装箱(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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论