• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Which code is better

 
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.HashMap;

class Sample{
private String key = "ABC";

public Sample(){

HashMap map = new HashMap();
map.put(key, "XYZ");
System.out.println("Attribute created");
String s = (String)map.get(key);
System.out.println("Value == "+ s);
}
public static void main(String[] arg){
long time;
System.out.println(time = System.currentTimeMillis());
Sample s = new Sample();
System.out.println(System.currentTimeMillis() - time);

}

}



/////////////////////////////////////////////////////////////////////////



import java.util.HashMap;

class Sample1{

public Sample1(){

HashMap map = new HashMap();
map.put("ABC", "XYZ");
System.out.println("Attribute created");
String s = (String)map.get("ABC");
System.out.println("Value == "+ s);

}
public static void main(String[] arg){
long time;
System.out.println(time = System.currentTimeMillis());
Sample1 s = new Sample1();
System.out.println(System.currentTimeMillis()- time);

}

}


//////////////////////////////////////////////////////////////


Which of the two above codes is better in terms of

1. Performance
2. Execution Time
3. Good coding practise

Or any other criteria that compare two similer applications.
[ May 08, 2007: Message edited by: Sunil Kumar Gupta ]
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I prefer option 2, and I prefer using HashMap<String, String>. But I don't think it will improve the performance and execution time very much.
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Louis Wang:
I prefer option 2, and I prefer using HashMap<String, String>. But I don't think it will improve the performance and execution time very much.



Thanks Louis for quick response.

My intention was to know that in sample.java , I have declared a instance variable key with value "ABC" and used the same variable everywhere. In Sample1.java, I have not declared any instance variable , I have just used "ABC" eveywhere. Now which one is better to use instance variable for key or just use String literal. And why ?
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
???
 
reply
    Bookmark Topic Watch Topic
  • New Topic