• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic Classes question

 
Ranch Hand
Posts: 66
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think there would be a compiler error?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


might be this line, missing " around hi .
 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic