Beginner: Two simple questions

F

Floppy

Hello,

I was reading the chapter about vectors, and find out that every element is
an object. Why does each element have to be an object?

Also is there a program that shows the stack and heep as the program is
running?

Thanks
 
G

ghl

Floppy said:
Hello,

I was reading the chapter about vectors, and find out that every element is
an object. Why does each element have to be an object?

Vectors are "flexible" arrays, holding varying numbers of elements. What the
vector actually stores is reference variables. Reference variables are used
to reference objects. Therefore, a vector stores "objects" in a loose sense.
In the technical sense every element of a vector is a reference.
Once you understand this, you see that your statement "every element is an
object" is wrong and leads to questions like the one you asked, which
doesn't make sense.
It's only a lazy way of thinking/speaking to say a vector "contains
objects." A vector (and all the collection framework structures) contain
references to objects.
 
M

Marco Schmidt

Floppy:
I was reading the chapter about vectors, and find out that every element is
an object. Why does each element have to be an object?

Object is the base class of all classes in Java. Each class extends
Object, thus every object can be stored in a Vector. So there is no
restriction when it comes to classes, if that's what you meant.

On the other hand, no variables of primitive types like int, char or
float can be stored in a Vector. That _is_ a restriction, which will
be removed in Java 1.5.
Also is there a program that shows the stack and heep as the program is
running?

Many IDEs give you the opportunity to check a variable's content while
the program is being debugged. BTW, it's "heap" (just so you are more
successful with web searches).

Regards,
Marco
 
R

Roedy Green

I was reading the chapter about vectors, and find out that every element is
an object. Why does each element have to be an object?

The author could have written variants to contains ints, strings etc.

However, Object was the most general. It can hold int if you put it in
an Integer wrapper.
 
J

Jesper Nordenberg

Floppy said:
Hello,

I was reading the chapter about vectors, and find out that every element is
an object. Why does each element have to be an object?

Because of the way Java was designed. Primitive types are separated from objects.
Also is there a program that shows the stack and heep as the program is
running?

It's called a debugger. There are plenty of free ones.

/Jesper Nordenberg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top