2

Why is this type of conversion(array to Object) possible in Java and what does x refer to ?(can I still access the array elements "s1","s2","s3" through x). Where is the array to Object conversion used?

String[] array = {"s1","s2","s3"};  
 Object x = array;  

5 Answers 5

5

This is possible because an Array is an Object. When you do this widening conversion, you tell Java "This is an Object and you don't need to know anything else about it." You won't be able to access the array elements anymore , because plain Objects don't support element access. However, you can cast x back to an array, which would let you access its elements again:

String[] array = {"s1","s2","s3"};  
Object x = array;

// These will print out the same memory address, 
// because they point to the same object in memory
System.out.println(array);
System.out.println(x);

// This doesn't compile, because x is **only** an Object:
//System.out.println(x[0]);

// Cast x to a String[] (or Object[]) to access its elements.
String[] theSameArray = (String[]) x;
System.out.println(theSameArray[0]); // prints s1
System.out.println(((Object[]) x)[0]); // prints s1
Sign up to request clarification or add additional context in comments.

Comments

3

This is called a widening reference conversion (JLS Section 5.1.5). x still refers to the array, but Java only knows x as an Object.

You cannot access the array elements directly through x unless you cast it back to String[] first.

1 Comment

I would only use this conversion when I had to place such a String[] into a collection (or another array) of Objects that was meant to contain such heterogeneous items whose only commonality is that they're all Objects.
1

Every array type in Java ultimately is a kind of Object. There is no conversion going on here; it's just the usual ability to assign a subtype value to a supertype variable.

Comments

0

The other answers have pointed out that arrays extend Object. To answer your last question:

Where is the array to Object conversion used?

This would rarely be used, but one use case is related to varargs. Consider this method:

static void count(Object... objects) {
    System.out.println("There are " + objects.length + " object(s).");
}

In order to treat a single array argument as an element of the varags, you would need to cast to Object:

String[] strings = {"s1", "s2", "s3"};

count(strings);         //There are 3 object(s).
count((Object)strings); //There are 1 object(s).

This is because a varargs parameter is an array first and foremost, so when it's ambiguous the compiler treats an array parameter that way. Upcasting to Object tells the compiler otherwise.

Comments

0

in your code x is a pointer to an array of String which names array.

so what ever you change in array would happen to the x too, but it seems to be useless , because Object is a super class for String[] so wherever you should use Object you can use String[].

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.