java compiler and string literals

  • Thread starter John and Diane Curley
  • Start date
J

John and Diane Curley

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
 
T

Timo Stamm

John said:
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?

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


Timo
 
T

Tony Morris

John and Diane Curley said:
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

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
 
C

Chris Uppal

John said:
[...] 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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,251
Members
46,851
Latest member
CristineKo

Latest Threads

Top