Difference Between Int[] Array and Int Array[]

Difference between int[] array and int array[]

They are semantically identical. The int array[] syntax was only added to help C programmers get used to java.

int[] array is much preferable, and less confusing.

Java: int[] array vs int array[]

Both are equivalent. Take a look at the following:

int[] array;

// is equivalent to

int array[];
int var, array[];

// is equivalent to

int var;
int[] array;
int[] array1, array2[];

// is equivalent to

int[] array1;
int[][] array2;
public static int[] getArray()
{
// ..
}

// is equivalent to

public static int getArray()[]
{
// ..
}

Difference between int * array[60] and int * array = new int(60);

int * array = new int(60); //same thing?

No, they're not the same thing. array is just a pointer here, and then point to an int with initialized value 60.

If you meant int * array = new int[60];, array will point to an array of 60 ints, they're still not the same thing.

Note that just as the declaration, int* array means it is a pointer, while int* array[60] means it is an array (of 60 pointers). (Array might decay to pointer, i.e. int** for int* array[60], it's not same as int*.)

int[] and Integer[] arrays - What is the difference?

There is a difference at run-time.

int[] is an array of primitive int values. Integer[] is an "object" array, holding references to Integer objects.

Most important practical difference: int[] cannot hold null values.

But I'm still confused: does int[] store just a primitive values? If so - doesn't it mean that primitive types can live on heap without being wrapped?

int[] does store primitive types. And the array itself lives on the heap. However, those primitives are allocated as part of the array. They are not stored separately elsewhere on the heap. This is very similar to how a primitive field is part of an object instance: The object is on the heap, and its field is an integral part of that object (whereas for a non-primitive field, only the reference is stored inside the object and the target instance that reference points at is stored separately on the heap).

You could say the int is "wrapped" inside the array.

Confusion between int array[int] and int *array

A pointer is a pointer, it points somewhere else (like the first element of an array). The compiler doesn't have any information about where it might point or the size of the data it might point to.

An array is, well, an array of a number of consecutive elements of the same type. The compiler knows its size, since it's always specified (although sometimes the size is only implicitly specified).

An array can be initialized, but not assigned to. Arrays also often decay to pointers to their first element.

Array decay example:

int array[10];

int *pointer = array; // Here the symbol array decays to the expression &array[0]

// Now the variable pointer is pointing to the first element of array

Arrays can't naturally be passed to function. When you declare a function argument like int arr[], the compiler will be translating it as int *arr.

All of this information, and more, should be in any good book, tutorial or class.

IntArray vs ArrayInt in Kotlin

Array<Int> is an Integer[] under the hood, while IntArray is an int[]. That's it.

This means that when you put an Int in an Array<Int>, it will always be boxed (specifically, with an Integer.valueOf() call). In the case of IntArray, no boxing will occur, because it translates to a Java primitive array.


Other than the possible performance implications of the above, there's also convenience to consider. Primitive arrays can be left uninitialized and they will have default 0 values at all indexes. This is why IntArray and the rest of the primitive arrays have constructors that only take a size parameter:

val arr = IntArray(10)
println(arr.joinToString()) // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

In contrast, Array<T> doesn't have a constructor that only takes a size parameter: it needs valid, non-null T instances at all indexes to be in a valid state after creation. For Number types, this could be a default 0, but there's no way to create default instances of an arbitrary type T.

So when creating an Array<Int>, you can either use the constructor that takes an initializer function as well:

val arr = Array<Int>(10) { index -> 0 }  // full, verbose syntax
val arr = Array(10) { 0 } // concise version

Or create an Array<Int?> to avoid having to initialize every value, but then you'll be later forced to deal with possible null values every time you read from the array.

val arr = arrayOfNulls<Int>(10)

Difference between int * array and int array[] in a function parameter

There is no difference. Both functions types (after adjustment) are "function taking a pointer to int and an int, returning void." This is just a syntactic quirk of C++: the outermost [] in a function parameter of non-reference type is synonymous with *.

What's the difference between [Int] & ArrayInt?

From The Swift Programming Language: Collection Types

Array Type Shorthand Syntax

The type of a Swift array is written in full as Array<SomeType>, where SomeType is the type of values the array is allowed to store. You can also write the type of an array in shorthand form as [SomeType]. Although the two forms are functionally identical, - the shorthand form is preferred and is used throughout this guide when referring to the type of an array.

That is, both comments are correct - and both represent the same type.



Related Topics



Leave a reply



Submit