The syntax of generic classes and methods was not added until version 5.0 of Jav
ID: 3586847 • Letter: T
Question
The syntax of generic classes and methods was not added until version 5.0 of Java. Nonetheless, early versions of the language had collections classes such as an array list, or a linked list which were able to store objects, instances of different classes.
a. How were those classes implemented without the generic syntax?
b. Give an example of a program that uses the non-generic version of a class and the equivalent program that uses the generic version. How do the two implementations differ?
c. Why is having the syntax of generics better? Explain and illustrate with a method of the example class.
Explanation / Answer
a)
Ans)
Now I am going to explain about how the classes implemented without the generic syntax when the syntax of generic classes and methods was not added until version 5.0 of Java.
Explanation:
1. Before generics came in to picture then we can store any type of java objects in to the collection which is non-generic but now because of this generics it will force the programmer to store only specific type of objects
for example:
List list = new ArrayList();
list.add("hello everyone welcome");
2. before generics came in to the picture then we need to type cast but now after generics it is not necessary to typecast.
3. so in this way the classes implemented without the generic syntax.
b)
Ans)
Now I am going to give an example of a program that uses the non-generic version of a class and the equivalent program that uses the generic version
Non-generic version code:
The code for the non-generic one is given below where typecasting is required in non-generic version
import java.util.*;
class NonGeneric
{
public static void main(String args[])
{
ArrayList list=new ArrayList();
list.add("jay");
list.add("prakash");
String f=(String)list.get(1);//type casting is required
System.out.println("The element is: "+f);
Iterator itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Generic version code:
The code for the generic one is given below where typecasting is not required in generic version
import java.util.*;
class Generic
{
public static void main(String args[])
{
ArrayList<String> list=new ArrayList<String>();
list.add("jay");
list.add("prakash");
String f=list.get(1);//type casting is not required here in generic code
System.out.println("element is: "+f);
Iterator<String> itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
c)
Ans)
Now I am going to explain about Why having the syntax of generics is better
Explanation:
the syntax of having generics is better because we can store only that type of elemnts in to it and if we dont have that syntax then it will store all and different types of data in to it so which is not a good practise so they have given that generic syntax.
because of storing only specific type of values the generic syntax is very better compared to non-generic syntax.
The example class ig given below:
import java.util.*;
class Generic
{
public static void main(String args[])
{
ArrayList<String> list=new ArrayList<String>();
list.add("jay");
list.add("prakash");
String f=list.get(1);//type casting is not required here in generic code
System.out.println("element is: "+f);
Iterator<String> itr=list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Hope This Helps, if you have any doubts Please comment i will get back to you, thank you and please thumbs up
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.