JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Re:J2SE 1.5 的新特点 [Re:tempo]
Dream

Hope has a place



發文: 31
積分: 1
於 2003-10-05 01:56 user profilesend a private message to usersend email to Dreamreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
tempo wrote:
如同 duncan 所說的..不管什麼 type 全部還是當成 Object..
Vector<int> vec; -> Vector vec;
然後用的時候再 (Integer) 轉型..


Yes, you can say that again. FYI, below is quoted from Oracle magazine (Sep/Oct 2003) page 78:

It's interesting to note that parameterized types and raw types are compiled to be the same type. No special classes enter the picture, and everything is accompilished using compiler trickery.
An instanceof check proves this:
1
2
3
4
5
6
7
8
9
10
11
public static void oldMethod(List list) {
   System.out.println(list.size());
}
public static void main(String[] args) {
   List<String> wrods = new ArrayList<String>();
   oldMethod(words);   // no problem
}
 
words instanceof List                // true
words instanceof ArrayList           // true
words instanceof ArrayList,String>   // true
That raises the question, "If they're the same type, how strong can the checking be ?" It's a contract written in ink, not in blood. This code generates a compile error because you can't add a Point to a List<String>:
1
2
List<String> list = new ArrayList<String>();
list.add(new Point());   // compile error

But this code compiles!
1
2
List<String> list = new ArrayList<String>();
((List)list).add(new Point());

It casts the parameterized type to a raw type, which is legal and avoids the type check but generates a warning, as explained earlier, that call are going unchecked:
1
2
3
warning: unchecked call to add(E) as a memberof the raw type
java.util.List ((List)list).add(new Point());
               ^


reply to postreply to post
I'm old enough to run against the wind and chase after my dream ...
話題樹型展開
人氣 標題 作者 字數 發文時間
16269 [精華] J2SE 1.5 的新特点 subbug 2433 2003-08-02 09:30
15091 Re:J2SE 1.5 的新特? piggy 311 2003-08-02 11:53
15261 Re:J2SE 1.5 的新特点 saijone 3765 2003-08-02 22:52
13838 Re:J2SE 1.5 的新特点 Duncan 689 2003-08-06 22:58
13691 Re:J2SE 1.5 的新特点 popcorny 1058 2003-08-06 23:17
13581 Re:J2SE 1.5 的新特点 tempo 667 2003-08-06 23:28
13586 Re:J2SE 1.5 的新特点 popcorny 675 2003-08-07 00:18
13776 Re:J2SE 1.5 的新特点 Duncan 1016 2003-08-07 00:35
14381 Re:J2SE 1.5 的新特点 popcorny 933 2003-08-07 00:57
13538 Re:J2SE 1.5 的新特点 Duncan 699 2003-08-07 08:59
13362 Re:J2SE 1.5 的新特点 tempo 387 2003-08-07 01:15
13565 Re:J2SE 1.5 的新特点 saijone 1722 2003-08-07 03:20
13330 Re:J2SE 1.5 的新特点 worookie 458 2003-08-07 05:56
13511 Re:J2SE 1.5 的新特点 Duncan 894 2003-08-07 09:06
13185 Re:J2SE 1.5 的新特点 worookie 149 2003-08-07 09:20
13462 Re:J2SE 1.5 的新特点 Duncan 194 2003-08-07 09:24
13200 Re:J2SE 1.5 的新特点 worookie 191 2003-08-07 09:34
13312 Re:J2SE 1.5 的新特点 saijone 660 2003-08-07 10:49
13494 Re:J2SE 1.5 的新特点 popcorny 267 2003-08-07 10:58
13185 Re:J2SE 1.5 的新特点 tempo 845 2003-08-07 11:27
13668 Re:J2SE 1.5 的新特点 Dream 1620 2003-10-05 01:56
13676 Re:J2SE 1.5 的新特点 Duncan 882 2003-08-07 00:21
13357 Re:J2SE 1.5 的新特点 tempo 434 2003-08-07 01:22
13251 Re:J2SE 1.5 的新特点 worookie 104 2003-08-07 02:04
13303 Re:J2SE 1.5 的新特点 popcorny 147 2003-08-07 11:02
13208 Re:J2SE 1.5 的新特点 tempo 157 2003-08-07 11:20
13102 Re:J2SE 1.5 的新特点 worookie 144 2003-08-07 14:04
13281 Re:J2SE 1.5 的新特点 worookie 169 2003-08-07 02:24
13341 Re:J2SE 1.5 的新特点 worookie 246 2003-08-07 01:29
13492 Re:J2SE 1.5 的新特点 popcorny 1947 2003-08-07 10:50
13269 Re:J2SE 1.5 的新特点 tempo 811 2003-08-07 11:19
14708 Re:J2SE 1.5 的新特点 saijone 764 2003-08-07 12:12
» JWorld@TW »  Java SE 討論區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8