Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Classes question

 
Suvojit Chakraborty
Ranch Hand
Posts: 66
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think there would be a compiler error?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


might be this line, missing " around hi .
 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not only for String, but for all objects, see the code below....
 
Suvojit Chakraborty
Ranch Hand
Posts: 66
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic