i am a java begineer and my confusion is:
in c++ we have seen that we can initiliaze the array just by writing this-
int a[5];
but in java we need new operator for array initilzation as-
int a[]=new int[5];
plz explain me in detail the reason for this???

int a[] (or int[] a ) defines a variable a that is a reference (like a pointer) to an array of ints. It's initial value is "null" - ie it does not contain an actual reference
new int[5] allocates memory in the heap for an array of 5 ints
so
int[] a = new int[5];
creates an array of 5 ints, creates an int array reference, and sets that reference to point to the array.

int [] A does the initialization but using a new operator like this int [] A = new int[x],creates an object that will serve as a reference to the memory location of the array,using the array without the new operator will give an error message of null pointer exception

In case you happen to have the values at compile time and want to hard-code them, you can also do this:

int[] a = {1,2,3,4,5};

Which creates the array on the right-hand side and assigns it to the array a.

But I can't think of a lot of occasions when I've needed something like that.

In java there are two steps first is declaring the array and the second is to initiliase the array.
So in first step the array is only declared.and in the Secon step it is getting the actual memory with the help of new keyword

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.