简体   繁体   中英

Why int[] a = new int[1] instead of just int a?

Is there some hidden meaning in this code which I don't see in java? How can it be useful?

int[] a = new int[1];

than just

int a;

because from my point of view it's the same?

int a

defines a primitive int.

int[] a = new int[1];

defines an array that has space to hold 1 int.

They are two very different things. The primitive has no methods/properites on it, but an array has properties on it (length), and methods (specifically its on clone method, and all the methods of Object).

Arrays are a bit of a weird beast. They are defined in the JLS .

In practice, it would make sense to do this when you need to interact with an API that takes an array and operates on the results. It is perfectly valid to pass in a reference to an array with 0, 1, or n properties. There are probably other valid reasons to define an array with 1 element.

I can't think of any use cases where you would want to define an array with one element, just to bypass the array and get the element.

One is on the stack, one is on the heap.

One difference is that you can write a method that changes its int argument by changing arg[0] . This trick is used quite a bit in some of the code I've seen. It allows you to, for instance, return a boolean indicate success or failure and an int value that serves some other purpose. Without that trick, you'd have to return some sort of object containing the two values.

int a;

defines a variable that can hold an int

int[] a;

defines a variable that can hold an array of int

int[] a = new int[1];

does that above but also initializes it by actually creating an array (of size 1 - it can hold 1 int) and defines the variable a to hold that array, but doesn't define what's in the array.

int[] a = new int[1]{1};

does that above but also defines what's in the array: the int 1.

I suppose it does a similar thing, in that space is allocated for 1 int, but the array also defines an array. I suppose you could say these are similar:

int a = 1;
int b = a + 1;
// now b == 2

int[] a = new int[1]{1};
int b = a[0] + 1;   
// now b == 2

An array of size one is not the same thing as a single integer.

Even if they carry the same information, they are different types, so you can use them in different contexts.

For example, if you have a function which performs a function on all elements of an array but you want to compute it only on one value, you should pass a int[1], because the function expects an array and wants to know how many values it should process.

All arrays in java are objects. when declaring: int x = 5; you're declaring a primitive type.

When declaring int[] x = new int[]; you're creating an object with type int[] .

So int[] is actually a class.

It is used when you need to pass a primitive data type by reference.

See this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM