wood burning stoves 2.0*
The moose likes Java in General and the fly likes Which is a good practice to refer maxQ.length ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Which is a good practice to refer maxQ.length ?" Watch "Which is a good practice to refer maxQ.length ?" New topic
Author

Which is a good practice to refer maxQ.length ?

Poobhathy Kannan
Ranch Hand

Joined: May 26, 2004
Posts: 94
Which is a good practice to refer maxQ.length ? see the bellow code blog.
1.directly calling maxQ.length
2.set it to a reference then use it..like
int length = maxQ.length;


code blog:

int[] maxQ = getQuantities();
.............
.............

compositePrice.setQty(maxQ.length);
..............
..............
if (press == PrintConstants.MIXED && pressCurrentIteration > 1)
{
quantityThreshold = maxQ.length;
}
for (int i = 0; i < maxQ.length; i++)
{


http://learnertobeginner.blogspot.com/
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
If the length of maxQ is going to be used in more than one place in your method, then I would obtain its value once and use it whenever necessary:

I would always use the final modifier as it helps in understanding that the length variable will not change.

Incidentally, if the length of maxQ will only be used in a loop, I would do this:

I just think that it's a little clearer and may even be a bit more efficient.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Poobhathy Kannan
Ranch Hand

Joined: May 26, 2004
Posts: 94
Thanks Chung-Wee

I have a small question on the part that you explained for loop.
What will happen if that Loop iterates 1 million time?
Because each and every time, it needs to access maxQ to get its length.

In that case it would be more efficient if I refer from a variable
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which is a good practice to refer maxQ.length ?
 
Similar Threads
i++ unary operator
how does Spring Roo help incorporate best practices
Why does this produce 0.2?
which one is recommended ?
Topic-wise Mock Tests