One of my readers asked me about the difference between ArrayList vs ArrayList< in Java?>, which was actually asked to him in a recent Java development interview. The key difference between them is that ArrayList is not using Generics while ArrayList is a generic ArrayList but they look very similar. If a method accepts ArrayList or ArrayList<?> as a parameter then it can accept any type of ArrayList like ArrayList of String, Integer, Date, or Object, but if you look closely you will find that one is raw type while the other is using an unbounded wildcard. What difference that could make? Well, that makes a significant difference because ArrayList with raw type is not type-safe but ArrayList<?> with the unbounded wildcard is type-safe.
Friday, October 22, 2021
Wednesday, August 4, 2021
How to write parametrized Generic class and method in Java Example
Parameterized Generic class and method in Java
Writing Generic parametrized classes and methods in Java is easy and should be used as much as possible. Generic in Java was introduced in version 1.5 along with Autoboxing, Enum, varargs and static import. Most of the new code development in Java uses type-safe Generic collection i.e. HashSet in place of HashSet but still Generic is underused in terms of writing its own parametrized classes and method. I agree that most Java programmers have started using Generic while working with the Java collection framework but they are still not sure how Generic can allow you to write Template kind of classes which can work with any Type just like the parametrized ArrayList in Java which can store any type of element.
Thursday, July 29, 2021
10 Interview Questions on Java Generics for Programmer and Developers
Generic interview questions in Java interviews are getting more and more common with Java 5 around there for a considerable time and many applications either moving to Java 5 and almost all new Java development happening on Tiger(code name of Java 5). The importance of Generics and Java 5 features like Enum, Autoboxing, varargs, and Collection utilities like CountDownLatch, CyclicBarrier, and BlockingQueue is getting more and more popular on Java interviews. Generic interview questions can get really tricky if you are not familiar with bounded and unbounded generic wildcards, How generics works internally, type erasure and familiarity with writing parametrized generics classes and methods in Java.
Subscribe to:
Posts
(
Atom
)