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?

1
  • The first is an array containing one int. The other is just an int. Could you explain why you think they're the same? Commented Jun 3, 2011 at 3:08

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

8 Comments

so what's the reason of creating an array of 1 element instead of creating just an int?
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.
From a scope that can access a but not reassign it, then the int[] allows mutation of the actual int value.
@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.
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 java.sun.com/docs/books/jls/third_edition/html/…
|
7

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

2 Comments

@Algorithmist: Thank you. :-) @Gabe: Isn't it?
More explanation on primitive living on stack of heap can be found here: stackoverflow.com/questions/2099695/…
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.

8 Comments

Yes, this is a form of pass-by-reference.
@Gabe - exactly. I guess I could have used one (well, three) words instead of three sentences. :)
@Gabe - technically speaking, it is a way to simulate pass by reference semantics in Java.
@Stephen: What's the difference between "a form of" and "a way to simulate"?
@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.
|
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

1 Comment

Actually, I think that int[] a = new int[1]{1}; is usually written int[] a = {1};
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.