class Test { // A Generic method example static <T> void genericDisplay(T element) { System.out.println(element.getClass().getName() + " = " + element); } // Driver method public static void main(String[] args) { // Calling generic method with Integer argument genericDisplay(11); // Calling generic method with String argument genericDisplay("Java Lab"); // Calling generic method with double argument genericDisplay(1.0); }}
Question
class Test { // A Generic method example static <T> void genericDisplay(T element) { System.out.println(element.getClass().getName() + " = " + element); } // Driver method public static void main(String[] args) { // Calling generic method with Integer argument genericDisplay(11); // Calling generic method with String argument genericDisplay("Java Lab"); // Calling generic method with double argument genericDisplay(1.0); }}
Solution
This is a Java program that demonstrates the use of a generic method. Here's a step-by-step explanation:
-
The class
Testis defined. -
Inside the
Testclass, a static generic method namedgenericDisplayis defined. This method takes a single argument of any type (denoted by<T>), and it prints the class name of the argument along with the argument itself. -
The
mainmethod is defined. This is the entry point of the Java program. -
Inside the
mainmethod, thegenericDisplaymethod is called three times with different types of arguments: an Integer, a String,
Similar Questions
What is the output of the following program:class Test { // A Generic method example static <T> void genericDisplay(T element) { System.out.println(element.getClass().getName() + " = " + element); } // Driver method public static void main(String[] args) { // Calling generic method with Integer argument genericDisplay(11); // Calling generic method with String argument genericDisplay("Java Lab"); // Calling generic method with double argument genericDisplay(1.0); }}Select one:a. Compilation errorb.11Java Lab1.0c. No outputd.java.lang.Integer = 11java.lang.String = Java Labjava.lang.Double = 1.0
class Test<T> { // An object of type T is declared T obj; Test(T obj) { this.obj = obj; } // constructor public T getObject() { return this.obj; }} // Driver class to test abovepublic class Main { public static void main(String[] args) { // instance of Integer type Test<Integer> iObj = new Test<Integer>(15); System.out.println(iObj.getObject()); // instance of String type Test<String> sObj = new Test<String>("Java Lab"); System.out.println(sObj.getObject()); }}Select one:a. 15Java Labb. 15c. Java Labd. Compilation Error
How do you declare a generic method in Java?Question 12Answera.<T> returnType methodName(T arg)b.returnType methodName<T>(T arg)c.returnType<T> methodName(T arg)d.<T> methodName(returnType arg)
class Test { public static void main(String args[]) { System.out.println(fun()); } static int fun() { static int x= 0; return ++x; }}
What is the output of the following program:class Test<T> { // An object of type T is declared T obj; Test(T obj) { this.obj = obj; } // constructor public T getObject() { return this.obj; }} // Driver class to test abovepublic class Main { public static void main(String[] args) { // instance of Integer type Test<Integer> iObj = new Test<Integer>(15); System.out.println(iObj.getObject()); // instance of String type Test<String> sObj = new Test<String>("Java Lab"); System.out.println(sObj.getObject()); }}Select one:a. 15b. Java Labc. Compilation Errord. 15Java Lab
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.