• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

variable declaration and object creation in side loop

 
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could anybody answer me from performance point of view.

Should i declare a variable outside a for loop or inside while iterating the List

1.
String str;
for(int i=0;i<list.size();i++){
str = (String)list.get(i);
}

2.
for(int i=0;i<list.size();i++){
String str = (String)list.get(i);
}

Sincerely
Pawan
(SCJP 5.0)
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Could anybody answer me from performance point of view.

Should i declare a variable outside a for loop or inside while iterating the List

1.
String str;
for(int i=0;i<list.size();i++){
str = (String)list.get(i);
}

2.
for(int i=0;i<list.size();i++){
String str = (String)list.get(i);
}



It all depends on the usage. If you want to use it other than in the for loop you can declare it outside the for loop or inside a method or declare it as instance variable.If you just want to use it in for loop then declare it inside the for loop.

Nik
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answer.

Actually this loop comes under a method. But i want to use String str's object inside the loop, not outside it. So if i use it inside the loop, is there any performance issue,if i also declare str inside the loop instead of only one time creating it outside for loop.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't think that there should be a performance penalty. But even if there is one good programming practice syas that a variable should be defined in the smallest possible scope. If you don't need it outside the loop declare it inside the loop.
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are right, i have tested it using both declaring String str inside the inner for loop and outside both loops. Results are same.

----------------------------------------------------------
List list = new ArrayList();
for(int i=0;i<500000;i++){
list.add(String.valueOf(i));
}

System.out.println("start :"+new Date());
System.out.println("total memory :"+Runtime.getRuntime().totalMemory());
System.out.println("max memory :"+Runtime.getRuntime().maxMemory());
String str = null;
for(int i=0;i<500;i++){
for(int k=0;k<list.size();k++){
str = (String)list.get(k);
}
}
System.out.println("free memory :"+Runtime.getRuntime().freeMemory());
System.out.println("end :"+new Date());

----------------------------------
Both times i get same results

start :Wed May 16 15:02:13 GMT+08:00 2007
total memory :40218624
max memory :66650112
free memory :11034680
end :Wed May 16 15:02:20 GMT+08:00 2007


Sincerely,
Pawan
(SCJP5.0)

start :Wed May 16 15:03:32 GMT+08:00 2007
total memory :40218624
max memory :66650112
free memory :11034680
end :Wed May 16 15:03:39 GMT+08:00
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.
String str;
for(int i=0;i<list.size();i++){
str = (String)list.get(i);
}

2.
for(int i=0;i<list.size();i++){
String str = (String)list.get(i);
}

It depandes on the usage of variables
case 2: if you want to use it outside the FOR loop, it wont work because the scope of the variable is only inside the FOR loop.

case 1: The variable str v can use for some other coding. Ex:in some other loops which inside that class or inside where the variable declared.
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic