Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

java compiler and string literals

Thread view: 
John and Diane Curley - 21 May 2006 02:45 GMT
Hi, All:

Just verifying Java Compiler optimization and string literals.  If I have
the following method, how many String objects are created in the for loop:

public void method() {
    for(int i = 0; i < 1000000; i++) {
           System.out.println("xxx");
           System.out.println("xxx");
           System.out.println("xxx");
           System.out.println("xxx");
           System.out.println("xxx");
    }
}

I say one object is created for the entire for loop and the code is fine, my
colleague says many and this code is inefficient.  Any people want to
comment?

Thanks,
John
Timo Stamm - 21 May 2006 03:32 GMT
John and Diane Curley schrieb:
> Hi, All:
>
[quoted text clipped - 14 lines]
> colleague says many and this code is inefficient.  Any people want to
> comment?

Only one String object is created. See $3.10.5 of the Java Language
Specification.

Timo
java-interview - 21 May 2006 10:36 GMT
only one String Object will be created
java-interview - 21 May 2006 10:37 GMT
only one String Object will be created
Tony Morris - 21 May 2006 11:50 GMT
> Hi, All:
>
[quoted text clipped - 17 lines]
> Thanks,
> John

None are create in the for loop. One is created at class load time from the
class' constant_pool.
See if this helps:
http://jqa.tmorris.net/GetQAndA.action?qids=68&showAnswers=true

--
Tony Morris
http://tmorris.net/
Chris Uppal - 21 May 2006 11:53 GMT
> [...] how many String objects are created in the for loop:
>
[quoted text clipped - 10 lines]
> I say one object is created for the entire for loop and the code is fine,
> my colleague says many and this code is inefficient.

You and your collegue are both wrong, but he is a lot more wrong than you ;-)

/No/ strings are created in that loop.  One instance of String ("xxx") is
created around about the time when the class is loaded.  In the loop you refer
to that single pre-existing String five million times; you do not create any
strings at all.

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.