Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » J2EE Interest »

Re: Strings

Frank W. Zammetti

2005-10-27

Replies:

I would suggest that a modern compiler would likely make that
optimization on your behalf anyway. I would just looking at the
generated bytecode of both of these and see if that theory is correct.

Also, even if it doesn't, this is a micro-optimization that most people
agree is pointless these days, unless you really have a need for the
absolutely most efficient code (and most people don't in most cases,
it's more important that code be clean and readable). If you truly do
have that need, then definitely see what the compiler does. This is
exactly the type of optimization I would expect any decent compiler to make.

--
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM: fzammetti
Yahoo: fzammetti
MSN: fzammetti@(protected)

SUBSCRIBE EJB-INTEREST anonymous wrote:
> Hi,
> Can anyone say me which one of the below two options would be better in
> perfromance and which one would create more string objects?
> One has String ,str within while loop and one outside while loop.
> Thanks in advance,
>
> Method : 1
> public void printList(Vector v)
> {
> boolean flag = true;
> int i=0;
> while(flag){
> String str = (String)v.get(i);
> System.out.println("The value :"+ str);
> if( i==5)
> flag = false;
> }
> }
>
>
> Method : 2
> public void printList(Vector v)
> {
> boolean flag = true;
> int i=0;
> String str = null;
> while(flag){
> str = (String)v.get(i);
> System.out.println("The value :"+ str);
> if( i==5)
> flag = false;
> }
> }
>
> ===========================================================================
> To unsubscribe, send email to listserv@(protected)
> of the message "signoff J2EE-INTEREST". For general help, send email to
> listserv@(protected)".
>
>
>

===========================================================================
To unsubscribe, send email to listserv@(protected)
of the message "signoff J2EE-INTEREST". For general help, send email to
listserv@(protected)".
©2008 junlu.com - Jax Systems, LLC, U.S.A.