AutoBoxing

 
csdn回复的一个帖子。
当时看到这段代码感觉很荒谬。
1.Integer i1=100; //怎么可以这样初始化呢?
2.i1==i2          //2个不一样的对象比较怎么得出来得结果是true?
不过后来看到有人回复说是jdk1.5的新特性。
查了下jdk1.5的java language features
 
对于为什么Integer i1=100可以这样写有一点明白了
 
As any Java programmer knows, you can’t put an int (or other primitive value) into a collection. Collections can only hold object references, so you have to box primitive values into the appropriate wrapper class (which is Integer in the case of int). When you take the object out of the collection, you get the Integer that you put in; if you need an int, you must unbox the Integer using the intValue method. All of this boxing and unboxing is a pain, and clutters up your code. The autoboxing and unboxing feature automates the process, eliminating the pain and the clutter.
 
还附带了一个例子
  1. public class Frequency {   
  2.    public static void main(String[] args) {   
  3.       Map<String, Integer> m = new TreeMap<String, Integer>();   
  4.       for (String word : args) {   
  5.           Integer freq = m.get(word);   
  6.           m.put(word, (freq == null ? 1 : freq + 1));   
  7.       }   
  8.       System.out.println(m);   
  9.    }   
  10. }  

但可以对于为什么i1==i2打印出来时true还是想不明白。网上找资料。终于找到一篇比较靠谱的文章,提示说用dj反编译下看看。我试了下.
java 代码
  1. public static void main(String[] args) {   
  2.  Integer i=200;   
  3.  Integer ii=200;   
  4.  System.out.println(ii==i);   
  5.  Integer iii=100;   
  6.  Integer iiii=100;   
  7.  System.out.println(iii==iiii);   
  8.    Map<String, Integer> m = new TreeMap<String, Integer>();   
  9.    for (String word : args) {   
  10.        Integer freq = m.get(word);   
  11.        m.put(word, (freq == null ? 1 : freq + 1));   
  12.    }   
  13.    System.out.println(m);   
  14. }  
 
反编译为
 
public static void main(String args[])    {        Integer integer = Integer.valueOf(200);        Integer integer1 = Integer.valueOf(200);        System.out.println(integer1 == integer);        Integer integer2 = Integer.valueOf(100);        Integer integer3 = Integer.valueOf(100);        System.out.println(integer2 == integer3);        TreeMap treemap = new TreeMap();        String args1[] = args;        int i = args1.length;        for(int j = 0; j < i; j++)        {            String s = args1[j];            Integer integer4 = (Integer)treemap.get(s);            treemap.put(s, Integer.valueOf(integer4 != null ? integer4.intValue() + 1 : 1));        }        System.out.println(treemap);    }  
 
发觉原来和我想的不一样,我本来以为AutoBoxing是通过new一个wrapper class实现的结果却是用valueOf()。但是就算这样,那还是2个对象阿。
于是去看了下Integer.valueOf的源码。
 
public static Integer valueOf(int i) {        final int offset = 128;        if (i >= -128 && i <= 127) { // must cache         return IntegerCache.cache[i + offset];         }            return new Integer(i);     }  
 
说明对于一个byte范围大小的参数有特别的处理。于是看了下IntegerCache的源码
 
这是Integer的一个内部类
private static class IntegerCache {          private IntegerCache(){}          static final Integer cache[] = new Integer[-(-128) + 127 + 1];          static {             for(int i = 0; i < cache.length; i++)             cache[i] = new Integer(i - 128);          }    }   
 
现在我终于明白了
Integer i=200;    Integer ii=200;    System.out.println(ii==i);  
 
结果为true是因为它们就是同一个对象。IntegerCache初始化了256个Integer缓存。
i,ii只是一开始初始化的new Integer(100)的引用而已。
 
AutoBoxing终于明白了。
 
 
内容概要:本文档定义了一个名为 `xxx_SCustSuplier_info` 的视图,用于整合和展示客户(Customer)和供应商(Supplier)的相关信息。视图通过连接多个表来获取组织单位、客户账户、站点使用、位置、财务代码组合等数据。对于客户部分,视图选择了与账单相关的记录,并提取了账单客户ID、账单站点ID、客户名称、账户名称、站点代码、状态、付款条款等信息;对于供应商部分,视图选择了有效的供应商及其站点信息,包括供应商ID、供应商名称、供应商编号、状态、付款条款、财务代码组合等。视图还通过外连接确保即使某些字段为空也能显示相关信息。 适合人群:熟悉Oracle ERP系统,尤其是应付账款(AP)和应收账款(AR)模块的数据库管理员或开发人员;需要查询和管理客户及供应商信息的业务分析师。 使用场景及目标:① 数据库管理员可以通过此视图快速查询客户和供应商的基本信息,包括账单信息、财务代码组合等;② 开发人员可以利用此视图进行报表开发或数据迁移;③ 业务分析师可以使用此视图进行数据分析,如信用评估、付款周期分析等。 阅读建议:由于该视图涉及多个表的复杂连接,建议读者先熟悉各个表的结构和关系,特别是 `hz_parties`、`hz_cust_accounts`、`ap_suppliers` 等核心表。此外,注意视图中使用的外连接(如 `gl_code_combinations_kfv` 表的连接),这可能会影响查询结果的完整性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值