赛迪网 > IT技术 Java > Java大图列表
  IT资讯搜索
 
IT产品搜索
[程序开发][网管世界][网络安全][数据库技术]
[操作系统][嘉宾聊天·在线访谈][活动集锦]
[精彩专题][Symantec专区][订阅IT技术周刊]
[开发论坛][网管论坛][安全论坛][数据库论坛]
[操作系统论坛][Sybase专区][IBM dW技术专区]
[病毒求助][病毒与漏洞播报][文档·源码下载]

Java编译器对于循环进行了真正的编译优化吗?

发布时间:2007.12.14 05:17     来源:赛迪网    作者:baocl

对于Vector v=new Vector()若v中包含了10000个对象,我们进行一个loop如下
for(int I=0; I<  v.size(); I++) { System.out.println("testing..."); }
大家一定会用如下的写法吧
int size = v.size(); for(int I=0; I<  size; I++) { System.out.println("testing..."); }
///////////////////////////
但有有人说不能用完全解释语言过程来理解Java,v.size()在循环中并没有被调用10000次,因为,编译的循环优化会对其进行优化,
因此,实际运行时只运行一次,和用初始化v.size()的程序效率一样。这是很早的编译优化机制。
大家肯定这种说法正确吗?我不太赞同,很简单地可以用一个test作难证:
一个类Vector的类TetstObj.java


public class TestObj {
  int count=0;
  public TestObj() {
    count+=5;
  }
  public int size(){
    System.out.println("size="+count);
    return count;
  }
}

测试类:test.java


public  class test{
  public test(){}
  public static void main(String[] args){
    TestObj similarVector=new TestObj();
    for(int i=1;i<  similarVector.size();i++){
      //do something
    }
  }
}

结果还是5个size=1的打印结果.
size=5
size=5
size=5
size=5
size=5
大家也可以分析一下java.util.Vector这个类(下面摘录Vector的部分源码)
例如只以一个insert对象而言:


/**
     * Returns the number of components in this vector.
     *
     * @return  the number of components in this vector.
     */
 protected int elementCount;
 /**
     * Inserts the specified object as a component in this vector at the
     * specified < code>index< /code>. Each component in this vector with
     * an index greater or equal to the specified < code>index< /code> is
     * shifted upward to have an index one greater than the value it had
     * previously. < p>
     *
     * The index must be a value greater than or equal to < code>0< /code>
     * and less than or equal to the current size of the vector. (If the
     * index is equal to the current size of the vector, the new element
     * is appended to the Vector.)< p>
     *
     * This method is identical in functionality to the add(Object, int) method
     * (which is part of the List interface). Note that the add method reverses
     * the order of the parameters, to more closely match array usage.
     *
     * @param      obj     the component to insert.
     * @param      index   where to insert the new component.
     * @exception  ArrayIndexOutOfBoundsException  if the index was invalid.
     * @see        #size()
     * @see       #add(int, Object)
     * @see       List
     */
 public synchronized void insertElementAt(Object obj, int index) {
    modCount++;
    if (index >= elementCount + 1) {
        throw new ArrayIndexOutOfBoundsException(index
                             + " > " + elementCount);
    }
    ensureCapacityHelper(elementCount + 1);
    System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
    elementData[index] = obj;
    elementCount++;
    }
    /**
     * Returns the number of components in this vector.
     *
     * @return  the number of components in this vector.
     */
   public synchronized int size() {
    return elementCount;
   }
 

这象上面的similarVector对象一样,v.size()会每次调用Vector中的size()方法,10000次的调用一次也不会少吧!!
所以就我自身来说我不会相信java的对循环的编译优化,你呢?


[ 发表评论 ] 字体[  ] [ 打印 ] [ 进入博客 ] [ 进入论坛 ]  [ 推荐给朋友 ]
  相关文章
· JAVA高级:Java布局管理器使用方法探讨 (12-13) · 与你共同分享java.util.Canlendar的优点 (12-13)
· 在类被实例化之前动态改变其静态的常量 (12-13) · 在.properties属性文件中存取设置参数 (12-13)
· 关于Java 语言中的 return 语句的讨论 (12-13) · 关于 Java 连接常见数据库的连接字符串 (12-13)
· 进阶:深入浅析关于如何jar的一点总结 (12-13) · JAVA基础--Java语言中关于修饰词总结 (12-13)
· 给初学者的文章--初学Java注意什么? (12-13) · 程序人生:初学者如何才算真正掌握Java (12-13)
  客户需求反馈表
* 姓  名:
更多资料  了解方案  认识厂商
* 单位名称:
* 联系电话:
* 电子邮件:
  赛迪推荐  
  手机·资费 ·新品·导购·评测·手机资费·宽带
手机搜索  诺基亚 N73 MOTO Z6
  IT产品 ·笔记本·台式机·服务器·打印·投影
IT产品搜索 
  IT技术 ·开发·网管·安全·数据库·操作系统
  信息化 ·热点·专题·访谈·周刊·方案案例
· 工信部“三定”公布 总编制731名设24司局
· 北京发电子商务监管意见 营利性网店须办照
· 直播 08中国城市信息化高峰论坛 案例点评
· 烽火网络校园解决方案 移民安置信息管理系统
  IT博客 ·曾剑秋·项立刚·Java学习·网管
  IT技术论坛 ·开发·网管·安全·数据库·系统