java - Is there a difference between int[] b = new int[a.length] b=a and int[] b = a? -
is there difference between
int[] b = new int[a.length]; b=a;
and
int[] b = a;
in java?
they're same, there few differences:
- in general, first 1 create array becomes unreachable (eligible garbage collection), not exist. (in both versions,
b
ends referring same arraya
, aside caveats below.) - if
a
null
, first 1 thrownullpointerexception
. - if you're unlucky, first 1 throw
outofmemoryerror
or similar. - the first 1 more cause compiler warnings, findbugs warnings, etc.
Comments
Post a Comment