1

I am a bit new to Java Programming. Two or three days ago, I encountered a Question regarding arrays in my mind which is give below.

Every Java Programmers knows, Array is a collection of Objects, it doesn't matter whether it contains primitive data types or Strings.

So my Question is, if Array is a collection of Objects so how does it treats or converts primitive data type into objects, because in Java, Primitive Data Type is different from Objects(like Strings). Consider the following program:-

   int[] Array = new int[3];
   Array[0] = 1;
   Array[1] = 2;
   Array[2] = 4;`
   for(int a=0;a<Array.length;a++) System.out.println(Array[a]);

I made the Array or Array Object using new keyword and datatype follows it. This is, of course, workable for arrays. But when I do something like this for variable, it would get fail.

int var1 = new int 3;

For attention, asking again, how does in Java Array treats or converts the Primitive Data Type as Objects, since generally Primitive Data Types are not Objects.

Thank You!

7
  • 4
    "how does in Java Array treats or converts the Primitive Data Type as Objects" It doesn't. Where have you heard this? Arrays of primitive types hold primitive types. Commented May 11, 2015 at 22:37
  • 1
    "Every Java Programmers knows, Array is a collection of Objects, it doesn't matter whether it contains primitive data types or Strings." Wrong. An array is an object, but it doesn't necessarily contain objects. Commented May 11, 2015 at 22:39
  • @Radiodef I did not heard this. I am using this term to just implement my Question easily. If Java doesn't treat Primitive Data Types as Objects, how does programmers say Array is Collection of Objects. Because Array holds integers or any other primitive data type to be stored which are not considered as Objects. Commented May 11, 2015 at 22:41
  • "how does programmers say Array is Collection of Objects" We don't say this. It's inaccurate. Are you asking if it's true or not? Commented May 11, 2015 at 22:44
  • @Radiodef I read it on some tutorial blog that "Array is collection of objects", that's why I am saying this. As you asked "Are you asking if it's true or not", so in truth I am just asking if Array just holds objects, so how is it able to hold primitive data types since both things are different. I thought that every element inside array will be Object, then Strings will remain Objects but Primitive Types which are not Object how stored as Objects inside Array.That's it :) Commented May 11, 2015 at 22:51

2 Answers 2

3

in java, there are 2 categories of types: primitive and reference(i.e. objects)

An array type (whether it's a primitive array or an object array) is always a reference type. For exmaple, int[] is a subtype of Object. You can call any methods in Object on an int[].

Nevertheless, int[] is an array of primitives, not objects.

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

Comments

3

JLS-10. Arrays says (in part),

In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.

...

All the components of an array have the same type, called the component type of the array. If the component type of an array is T, then the type of the array itself is written T[].

The value of an array component of type float is always an element of the float value set (§4.2.3); similarly, the value of an array component of type double is always an element of the double value set. It is not permitted for the value of an array component of type float to be an element of the float-extended-exponent value set that is not also an element of the float value set, nor for the value of an array component of type double to be an element of the double-extended-exponent value set that is not also an element of the double value set.

tl;dr An array of primitives is still an Object, but its' component type is still of the primitive type. Finally, while it's not applicable to your question, when a primitive type is used in a place where an Object is expected then Java does have a feature called Autoboxing which can convert a primitive to the corresponding wrapper type (and the reverse also can occur, that is unboxing). But arrays can store primitive types (unlike Collection(s) which can only store Object instances).

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.