File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes when vector created ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "when vector created ??" Watch "when vector created ??" New topic

when vector created ??

Nada Mohammed
Ranch Hand

Joined: Sep 22, 2000
Posts: 30
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"

Joined: Sep 29, 2000
Posts: 8521
When the new operator is used.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

You'd need to do something like this for 'lazy instantiation':

Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Nada Mohammed:
Because i want to care about performance in my work.

Take a look at - there are many interesting discussions on the subject of performance optimization.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Peter den Haan
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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
I agree. Here's the link:
subject: when vector created ??
jQuery in Action, 3rd edition