File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when vector created ??

 
Nada Mohammed
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,
suppose i need vector in my class a :
class a {
Vector v=new Vector() ;
void method_1(int i ) {
v.addElement(i) ;
}
}
when actually vector created ? when i declared
Vector v=new Vector() ;
or when i add first element in it
v.addElement(i) ;
Because i want to care about performance in my work .Therefore , i need vector created when there is object invoke method_1 .
Thanks a lot
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the new operator is used.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd need to do something like this for 'lazy instantiation':

------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Originally posted by Nada Mohammed:
Because i want to care about performance in my work.

Take a look at http://c2.com/cgi/wiki?CategoryOptimization - there are many interesting discussions on the subject of performance optimization.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nada Mohammed:
Because i want to care about performance in my work .
Excellent! In that case,
  • Don't performance optimise in any way, but rather concentrate on clean design. A clean design is generally far more amenable to performance optimisations; on the other hand, optimising on-the-fly is mostly misdirected (see below) and tends to compromise design.
  • Before you performance optimise, measure. You don't know where the performance bottleneck is until you've profiled your application. You might be surprised. Developers with more experience than you and me put together are often surprised at exactly where the bottlenecks are in their code.
  • Once you know what to optimise, concentrate on algorithmic optimisations first.
  • Lazy instantiation is, like so many optimisations, a tradeoff. If you end up never calling the method you saved yourself some time (object creation time and, more importantly, garbage collection time). The price you pay is the overhead of an additional test for each call. If you call the method most of the time, lots of times, the "optimisation" will make your code slower.
  • If you really care about performance, then get rid of the Vector and use an ArrayList; the lack of synchronization usually doesn't make a difference except to make it faster. Make sure to pre-size it if you can.

  • Have fun!
    - Peter
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic