• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

generics doubt

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


java the complete reference 8th edition
page no. 334
topic - Bounded Types



there is a compile time error at the point where I have written saying " cannot find doubleValue() and the book has given the reason for this :

compiler has no way to know that you are intending to create Gen object using only numeric types


I have underlined the last few words because here is my question
Gen<Integer> g1=new Gen<Integer>(ar);
here we have declared the type parameter as Integer, from which compiler should get to know that type is Integer and doubleValue() is a valid method for this type !
compiler should complaint when we are using type parameter other than Numeric ! like Gen<String> g1=new Gen<String>(ar);
or the compiler is made smart enough so that no risk is taken for the condition where a programmer could write such incorrect code ??
 
Ranch Hand
Posts: 209
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming you have defined a Dog class, when the compiler compiles the Gen class, how is it supposed to know that you do not intend to do something like this?


doubleValue() is defined in the Number class, so you could declare Gen like this:



and then all is good.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Hayward wrote:Assuming you have defined a Dog class, when the compiler compiles the Gen class, how is it supposed to know that you do not intend to do something like this?


compiler can see what I have written and complaints if I have written incorrect . But answer to your point is, Compiler get to know about incorrect if I write the incorrect one. Gen<Dog> d = new Gen<Dog>(dogs); is a incorrect code and compiler should complaint only if I have written this line of code but why does compiler complaint even if I have not written this statement in my whole program code ?
I have written Gen<Integer> d = new Gen<Integer>(); which perfect for that method , then why the compiler is predicting the case where I can write the incorrect one...why such prediction by compiler even if there is no such incorrect code ???
 
Richard Hayward
Ranch Hand
Posts: 209
13
VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if the source code for the Gen class were in the file Gen.java, but I wrote it, not you? All I gave you was the Gen.class file to use.
You then write the TestGen class in TestGen.java.

When I wrote the Gen class, I didn't know that when you later use it, you are only going to make Gen<Integer> instances.


 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:
I have written Gen<Integer> d = new Gen<Integer>(); which perfect for that method , then why the compiler is predicting the case where I can write the incorrect one...why such prediction by compiler even if there is no such incorrect code ???



First, the code is *not* correct... you just happens to be using it in a way that won't break it. This does not make the code correct.

Second, let's take Richard example to an extreme. Let's say you compile and give the generic class bytecode to Richard -- after all, from your argument, the code should compile right? Richard then uses your compiled class, but for his Dog class, then what? Should the compiler somehow go back to tell you that your class won't compile? And if so, how?

Henry
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
Second, let's take Richard example to an extreme. Let's say you compile and give the generic class bytecode to Richard -- after call, from your argument, the code should compile right? Richard then uses your compiled class, but for his Dog class, then what? Should the compiler somehow go back to tell you that your class won't compile? And if so, how?
Henry


so, in short the compiler take care for generic class so that no one can use in incorrectly(In future if someone is using that generic class) by giving compilation error.... right ??

ok let me answer myself... inside the class Gen<T> T[] o; is declared and at compile time the type parameter is unknown ... so compiler cant find the method doubleValue()... right ??
and using the upper-bound for type parameter T, compiler gets sure that aahhh it's ok, T is type of NUmber and found the doubleValue()..... Right ??
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:
so, in short the compiler take care for generic class so that no one can use in incorrectly(In future if someone is using that generic class) by giving compilation error.... right ??



I don't know why you are treating generics as a special case. This is how the compiler behaves for practically everything. For examples...

Example 1. Create a method that takes an Object parameter, and try to call the doubleValue() method of the object parameter (without casting, of course). The compiler will not let you do it, even if you only pass Number instances to the method.

Example 2. Create an Object reference variable, and try to call the doubleValue() method using that reference (without casting, of course). The compiler will not let you do it, even if you only assign Number instance to the variable.

And there are likely many other examples. So, why the need to think that generics are treated otherwise?

Henry
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok ok... now i understood ..
thank you henry, I guess I was messed up in some points
one more thing to ask,
at compile time, the Type Parameter <T> is unknown or the Object type ??
 
Ranch Hand
Posts: 34
2
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gonna try to answer the OP last question, someone correct me if I'm wrong, as I'm just going through Generics for the first time these days.

For a generic class which uses an unbounded type parameter, say T

on compilation, the Java compiler replaces all occurrences of T with Object:

For a generic class that uses a bounded type parameter:

the compiler replaces all occurrencs T with its first bound class (String in this case):


Hope to have put it right...

Cheers,
Marco
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello marco,
I don't think so
try these commands for running your code

javac Gen.java

javap -c Gen


and see what compiler had done to your code !
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:
at compile time, the Type Parameter <T> is unknown or the Object type ??



I guess it depends on which compile time. While compiling the Gen class, the T is, of course, unknown. While compiling the code that uses the Gen class, the generic is known as defined by the reference. And this knowledge can be lessened with the use of wildcards. etc.

Puspender Tanwar wrote:hello marco,
I don't think so



This side discussion is actually related to something else. The byte code that is generated does indeed has the type erased. This is done for backward compatibility purposes. However, this does not mean that the compiler treats the type as Object during compilation -- it is simply how the code is generated.


Oh, and BTW, generics and arrays don't work very well together. It is probably a good idea to avoid mixing them.

Henry
 
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic