Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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
???
 
Fire me boy! Cool, soothing, shameless self promotion:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic