Please comment, vote on the idea and suggest on this sample code

//@Compiled with javac -version 1.6.0_01-ea

//@Executed with the details as below:

/*

java version "1.6.0_01-ea"

Java(TM) SE Runtime Environment (build 1.6.0_01-ea-b01)

Java HotSpot(TM) Client VM (build 1.6.0_01-ea-b01, mixed mode, sharing)

on Microsoft Windows XP PC over Intel Celeron Processor.

*/

class GenericTest{

publicstaticvoid main(String args[]){

//How to explicity specify the Float as U in constructor rather than recognizing by inference

//Something like the below commented statement is giving an error.

//AClass<Integer, String> aClass = new <Float>AClass<Integer, String>(30, "Hello World", 3.14);

AClass<Integer, String> aClass =new AClass<Integer, String>(30,"Hello World", 3.14);

//The explicit type parameter passing to method is expected

//to be as similar to the constructor call. Please refer to the comments near to the

//method defintion. The below commented statement is giving an error.

//aClass.showThis<Character, String>('?);

aClass.<Character, String>showThis('?);

aClass.display();

}

}

/*class AClass<S extends Integer,T> {*/

//JLS (Java Language Specification) says that S can be replaced by here Integer

//(as the super-class) or any class that is a sub-class of Integer

//where Integer is an inclusive upper limit.

//But here it is not working. We have to use Number which is one class level above.

class AClass<Sextends Number,T>{

S s;

T t;

public <U, V> AClass(S s, T t, U u){

this.s = s;

this.t = t;

/*V v = "One String";*/ V v =null;

System.out.println("Inside Constructor to display value - " + u);

System.out.println("Inside Constructor to display value - " + v);

}

//Generic method should look must be similar to generic class syntax construct

//The type parameter tag must come after the method name token as expected in the

//generic class syntax construct. But the below commented statement is giving an error.

//void showThis<P, Q extends String>(P p){

<P, Qextends String>void showThis(P p){

//JLS (Java Language Specification) doesn't state for cases where inference

//tries to happens in the method body. So the below commented statement is

//not getting inferred correctly, even though if we explicity specify the

//type parameter at the callee. This seems that the type parameter specification

//at the calle is no way giving any advantage by any chance.

/*Q q = "A String";*/ Q q =null;// Only null assignment allowed.

System.out.println("Showing This " + p);

System.out.println("showThis()::q=" + q);

}

void display(){

System.out.println("this.s = " + this.s +" this.t = " + this.t);

}

}

[4742 byte] By [Thomas.Mathewa] at [2007-11-27 6:11:44]
# 1

I vote you re-post the code, but remove those comments from it and put them in a paragraph where they belong! Reading that made me fall into a mild coma, which is annoying because for once my girlfriend is up on a Saturday morning and we can go to breakfast.

No offence, just try and make things more readable, and people will read it :-)

georgemca at 2007-7-12 17:18:14 > top of Java-index,Core,Core APIs...
# 2
Agreed. I tried to read it too, and gave up because of all the comments.
DrClapa at 2007-7-12 17:18:14 > top of Java-index,Core,Core APIs...
# 3
Have a look at this Java-DOC tutorial: http://java.sun.com/j2se/javadoc/writingdoccomments/
prometheuzza at 2007-7-12 17:18:14 > top of Java-index,Core,Core APIs...
# 4

Hi All,

First of all utterly sorry to all for the spaghetti code; I did it in a hurry. I tried to search on editing the original post, but I couldn't find a link. However, I shall summarize the above posted code. I have a set of doubts as well as suggestions in Generics. That is what I put all together into that code. I shall list out those and shall post as separate Threads.

1. How a Generic Constructor be called in a Generic Class?

2. Suggestion on Type Parameter passing syntax to a Generic Method/Constructor

3. Bounded types and Autoboxing

4. Usage of Type Parameter in Generic Method's body

Thomas.Mathewa at 2007-7-12 17:18:14 > top of Java-index,Core,Core APIs...
# 5

> //How to explicity specify the Float as U in constructor rather than recognizing by inference

> //Something like the below commented statement is giving an error.

> //AClass<Integer, String> aClass = new <Float>AClass<Integer, String>(30, "Hello World", 3.14);

this is not working because:

1) you need 2 types for the construcotr: public <U, V> AClass(S s,...

2) 3.14 is double and not float

this one should work...=new <Double,Number>AClass<Integer, String>(30,"Hello World", 3.14);

> /*class AClass<S extends Integer,T> {*/

> //JLS (Java Language Specification) says that S can be replaced by here Integer

> //(as the super-class) or any class that is a sub-class of Integer

> //where Integer is an inclusive upper limit.

> //But here it is not working. We have to use Number which is one class level above.

using Integer as upper limit is not an error, should be only a warning because Integer is final and cannot be further extended.

(using eclipse 3.2.0 buildId=I20070222-0951 with JDK 1.6.0-b105)

> //Generic method should look must be similar to generic class syntax construct

> //The type parameter tag must come after the method name token as expected in the

> //generic class syntax construct. But the below commented statement is giving an error.

> //void showThis<P, Q extends String>(P p){

Why should it look similar?

JLS 8.1:NormalClassDeclaration:

ClassModifiersopt class Identifier TypeParametersopt Superopt Interfacesopt

JLS 8.4: MethodHeader:

MethodModifiersopt TypeParametersopt ResultType MethodDeclarator

> //JLS (Java Language Specification) doesn't state for cases where inference

> //tries to happens in the method body. So the below commented statement is

> //not getting inferred correctly, even though if we explicity specify the

> //type parameter at the callee. This seems that the type parameter specification

> //at the calle is no way giving any advantage by any chance.

> /*Q q = "A String";*/

> Q q = null; // Only nullll assignment allowed.

? if the JLS doesn't state it, maybe it should not happen... ?

And it should it work?class Bsub extends Asuper { }

...

<Q extends Asuper>void test() {

Q q = new Asuper(); // works only if Q is Asuper, but not if Q is Bsub

hope that helps...

[]

S_i_m_ua at 2007-7-12 17:18:14 > top of Java-index,Core,Core APIs...