Array Initialization Differences Java

what‘s the differences between initializing an array of objects and initializing an object?

Given Student.java:

public class Student
{
private String name;

public Student () { name = ""; }

public String getName () { return name; }

public void setName (String name) { this.name = name; }
}

Then we have:

    Student[] A = null;
  • A is a reference of type Student[] which means Array of Student references
  • A reference is a memory address
  • A has null value; meaning it refers to nothing

    Student[] B = new Student[10];
  • B is a reference of type Student[]

  • new Student[10]; will reserve a memory of 10 memory references of type Student
  • B takes the reference returned by new which reference the 10 blocks of Student references
  • All of the reference blocks reserved by new are initialized to null
  • Attempting B[0].getName(); will compile, but will throw a NullPointerException on run-time

    B[0] = new Student();
  • B[0] is the first element in the array B initially refers to null

  • new Student(); will create an Object of type Student by invoking its constructor Student() known as the default constructor
  • After this, you can call Student functions like B[0].getName();

    Student C = null;
  • C is a reference of type Student

  • C has the value of null which means it refers to nothing
  • Attempting C.getName(); will compile, but will throw a NullPointerException on run-time

    C = new Student();
  • C will take the reference returned by new Student();

  • new Student(); will create an object of type Student by calling its constructor Student() and return its memory reference

difference between these 2 ways of initializing an simple array

In your case there is no difference.

There will be a difference when you are not assigning your array to variable and doing inline creation.

for example, conside there is method, which takes an array as argument.

  private  void someX(int[] param){
// do something
}

Your case:

  someX(myArr);     // using some declared array .I.e your case

Now see the difference while calling it in other cases.

      someX(new int[] {1,2,3}); //  yes, compiler satisfied.
someX({1,2,3}); //Error. Sorry boss, I don't know the type of array

What's the difference between two array declarations in Java?

They are identical except that as you mention you have to initialize it if you put the brackets after the name. One advantage of declaring them before the name is multiple array initialization like this:

int [] myArray1, myArray2;

int myArray1[], myArray2[];

The Java way according to the documentation is to put the brackets before the array name.

Array initialization - performance differences

For "abcdefg".toCharArray(), String.toCharArray()'s source code is

public char[] toCharArray() {
char result[] = new char[count];
getChars(0, count, result, 0);
return result;
}

getChars calls System.arraycopy so its performance with your arraycopy() should be the same. However, String's getChars copies from its internal char[] field, which is declared as final

private final char value[];

Where as your arraycopy() copies from SRC, which is non-final

private static char[] SRC = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };

This is just a guess, but trying making SRC final and see what happens.

Java: difference between initializing an array of integers using brackets and using new keyword

Yep, an array is an object, so it gets space allocated on the heap.

Every array type, including primitives, has a class. So when you create an array of primitive int, the JVM creates an instance of int[].class on the heap.

What is the difference between Array.newInstance and direct initialization?

With the Arrays.newInstance you can create a generic array of a type which you dont know at declaration time. by doing so you avoid a nasty Object[] declaration which is crucial for type safety.

For you example I could also write

array[i]=new Bar();

See this this post here.



Related Topics



Leave a reply



Submit