8

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?

Gabe
  • 84,912
  • 12
  • 139
  • 238
Sergey
  • 11,548
  • 24
  • 76
  • 113
  • The first is an array containing one `int`. The other is just an `int`. Could you explain why you think they're the same? – BoltClock Jun 03 '11 at 03:08

6 Answers6

19
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.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • so what's the reason of creating an array of 1 element instead of creating just an int? – Sergey Jun 03 '11 at 03:00
  • When you have a Java object integer, you have methods you can use to do manipulations on this object. A primitive type only allows basic arithmetic operations. – SamT Jun 03 '11 at 03:03
  • From a scope that can access `a` but not reassign it, then the `int[]` allows mutation of the actual `int` value. – Synesso Jun 03 '11 at 03:06
  • @hvgotcodes +1 .just want to ask ,sometimes i use ArrayName.length without () thus it signifies that length is a static member .But where it is present i.e in which class is it Object. – Algorithmist Jun 03 '11 at 03:06
  • myArray.length is NOT a static member. Any array that you create will have its own length field. Everything in java that is not a primitive is an Object. I think array is a special case -- there is no class Array in the Java api. it is defined in the JLS http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.4.525 – hvgotcodes Jun 03 '11 at 03:15
  • Apart from the ability to pass it in as an array, are there any benefits to using it rather than Integer? (the class, not the primitive int.) – ArtOfWarfare Oct 09 '12 at 17:40
  • Are there benefits to using a lawnmower rather than a frying pan? They are different things and do different things. – Taylor Nov 05 '13 at 17:07
  • Another use-case of a one-element array is to let an anonymous nested class modify the enclosing scope's local variable values. – Nayuki Sep 10 '15 at 16:01
7

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

user541686
  • 205,094
  • 128
  • 528
  • 886
  • @Algorithmist: Thank you. :-) @Gabe: Isn't it? – user541686 Jun 03 '11 at 03:18
  • More explanation on primitive living on stack of heap can be found here: http://stackoverflow.com/questions/2099695/java-array-is-stored-in-stack-or-heap – lizzie Aug 20 '14 at 15:24
4

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Yes, this is a form of pass-by-reference. – Gabe Jun 03 '11 at 03:04
  • @Gabe - exactly. I guess I could have used one (well, three) words instead of three sentences. :) – Ted Hopp Jun 03 '11 at 03:06
  • @Gabe - technically speaking, it is a way to **simulate** pass by reference semantics in Java. – Stephen C Jun 03 '11 at 03:24
  • @Stephen: What's the difference between "a form of" and "a way to simulate"? – Gabe Jun 03 '11 at 04:14
  • @Gabe - one is real, and the other is fake. In real call-by-reference the callee is passed the address of a variable, either explicitly or implicitly. In this fake version, the caller has to create and populate an array, and then pull the value out of the array on return. – Stephen C Jun 03 '11 at 04:18
  • Actually, in Java, all objects are passed by reference and all primitives are passed by value. The trick here is passing a primitive by reference by embedding it in an object. An int array in Java is, it turns out, a very convenient object for this. (You might think Integer would be good for that, but Integer is immutable, unlike an int array.) – Ted Hopp Jun 03 '11 at 13:42
  • This is a common trick, but when I see this nowadays I change the code to use `AtomicBoolean` instead of `boolean[1]`, `AtomicReference` instead of `T[1]`, etc. – Hugues M. Aug 28 '18 at 09:30
  • @HuguesM. - Yes, that works very nicely as well. Any mutable object that encapsulates the argument value will work. – Ted Hopp Aug 28 '18 at 13:49
3
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
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

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.

akappa
  • 10,220
  • 3
  • 39
  • 56
0

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.

Smern
  • 18,746
  • 21
  • 72
  • 90