- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a constructor reference for an array in Java?
A constructor reference is similar to method reference except that the name of a method is new. We can also create a constructor reference with an array type. For instance, if we need to create an integer array by using the constructor reference: int[]:: new, where the parameter is a length of an array.
Syntax
ArrayTypeName[]::new
Example
@FunctionalInterface interface ArrayCreator { int[] makeArray(int number); } public class ArrayConstructorRefTest { public static void main(String[] args) { ArrayCreator arrayCreator = int[]::new; // Constructor Reference for an Array int[] intArray = arrayCreator.makeArray(10); for(int i = 0; i < intArray.length; i++) { intArray[i] = i * i - i / 2; System.out.println("[" + i + "] = " + intArray[i]); } } }
Output
[0] = 0 [1] = 1 [2] = 3 [3] = 8 [4] = 14 [5] = 23 [6] = 33 [7] = 46 [8] = 60 [9] = 77
- Related Articles
- Differences between Method Reference and Constructor Reference in Java?
- How to create a parameterized constructor in Java?
- How to create a default constructor in Java?
- How to implement a constructor reference with one or more arguments in Java?
- How to create an Array in Java?
- How to create a thread using method reference in Java?\n
- Reference to a constructor using method references in Java8
- How to create an ArrayList from an Array in Java?
- How to pass an array by reference in C++
- How to create an array of Object in Java
- What are the rules to create a constructor in java?
- Where and how can I create a private constructor in Java?
- How to create LabelValue Tuple from an array in Java
- How to create an array of linked lists in java?
- How to declare a constructor in Java?

Advertisements