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 typeStudent[]
which means Array of Student references- A reference is a memory address
A
hasnull
value; meaning it refers to nothingStudent[] B = new Student[10];
B
is a reference of typeStudent[]
new Student[10];
will reserve a memory of 10 memory references of typeStudent
B
takes the reference returned bynew
which reference the 10 blocks ofStudent
references- All of the reference blocks reserved by
new
are initialized tonull
Attempting
B[0].getName();
will compile, but will throw aNullPointerException
on run-timeB[0] = new Student();
B[0]
is the first element in the arrayB
initially refers tonull
new Student();
will create an Object of typeStudent
by invoking its constructorStudent()
known as the default constructorAfter this, you can call Student functions like
B[0].getName();
Student C = null;
C
is a reference of typeStudent
C
has the value ofnull
which means it refers to nothingAttempting
C.getName();
will compile, but will throw aNullPointerException
on run-timeC = new Student();
C
will take the reference returned bynew Student();
new Student();
will create an object of typeStudent
by calling its constructorStudent()
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
Counting the Number of Files in a Directory Using Java
How to Check for a Valid Url in Java
How to Test If My Font Is Rendered Correctly in PDF
How to Set Java_Home in MAC Permanently
How to Copy File Inside Jar to Outside the Jar
How to Convert from Cmyk to Rgb in Java Correctly
Checking for a Null Int Value from a Java Resultset
How to Improve Performance of This Regular Expression Further
Maven: How to Change Path to Target Directory from Command Line
How to Calculate the Difference Between Two Arraylists
How to Check If a Url Exists or Returns 404 with Java
Best Way to Pick a Random Subset from a Collection
Open a JPAnel After Pressing a Button in a Jframe
Spring - Redirect After Post (Even with Validation Errors)
What Is the "Default" Implementation of Method Defined in an Interface