×

Loading...

Topic

  • 工作学习 / IT杂谈 / Are the following 4 pieces of code equivalent to each other?
    1) for(int i=0; i<10; i++) {
    System.out.println("i =" +i);
    }
    2) for(int i=0; i<10; ++i) {
    System.out.println("i =" +i);
    }
    3) for(int i=0; i<10; ) {
    System.out.println("i =" +i);
    i++;
    }
    4) for(int i=0; i<10; ) {
    System.out.println("i =" +i);
    ++i;
    }

    The question is: Are the above 4 pieces of code equivalent to each other? Can anybody tell me why? Thank you so much.
    • Are these things really so important? Architecture is more important for Java.
      • Few Chinese guys can play a role of architect
        They are not so important. However, a similar question exists in the Sun's testing problem bank.

        For us chinese guys, we need strong coding ability. For those who just begin, some small tricks are as importanbt as architecture.
      • Canada needs more programmer than architects
        Peter, thank you for your comments. A cruel fact is that most of us Chinese guys can only make our living by coding. So I posted some small tricks for people who are new in Java.
        In the real life, I think coding is essential to us. One of my friend's wife just lost her job because her poor coding ability jeopardized the whole project.
        We would be greatly appreciated if you could post something about architecture. Thank you in advance. --Jabber
        • I'm also new to Java.
          But I think for new beginner with experience in C++, the most difference between C++ and Java(especially J2EE) is in architecture. J2EE brought in a lot of new ideas and it gave revolutionary changes to the Java language.

          I would like to recommend studying Java by steps like RMI->EJB->Servlet->JSP... don't spend time in specific programming details too early.

          I get some chance in my company to do some design/architecture work with Oracle/Java. For Oracle, it's no problem for me. But I have not much experience with J2EE technology. So I would like to discuss with someone here for these things.

          Jabber, is that easy to setup the apache and tomcat up and get running? Is NT or W2K version available? I want to setup some testing environment in my two PCs at home. Do you have any recommendation for which application server/operation system to use?
          • FromZtoA, I donot know how to setup apache and tomcat in nt or w2k, but it is very easy to setup apache and tomcat in Linux. If you need some informations about that,I will email to you.
          • Also, NT version is available.
          • FromZToA, thanks for your comments
            FronZtoA, if I am not wrong here, you know everything from Z to A. :-) : -)

            I understand your interest in architecture.
            The best way to learn it is to discuss with people. Hope you can contribute more to our discussions.

            As for your questions, I asnwer them as follows: First make Tomcat and Apache
            work independently. They use port numbers 80 and 8080,respectively. After this, try to configure them to work together. You had better have a NT box though Tomcat can
            work on 95/98.

            There is a folder called "uguide" under tomcat, you need to read it carefully.
          • I am new to Java
            I am new to java and is now reading the book
            ThinkInJava. But I find out that I did not make biggest use of it. Can you give some suggestions on how to get an overview of the whole architecture as soon as possible?
            • NO architecture stuff in <<Thinking in Java>>
              <<Thinking in Java>> just discusses Java language.
              If you want to learn Java architecture,
              download J2ee bluebook from sun's site.
        • And why do you want to move to canada from U$A?
          • This is my choice of life
            I have no patience to wait for a USA green card. Now I think my life will be more meaningful once I settle down in Toronto.
            I will set up my own Java training center
            to help new immigrants (Of course, I will make money.) I am a very good Java instructor but I cannot set up my own business in USA because I don't have a green card. Definitlely, I will make less moeny in Canada than in USA.
    • The answer is ....
      The cited 4 pieces of code are equivalent. Though it is thought to be trivial, I explain the reason as follows:

      First, ++i is the same as i++ if we don't care the return value. Hence, 1) and 2) are equivalent. ++i has a return value of (i+1) while i++ has a return value of i. In our case,
      this is irrelevant.

      Second, in the for-loop control, the code in the parentheses{} is executed and next the code after the second semi-colon in the braces (). In 3), ++i is moved from within the braces into the parentheses, but the work flow is not changed. So 1) is equivalent to 3).

      If one takes Sun's Java 2 programmer certificate test, he/she may meet similar question.