typecasting arrays

String[] strArray = (String[]) myList.getSelectedValues();

This code compiles.

myList is a javax.swing.JList. myList.getSelectedValues() returns type Object[]. I want to convert this into an array of Strings.

Although this line compiles, I get an exception at runtime:Exception in thread"AWT-EventQueue-0" java.lang.ClassCastException: [Ljava.lang.Object;

However, when I use a for loop to typecast the array items one-by-one, I have no problems.

[503 byte] By [Puhfyna] at [2007-11-27 11:36:25]
# 1

You can't cast an Object[] to a String[] because it's NOT a String[].

jverda at 2007-7-29 17:08:40 > top of Java-index,Java Essentials,New To Java...
# 2

Why does the compiler allow it?

And these objects are originally Strings that were casted into Objects.

Puhfyna at 2007-7-29 17:08:40 > top of Java-index,Java Essentials,New To Java...
# 3

> Why does the compiler allow it?

The compiler must allow you to cast an Object reference to any other reference type.

Of course, this doesn't mean it will succeed at runtime.

BigDaddyLoveHandlesa at 2007-7-29 17:08:40 > top of Java-index,Java Essentials,New To Java...
# 4

> And these objects are originally Strings that were casted into Objects.

Doesn't matter. The array elements could even be Strings:

Object[] objArray = {"hello", "world"};

Object obj = objArray;

String[] strArray = (String[]) obj; //ClassCastException!

BigDaddyLoveHandlesa at 2007-7-29 17:08:40 > top of Java-index,Java Essentials,New To Java...
# 5

> Why does the compiler allow it?

http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#25379

> And these objects are originally Strings that were

> casted into Objects.

Casting doesn't change the object.

An Object[] full of Strings is not a String[]. It's just an Object[] that at the moment doesn't happen to contain anything but Strings.

jverda at 2007-7-29 17:08:40 > top of Java-index,Java Essentials,New To Java...
# 6

> Why does the compiler allow it?

>

> And these objects are originally Strings that were

> casted into Objects.

You aren't casting the individual String elements, you're casting the array object, which is a completely different object than any of it's elements.

hunter9000a at 2007-7-29 17:08:40 > top of Java-index,Java Essentials,New To Java...
# 7

> > Why does the compiler allow it?

> >

> > And these objects are originally Strings that were

> > casted into Objects.

>

> You aren't casting the individual String elements,

> you're casting the array object, which is a

> completely different object than any of it's elements.

I think your reply pretty much answers it for me, but I'm still somewhat hazy.

Since I cannot perform a narrowing conversion here, does this mean an Object[] can never be a legitimate String[]?

Puhfyna at 2007-7-29 17:08:40 > top of Java-index,Java Essentials,New To Java...
# 8

You know, if you justed used Groovy, this wouldn't be a problem.

;-)

BigDaddyLoveHandlesa at 2007-7-29 17:08:40 > top of Java-index,Java Essentials,New To Java...
# 9

> Since I cannot perform a narrowing conversion here,

> does this mean an Object[] can never be a legitimate

> String[]?

The Object array itself will never be a String array.

However, a String array can be referred to by a reference of type Object[], and we can then cast that reference to String[].

jverda at 2007-7-29 17:08:40 > top of Java-index,Java Essentials,New To Java...
# 10

> You know, if you justed used Groovy, this wouldn't be a problem.

Don't hate the playa, hate the game.

But you're right.

;o)

yawmarka at 2007-7-29 17:08:40 > top of Java-index,Java Essentials,New To Java...
# 11

lol i'll think about it.

why is this no longer an issue in Groovy, then?

Puhfyna at 2007-7-29 17:08:40 > top of Java-index,Java Essentials,New To Java...
# 12

> why is this no longer an issue in Groovy, then?

Groovy's not as retentive when it comes to declaring variable type. Example:

#! /usr/bin/groovy

import javax.swing.*

list = new JList()

list.setListData 'A', 'B', 'C'

list.setSelectedIndices 0, 2

assert list.selectedValues == ['A', 'C']

~

yawmarka at 2007-7-29 17:08:40 > top of Java-index,Java Essentials,New To Java...