vendredi 27 février 2015

C++ vs Java: Arrays of Objects


C++:



Student* sp = new Student[10];


Now, please correct me if wrong. I believe this creates an array of 10 instances of Student, each calling the default constructor with no arguments. So, how do I create an array of 10 Student objects, each created using a parameterized constructor (with arguments). Please use arrays and not vectors.


Java:



Student [] sa = new Student[10];


Again, correct me if I'm wrong but I think in case of Java though, no instances are created when this statement executes and instead only memory is reserved to hold 10 references to Student instances. And it is necessary that we follow it up with:



sa[0] = new Student(*arguments*);
sa[1] = new Student(*arguments*);
.
.
.

sa[9] = new Student(*arguments*);


I'm trying to see the difference and quite frankly, I'm confused. Kindly clarify please.




Aucun commentaire:

Enregistrer un commentaire