• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any way to cast an entire array to the required type, instead of having to loop through and cast each element explicitly e.g.

ArrayList array = new ArrayList();
// Add some elements of String type

Object[] array = array.toArray();

for (int i = 0; i < array.length; i++) {
array[i] = (String) array[i];
}

Any help would be appreciated?
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you have doesn't actually do anything to the objects in your array. If you want to specify that all the elements are Strings, you should define the array as String[] array, and iterate through the ArrayList. This still leaves you with the problem of having to cast each object though...

If you are using Java 1.5, you can make use of a new feature: Generics. To specify that all the elements in your ArrayList are Strings, you can define the ArrayList as ArrayList<String> al = new ArrayList<String>();.

What exactly do you need to perform this conversion for? If it's just to gain access to the String methods, then you can simply perform the cast when you need to call the method - there's no need to copy everything into a String array.
[ August 31, 2004: Message edited by: Fletcher Estes ]
 
Ravi Singh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the (String[]) cast actually do?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Imagine you've got a pet alligator. You're talking it for a walk on a leash at midnight. It's very dark outside. You meet a friend, who you can barely see. He says "what are you doing?" And you say "Walking my pet." And he takes the leash from you before you can stop him.

In his hand, he has a generic leash. It could be attached to a dog or a cat or anything. He has no idea.

You tell him, "be careful -- my pet alligator is on the end of that leash."

Now he knows he's holding an alligator leash, and I imagine he holds it a bit differently.

When you cast an Object reference to a String reference, you're telling the compiler "this Object variable is pointing to a String", just as, in my story, you tell your friend that the leash is really an alligator leash. In Java:



That's all a cast does, is tell the compiler that a variable of some general type really refers to an object of one specific subtype. It doesn't change the object in any way.
 
Ravi Singh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So is using (String[]) just a shorter way of casting all array elements?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The array is itself an object.

A String[] is a subclass of Object[].

A String[] can hold only Strings.

An Object[] could also hold Strings.

If you have a real Object[] and cast it to a String[], you'll get a ClassCastException, even if the Object[] contains all Strings.

Casting (String[]) is telling the compiler that the given Object[] object is actually an object of type String[] -- and if you're lying, there's trouble!
 
Ravi Singh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So providing that the 'real' actual object at the end of the object reference is a String you can perform a cast such as:

(String) reallyStringRef;

Does this apply for the object references for all other subclasses/superclasses?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ravi Singh:

Does this apply for the object references for all other subclasses/superclasses?



Yep.
 
Ravi Singh
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!
reply
    Bookmark Topic Watch Topic
  • New Topic