participate


Generics - generics 2.0 : A bug ,or I am doing something wrong ?
<<   Back to Forum  |   Give us Feedback
4 Duke Stars available
This topic has 11 replies on 1 page.
veenurs
Posts:53
Registered: 1/22/01
generics 2.0 : A bug ,or I am doing something wrong ?   
Jun 10, 2003 11:30 PM

 
Downloaded generic 2.0 .
Yup, I'm quite new to generics, but it definitly lets me use all the additions, unlike the old compiler.
But I have two questions , and I think this may be as good a place to ask as any other-
1. Is there an API documnetation for tiger ?
2. why doesn't this work ? -
           Double myFunction( MyObject mo )
       {
        if(xyz) return myHashMap.get(mykey) // (A)
        else return -1.00 ; //(B)
       }
      


Where (A) : values in myHashMap are Double,Keys are members defined in enum .This line compiles but throw a run-time error .
I have to write
 Double d=myHashMap.get(mykey); return d;
       

(B)Does not compile .Shouldn't autoboxing take care of it?
 
we6junkie
Posts:156
Registered: 6/5/03
Re: generics 2.0 : A bug ,or I am doing something wrong ?   
Jun 11, 2003 4:26 AM (reply 1 of 11)  (In reply to original post )

 
Hmm..am not really sure what "autoboxing" means ..are you referring to auto upcasting or something ?

Well you would need to caste it (coz hashmap would store it as an object and not as a double..I mean the particular class information is lost).Hence when you are retrieving , you need to cast it properly or you declare the return type as Object.
And also when you are returning -1.00 , compiler will complain , coz it doesnt expect a primitive...but an object.

thx.
 
we6junkie
Posts:156
Registered: 6/5/03
Re: generics 2.0 : A bug ,or I am doing something wrong ?   
Jun 11, 2003 4:26 AM (reply 2 of 11)  (In reply to original post )

 
Hmm..am not really sure what "autoboxing" means ..are you referring to auto upcasting or something ?

Well you would need to cast it (coz hashmap would store it as an object and not as a double..I mean the particular class information is lost).Hence when you are retrieving , you need to cast it properly or you declare the return type as Object.
And also when you are returning -1.00 , compiler will complain , coz it doesnt expect a primitive...but an object.

thx.
 
YATArchivist_sn
Posts:49
Registered: 6/6/03
Re: generics 2.0 : A bug ,or I am doing something wrong ?   
Jun 11, 2003 5:06 AM (reply 3 of 11)  (In reply to #2 )

 
we6junkie, with all due respect, you don't seem competent to answer questions in this particular forum. Read the documentation on generics and autoboxing - there are links one or two of the six or so most recent threads.
 
we6junkie
Posts:156
Registered: 6/5/03
Re: generics 2.0 : A bug ,or I am doing something wrong ?   
Jun 11, 2003 5:10 AM (reply 4 of 11)  (In reply to #3 )

 
Hi Archivist,
No offence taken...perhaps I do need to look at those.
I shall do that...thanks for the advice.


Thx.
 
lord_pixel
Posts:252
Registered: 10/21/97
Re: generics 2.0 : A bug ,or I am doing something wrong ?      
Jun 11, 2003 8:24 AM (reply 5 of 11)  (In reply to original post )

 
Well, the example below works for me. We can't really answer your question unless you show us how you declare your HashMap.

import java.util.*;
 
public class HashMapFun {
    Map<String, Double> m_map = new HashMap<String,Double>();
 
    HashMapFun() {
        m_map.put("One", 1.0d);
        m_map.put("Two", 2.0d);
 
        System.out.println(getValue("Two"));
        System.out.println(getValue("One"));
    }
    
    Double getValue(String a_key) {
        if ("Two".equals(a_key) ) {
            return m_map.get(a_key);
        } else {
            //surprise!
            return new Double(3.0d);
        }
    }
 
    public static void main(String[] argv) {
        new HashMapFun();
    }
}


I do think you're expecting too much from autoboxing. I believe its only specified to work in certain places. Then again: why is that? It'll only serve to confuse people. I don't see why

    return 3.0d;


Shouldn't work in the above example?
 
veenurs
Posts:53
Registered: 1/22/01
Re: generics 2.0 : A bug ,or I am doing something wrong ?   
Jun 12, 2003 4:17 AM (reply 6 of 11)  (In reply to original post )

 
Hi ,
Thanks all , specially The Lord.
Yeah , I went through almost whole of the forums and tried to put the bits togather . Not everything goes well with the compiler yet.
To start with , it seems auto-Unboxing doesn't work yet .So I better forget
abt. a few things.
However, I'd like to know this -
Is return -1.00d not a case of autoboxing , and only Map.put("Three",3) is ?

Also, I'm still looking for something like an API for generics .

PS- Sorry for my late response.It's partly due to the time-lag.
Thanks again.
 
veenurs
Posts:53
Registered: 1/22/01
Re: generics 2.0 : A bug ,or I am doing something wrong ?   
Jun 12, 2003 4:18 AM (reply 7 of 11)  (In reply to original post )

 
Also , the Hashmap is simply
Hashmap<MyObjectType,Integer>= new Hashmap<MyObjectType,Integer>();
-rahul
 
gafter
Posts:669
Registered: 6/25/98
Re: generics 2.0 : A bug ,or I am doing something wrong ?   
Jun 12, 2003 7:15 AM (reply 8 of 11)  (In reply to original post )

 
1. Is there an API documnetation for tiger ?

No, because we don't yet have a generics-enabled javadoc.
However, with the 2.0 prototype we ship the sources for
the APIs. Look at them!

2. why doesn't this work ? -

Boxing and unboxing are only implemented as argument
conversion in this prototype.
 
veenurs
Posts:53
Registered: 1/22/01
Re: generics 2.0 : A bug ,or I am doing something wrong ?   
Jun 12, 2003 11:02 PM (reply 9 of 11)  (In reply to #8 )

 
Thanks , Gafter.
Can't give the dukes to you, though, I think.Won't be "Officially Correct " for you,it seems.
Thanks to everyone again.
-Rahul
 
fredastaire
Posts:97
Registered: 1/3/00
Re: generics 2.0 : A bug ,or I am doing something wrong ?   
Jun 14, 2003 2:00 PM (reply 10 of 11)  (In reply to #9 )

 
Incidentally, although Sun doesn't have a generics-compatible javadoc, SinjDoc does exist and is exactly such a thing. So you can generate the API docs yourself. SinjDoc doesn't yet support the new variance syntax or the JSR-201 features, but it will shortly. See http://cag.lcs.mit.edu/~cananian/Projects/GJ/
 
gafter
Posts:669
Registered: 6/25/98
Re: generics 2.0 : A bug ,or I am doing something wrong ?   
Jun 22, 2003 7:59 AM (reply 11 of 11)  (In reply to original post )

 
In the current prototype, boxing and unboxing are argument
conversions only.
 
This topic has 11 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 63
  • Guests : 118

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums