• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Check the object array instance of string array

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I need to check the given object array is an instance of string array or not. Please refer to the following code. When the code is executed the control never goes into the if loop (if(objArray instanceof String[])). What am I missing here? Secondly when I try to cast the object array to a string array, I get class cast exception. Please help me out.

Thanks,

Smitha

public class TestString {

public static void main(String[] args) {
String[] strArray = new String[] { "one", "two" };
Object[] objArray = new Object[] { "one1", "two2" };

System.out.println("From string array: " + (String) strArray.toString());
System.out.println("From object array: " + (String) objArray.toString());

if(objArray instanceof String[]) {
//Never enters into this loop
for(int i=0; i<objArray.length; i++) {
System.out.println(objArray[i]);
}
} else {
System.out.println("Failed");
}
//Fails here - java.lang.ClassCastException: java.lang.Object
String[] strArray1 = (String[]) objArray;
}
}
 
author and iconoclast
Posts: 24204
44
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
Hi Smitha,

First, a bit of business: seems you've been around here a long time! You're surely aware of our naming policy. It requires that you use a full, real (sounding) first and last name for your display name. You can change your display name here. Thanks!

Now, on to your problem: String[] is indeed subclass of Object[], so that, for example, this is OK:

Object[] myObjects = new String[] { "1", "2" };
String[] myStrings = (String[]) myObjects;

But note that the variable myObjects is pointing to something that was allocated as a String[], not as an Object[]. It's this last fact that makes the cast to String[] legal.

If, on the other hand, you allocate an Object[], then, even if it holds Strings, it's an array of Object, not an array of String, and it can't be cast to String[]. Likewise, whereas "myObjects instanceof String[]" is true using my definitions, "objArray instanceof String[]" is false in your program, because of the way objArray is allocated.

A cast of a reference type never changes the actual class of an object; it just gives the compiler more information than it presently has about the type a variable will be holding when the program runs.
 
Do not set lab on fire. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic