0

Online, the question of whether arrays are objects or variables is conflicting. Are arrays objects, or variables?

Blue Pelican Java book claims that they are variables, but they must be instantiated, so I'm not sure.

3
  • 1
    What do you mean by "objects or variables"? They are not exactly mutually exclusive, you know? Commented Jan 31, 2014 at 13:13
  • I think this is an interesting question and one I might have asked when I was first learning object oriented programming many years ago. Read through the answers, they're all right but illuminate the question in different ways. Commented Jan 31, 2014 at 14:18
  • Can you quote the particular part of the book that says that arrays are variables? Arrays are just another kind object, and references to them are held in variables, just like references to any other kind of object. Commented Feb 1, 2014 at 16:35

7 Answers 7

2

I think the JavaDocs clear that up in a single sentence

An array is a container object that holds a fixed number of values of a single type

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

Comments

1

I think that arrays are objects.

Comments

1

First, an instance of an array is a full right object in Java.

Second, an array may be the type of a variable (but not a variable). In that case, when the variable is instantiated, it will point to an array instance (which is an object).

Comments

1

when you ask "Are arrays objects, or variables?" I think you mean "Are arrays objects, or primitive data types?"

Arrays are objects and refer to a collection of primitive data types or other objects.

Arrays can store two types of data:

  • A collection of primitive data types
  • A collection of objects

Comments

0

Array is purely object.according to java doc

Comments

0

Try this code to check whether array is object or not .

 String[] str=new String[] {"A","B","X"};
 if (str instanceof Object){
     System.out.println("Yes!");
}else{
   System.out.println("No!");
}

3 Comments

Do you realize this will only compile if you use an object reference before instanceof? In other words, it will always print Yes! (well unless str is null).
'Yes!'. It means arrays are objects.
I know just pointing out it will work for everything else except a null reference (for primitive types it won't even compile).
0

In simple terms, a variable is how you declare an object and access it. So they are not two mutually exclusive things.

An array is an object, and you can use a variable to access it. While an array is an object, it may hold values of primitive types (example int[]) or it may hold objects of a class type (example String[])

int[] arr1 = new int[2];
System.out.println(arr1[0]); //output: 0

This will create an array object that can hold two values of primitive type int. The array object can be accessed using the variable arr1. Since the array holds primitives, they will be initialized to the default value 0 (or false for boolean).

String[] arr2 = new String[2];
System.out.println(arr2[0]); //output: null

This will create an array object that holds two objects of class type String. The array object can be accessed using the variable arr2. Since the array holds objects, it will initially hold null, which means no object.

More about arrays from The Java Tutorials

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.