Does the java compiler really compile and optimize the loop?

Source: Internet
Author: User

This article is only used to verify the java compiler in a cyclical manner. This article is a post, and I hope to get your analysis results. Here I reference a netizen (without personal attacks ):
///////////////////////////
If Vector v = new Vector () contains 10000 objects, we perform the following loop:
For (int I = 0; I <v. size (); I ++) {System. out. println ("testing ...");}
You must write the following code:
Int size = v. size (); for (int I = 0; I <size; I ++) {System. out. println ("testing ...");}
///////////////////////////
But some netizens said (just to illustrate the problem, there is no attack color ):
Java cannot be fully interpreted. v. size () is not called 10000 times in a loop, because the compiled loop optimization will optimize it,
Therefore, the program runs only once, which is the same as the program that initializes v. size. This is an early compilation optimization mechanism.
Are you sure this is correct? I don't agree with it. It's easy to use a test to prove it:
A Vector-like TetstObj. java

  1. Public ClassTestObj {
  2. IntCount = 0;
  3. PublicTestObj (){
  4. Count + = 5;
  5. }
  6. Public IntSize (){
  7. System. Out. println ("size =" + count );
  8. ReturnCount;
  9. }
  10. }

Test class: test. java
  1. PublicClassTest {
  2. PublicTest (){}
  3. Public Static VoidMain (String[] Args ){
  4. TestObj similarVector =NewTestObj ();
  5. For(IntI = 1; I <similarVector. size (); I ++ ){
  6. // Do something
  7. }
  8. }
  9. }

The result is still printed with 5 size = 1.
Size = 5
Size = 5
Size = 5
Size = 5
Size = 5
You can also analyze the java. util. Vector class (the source code of the Vector is extracted below)
For example, if only one insert object is used:
  1. /**
  2. * Returns the number of components in this vector.
  3. *
  4. * @ Return the number of components in this vector.
  5. */
  6.  Protected IntElementCount;
  7.  /**
  8. * Inserts the specified object as a component in this vector at
  9. * Specified <code> index </code>. Each component in this vector
  10. * An index greater or equal to the specified <code> index </code> is
  11. * Shifted upward to have an index one greater than the value it had
  12. * Previusly. <p>
  13. *
  14. * The index must be a value greater than or equal to <code> 0 </code>
  15. * And less than or equal to the current size of the vector. (If
  16. * Index is equal to the current size of the vector, the new element
  17. * Is appended to the Vector.) <p>
  18. *
  19. * This method is identical in functionality to the add (Object, int) method
  20. * (Which is part of the List interface). Note that the add method reverses
  21. * The order of the parameters, to more closely match array usage.
  22. *
  23. * @ Param obj the component to insert.
  24. * @ Param index where to insert the new component.
  25. * @ Exception ArrayIndexOutOfBoundsException if the index was invalid.
  26. * @ See # size ()
  27. * @ See # add (int, Object)
  28. * @ See List
  29. */
  30.  Public Synchronized VoidInsertElementAt (ObjectObj,IntIndex ){
  31. ModCount ++;
  32. If(Index> = elementCount + 1 ){
  33. Throw New ArrayIndexOutOfBoundsException(

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.