It's not a secret anymore!*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "when vector created ??" Watch "when vector created ??" New topic
Author

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"
Sheriff

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
Sheriff

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
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


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
author
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: http://aspose.com/file-tools
 
subject: when vector created ??
 
Similar Threads
Why am I getting this error?
Iterate tag for Vector
which object can be add into a Vector?
Creating an instance of Vector Class and adding an Element?
Help please