Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Download 1.52 Mb.
Pdf ko'rish
bet86/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   82   83   84   85   86   87   88   89   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

227.
a.
b.
228.
a.
CHAPTER
04
Method References
What is a method reference? What are the benefits of
method references?
Answer:
A method reference is a new operator introduced by Java 8. It is
a shortcut operator for a lambda expression. It is represented by
the::symbol.
Method references have several benefits as follows:
They avoid code duplication: Sometimes, your code may
already have a method that has the same code that you
would like to specify in a lambda expression. In such a
scenario method references are useful. So instead of re–
writing the code again in the lambda expression, you can
refer to the existing method via a method reference.
They make code concise: Method references are a step
further from lambda expression. They help to make the code
even more concise by moving the code from the lambda
expression into a separate method.
What are the different types of method references?
Answer:
There are 4 types of method references. These are as follows:
Static method reference: This occurs when a static method
of a class is referenced via the method reference operator. Its
syntax is as follows:


b.
c.
d.
229.
class::staticmethod
Instance method reference: This occurs when an instance
method of a class is referenced via the method reference
operator. Its syntax is as follows:
object::instancemethod
Constructor reference: This occurs when a constructor of a
class is referenced via the method reference operator. Its
syntax is as follows:
class::new
Arbitrary method reference: This occurs when an instance
method of a class is accessed but not on any specific object.
Its syntax is as follows:
class::instancemethod

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   82   83   84   85   86   87   88   89   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling