Array of an object in Java vs C ++

My Background is C ++, and in C ++ we can easily create an array of objects using simple syntax. className obj[n]; , and also the constructor will call time n .

But when I tried to create an array of the object in java className[] obj=new className[n]; without calling the constructor. After searching, I found the answer to this question, https://stackoverflow.com/a/166269/24031/ ... that it simply creates a link n that can point to objects n , and I need to create objects for each link again. obj[0]=new className();

Now I just want to ask why Java does this? is there any reason even C ++ allows, but java doesn't allow you to create an array of objects in the same way? I searched for this, but still have not received an exact answer.

+7
java arrays
source share
4 answers

In C ++, you have the flexibility to choose the memory in which you create the object. You can create objects in an automatic region (on the stack), in a static region, or in a dynamic region. In the latter case, you get a pointer to the object and are responsible for releasing it after completion.

In contrast, all Java is a dynamic scope parameter because all objects are reference objects. In C ++, this is equivalent to using objects only through pointers and always creating them with new . When you do this in C ++, you should also populate your pointer array with new -ed objects:

 myclass *array[10]; for (int i = 0 ; i != 10 ; i++) { array[i] = new myclass(); } ... for (int i = 0 ; i != 10 ; i++) { delete array[i]; } 

Allowing the creation of arrays of objects in C ++ was a choice dictated by the need to allow programmers to allocate arrays of objects in the automatic area. It came with a compromise because the objects from which you are creating arrays must have default constructors. This is not ideal, because the default constructor requirement sounds arbitrary.

Java, on the other hand, is free of automatic memory requirements, so they went for a simple solution requiring you to initialize objects individually.

+9
source share

Not so often, you need to create objects of the same type as the array with the default constructor. Sometimes you want to call a custom constructor. Sometimes you want to instantiate subclasses and store them in an array.

Note that the Java array className[] obj more equivalent to the C ++ className* obj[n] , not just className obj[n] , because it is an array of object references, not an array of the objects themselves. With Java-8, you cannot create an array of objects (they are discussed as part of the Valhalla project , but will not even appear in Java-9). When the objects themselves are stored in an array in C ++, you must initialize the array. You cannot store "nulls" or something like this there, because null is not an object, it is a link (or a pointer). When you create an array of className* obj[n] in C ++ (which is more like a Java array of className[] obj ), it also does not initialize.

Finally, note that in Java-8, you can easily create all objects by creating them using the default constructor, like this:

 className[] array = Stream.generate(className::new).limit(n).toArray(className[]::new); 
+3
source share

What is allowed or not done depends on the language developers.

If you want to initialize all elements of an array with a reference to the same object in Java, you can use:

 className[] obj = new clasName[2]; Arrays.fill(obj, new className()); 

or create different objects and pass different arguments to each constructor

className[] obj = new className[] {new className(), new className()};

+2
source share

In Java, whenever you declare a variable, whether it be a member of an object or a local variable, it is either a primitive type ( byte , char , ...) or a reference type (a pointer to an object of some type).
Thus, there are no arrays of objects, only arrays of links.

In C ++, you have the freedom and responsibility to choose how much you specify, how to distribute, create and destroy objects for free, adding a lot of complexity:

  • dereference operator ( *pointer )
  • dereference-and-use-member ( pointer->member )
  • operator address ( &object )
  • a way to get a pointer to a type from other types ( type* var ).
  • placement-new (new type (pointer) (arg1, arg2);
  • Explicit delete operator
  • much more.
+1
source share

All Articles