aspose file tools*
The moose likes Beginning Java and the fly likes Core Java 2 Ex. 5-3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Core Java 2 Ex. 5-3" Watch "Core Java 2 Ex. 5-3" New topic
Author

Core Java 2 Ex. 5-3

Martin Clausen
Greenhorn

Joined: May 15, 2001
Posts: 14
Hi All
Two questions that will undoubtedly reveal me as a complete newbie.
I have keyed in Example 5-3 from "Core Java 2 Vol. 1 - Fundamentals".

My questions are these:
1) Why doesn't the code compile if i move the following code:
public static final int MAXSIZE = 100000;
public static final int NTRIES = 10;
to the top, as indicated by the commented out section ?(of course it compiles with comments, but not without).
2) I get the following results from running the program:
Allocating vector elements: 180 miliseconds
Allocating array elements: 211 miliseconds
Accessing vector elements: 1412 miliseconds
Accessing array elements: 1031 miliseconds
These results are not proportional to those found in the text - why ?
Thank you in advance for your answers.
Martin C.
(edited by Cindy to format code)
[This message has been edited by Cindy Glass (edited May 30, 2001).]
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
1) Local declaration of variables do not use keywords such as private , public,static or protected.SO if you left them in your main method you would have to take these keywords out.
2) this depends a lot on a hardware ,the faster the cpu better results.


Val SCJP <BR>going for SCJD
Martin Clausen
Greenhorn

Joined: May 15, 2001
Posts: 14
Ad 1) I think I get this one, although I cannot figure out why it is important that the:
public static final int MAXSIZE = 100000;
public static final int NTRIES = 10;
is at the end of the class, as opposed to at the begining like this:
import java.util.*;
class VectorBenchmark
{ public static void main(String[] args)

public static final int MAXSIZE = 100000;
public static final int NTRIES = 10;

{ Vector v = new Vector();

long start = new Date().getTime();
for (int i = 0; i < MAXSIZE; i++)
v.add(new Integer(i));
....
Ad 2) I realise that the values will vary, but how come they are not proportional, e.g. vector allocation is faster than array allocation, while vector access is slower than array access ?
Originally posted by Val Dra:
1) Local declaration of variables do not use keywords such as private , public,static or protected.SO if you left them in your main method you would have to take these keywords out.
2) this depends a lot on a hardware ,the faster the cpu better results.

Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Martin,
It is not important that the public static variables be declared at the end of your class instead of at the beginning. what IS important is that they are declared OUTSIDE of the main method.
If you look where they are at the end, the are placed after the main method closed. Where they are at the top is inside the main method. That makes the variables local to the main method and they cannot be public static (however they could be final).
Local variables belong to the method or block that they are declared in, not the instance and not the class (static).
If you move the statements BEFORE the main declaration it will work.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Martin Clausen
Greenhorn

Joined: May 15, 2001
Posts: 14
Ad 1) Thank you very much Cindy - silly of me not to notice.
Ad 2) Does anyone have an answer for that one ?
Originally posted by Cindy Glass:
Martin,
It is not important that the public static variables be declared at the end of your class instead of at the beginning. what IS important is that they are declared OUTSIDE of the main method.
If you look where they are at the end, the are placed after the main method closed. Where they are at the top is inside the main method. That makes the variables local to the main method and they cannot be public static (however they could be final).
Local variables belong to the method or block that they are declared in, not the instance and not the class (static).
If you move the statements BEFORE the main declaration it will work.

Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
I think Val already answered that for you.
The speed of each function within the JVM is dependant on the JVM that you are running in. Sun just provides the Specs and each vendor builds their own JVM as they see fit. Some are better at particular things than others.
MicroSoft has their preferred JVM built into Internet Explorer. Netscape has their JVM etc. Sun provides a downloadable JVM. Each of those can be run on a variety of operating systems (Well except Microsoft - but let's not go there) unix, linux, Win95/98, WinNT, Mac's etc. .
The JVM, the OS, the speed of your CPU etc. all combine to impact the relative speed of specific operations.
What did the guys in the Core Java 2 group test on? What are you testing on? Probably not the same platform.
Martin Clausen
Greenhorn

Joined: May 15, 2001
Posts: 14
That the absolute speeds should differ I get, but the relative too ? the array should be the fastest in both access and allocation shouldn't it ?
Or is it just me being a silly newbie ?
Martin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Core Java 2 Ex. 5-3