|
||||||||
Java 9 @SafeVarargs Annotation Enhancement Posted: April 1, 2023 @ 3:34 am |
@SafeVarargs annotation was introduced in Java 7. @SafeVarargs annotation is used suppressed compiler warnings related to Heap pollution. Heap pollution has occurred when one variable points to any other type of object then the compiler gives a warning message related to this Heap pollution. Until Java 8 @SafeVarargs annotation is used in the constructor, static methods and final methods. But Java 9 @SafeVarargs annotation is used in the private methods also. This is a small enhancement in Java 9 @SafeVarargs annotation. Table of content1. What is Varargs in Java? 1. What is Varargs in Java?Variable Arguments(Varargs) in Java is a method argument that takes any number of values of that declared type. 2. How to declare the Varargs method in Java?Syntax of Varargs method in Java: 3. Need of Varargs in JavaUntil Java 4 we can’t pass any number of values in already created methods. We need to create a new method with the same number of arguments in the program. This will increase the length of the code of the program and reduce the readability of the code. So Java resolves this problem to introduce the varargs feature in Java 5. 4. Java 9 @SafeVarargs annotation exampleExample without using @SafeVarargs annotationOutput Example with using @SafeVarargs annotationOutput 5. Java 8 @SafeVarargs with private method annotation exampleOutput 6. ConclusionIn this topic, we learnt about @SafeVarargs annotation, Java 9 @SafeVarargs annotation enhancement, what is varargs in java, the need for varargs in java and example of @SafeVarags in Java 9 and Java 8
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|