日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java box unboxing

發布時間:2025/3/8 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java box unboxing 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://www.java2s.com/Tutorial/Java/0040__Data-Type/BoxingandUnboxing.htm Boxing and Unboxing
1、Boxing refers to the conversion of a primitive to a corresponding wrapper instance, such as from an int to a java.lang.Integer. 2、Unboxing is the conversion of a wrapper instance to a primitive type, such as from Byte to byte.
在參數傳遞和返回值中使用autoBox class AutoBox2 {
????static int m(Integer v) {
????????return v; // auto-unbox to int
????}

????public static void main(String args[]) {
????????Integer iOb = m(100);

????????System.out.println(iOb);
????}
} 下面程序注意點: 1、map的get? ,put方法 2、auto-unboxing import java.util.*;

public class TestArgsWords{
???? private static final int one =1;
????????
???? public static void main(String args[]){
????????
????Map m = new HashMap();
????for(int i=0;i<args.length;i++){
????
??????Integer freq= (Integer)m.get(args[i]); ???? ?//map的get方法返回值是object,所以首先將object類型轉換為Integer,然后傳給freq
??????m.put(args[i],(freq==null? one:(freq+1)));?//freq自動解包成int值
????}
????System.out.println(m.size() + "distinct word detected");
????
????System.out.println(m);
???? }

} 執行結果:

#java TestArgsWords aa????aa????bb cc ab????bb

#4distinct word detected
{aa=2, ab=1, bb=2, cc=1} 3、利用泛型Generic改良上述程序 import java.util.*;

public class TestArgsWords{
???? private static final int one =1;
????????
???? public static void main(String args[]){
????????
????Map<String,Integer> m = new HashMap<String,Integer> (); //map?泛型
????for(int i=0;i<args.length;i++){
????
??????Integer freq= m.get(args[i]); //減少 m.get(args[i])的強制類型轉換
??????m.put(args[i],(freq==null? one:(freq+1)));
????}
????System.out.println(m.size() + "distinct word detected");
????
????System.out.println(m);
???? }

}

轉載于:https://blog.51cto.com/vicky001/403771

總結

以上是生活随笔為你收集整理的java box unboxing的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。