| Author |
Generic Classes question
|
Suvojit Chakraborty
Ranch Hand
Joined: Nov 10, 2009
Posts: 59
|
|
Following question is from book SCJP6 by Richard F. Raposa:
What is the result of the following program?
A. hi
B. hi followed by a runtime exception
C. hithere
D. Compiler error on line 9
E. Compiler error on line 14
I chose Option E but the ans says Option C.
Why so???(It has explanation which I fail to understand and I am willing to type it only if someone asks)
|
 |
Sridhar Santhanakrishnan
Ranch Hand
Joined: Mar 20, 2007
Posts: 317
|
|
|
Why do you think there would be a compiler error?
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
might be this line, missing " around hi .
|
 |
Rajeev Rnair
Ranch Hand
Joined: Mar 22, 2010
Posts: 308
|
|
Seetharaman Venkatasamy wrote:
might be this line, missing " around hi .
class Hello<T> is having a constructor which takes an argument of type T. This particular case you are creating an instance of Hello<String>, so you can pass a String argument to the constructor. For example, you can create
which will assign
and the hello.toString() will print the hello.t, which is "hi"
|
SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Not only for String, but for all objects, see the code below....
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Suvojit Chakraborty
Ranch Hand
Joined: Nov 10, 2009
Posts: 59
|
|
Sridhar Santhanakrishnan wrote:Why do you think there would be a compiler error?
I think its a violation to have this statement in code:
because the generic class expect a type
If you notice, we need to pass a type T so that the instance variable t and the local variable in const can have a type.
but when we say "System.out.print(new Hello("there"));" we are not suppluying the type.
|
 |
Suvojit Chakraborty
Ranch Hand
Joined: Nov 10, 2009
Posts: 59
|
|
Hi Abimaran,
I execute your code. It compiled but generated 2 warnings in following lines:
Output:
D:\Education\Java\JavaStudyRoom>javac -Xlint Hello.java
Hello.java:13: warning: [unchecked] unchecked call to Hello(T) as a member of the raw type Hello
System.out.println(new Hello("There"));
^
Hello.java:14: warning: [unchecked] unchecked call to Hello(T) as a member of the raw type Hello
System.out.println(new Hello(new Dog("ABI's")));
^
2 warnings
So I guess this is also a case of mixing Generics and non Generics.
Thank you so much for your code. Now I am clear with the answer.
Suvojit
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Suvojit Chakraborty wrote:Hi Abimaran,
I execute your code. It compiled but generated 2 warnings in following lines:
Output:
D:\Education\Java\JavaStudyRoom>javac -Xlint Hello.java
Hello.java:13: warning: [unchecked] unchecked call to Hello(T) as a member of the raw type Hello
System.out.println(new Hello("There"));
^
Hello.java:14: warning: [unchecked] unchecked call to Hello(T) as a member of the raw type Hello
System.out.println(new Hello(new Dog("ABI's")));
^
2 warnings
So I guess this is also a case of mixing Generics and non Generics.
Thank you so much for your code. Now I am clear with the answer.
Suvojit
Exactly! But warning is not Compilation Error!
You are Welcome!
|
 |
 |
|
|
subject: Generic Classes question
|
|
|