Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which is better? new a object or convert itslef

 
tony macmillan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package test;

import java.math.BigDecimal;
import java.util.HashMap;

public class Mytest {

public static void main(String[] args){
HashMap map = new HashMap();
BigDecimal test = new BigDecimal("100");
map.put("A",test);
BigDecimal test1 = new BigDecimal(map.get("A").toString()); //1
BigDecimal test2 = (BigDecimal)map.get("A"); //2
}
}
}
Somebody told me that the type 1 is as same as type 2, but I don´┐Żt think so. type 2 is faster than type 1 , isn's it?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Version 2 is vastly faster, yes. Parsing the string to determine a value for the new BigDecimal is a relatively expensive operation.
 
tony macmillan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic