Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8459181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:16:09+00:00 2026-06-10T13:16:09+00:00

I am not sure how downcasting of arrays works. Example this works: String[] sArray

  • 0

I am not sure how downcasting of arrays works.
Example this works:

String[] sArray = {"a", "b"};  
Object[] o = sArray;  
f((String[]) o);  

static void f(String[] s){  
  System.out.println("Ok");   
}  

But for the following:
new F(cert, (X509Certificate[]) ks.getCertificateChain("ALIAS"), key));
where F is

public F(X509Certificate certificate, X509Certificate[] certificateChain, PrivateKey privateKey) {      
}  

I get ClassCastException

java.lang.ClassCastException: [Ljava.security.cert.Certificate;
incompatible with [Ljava.security.cert.X509Certificate;

Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:16:11+00:00Added an answer on June 10, 2026 at 1:16 pm

    Arrays in Java are objects, and as such, have a class. They aren’t simply identityless containers. When you say:

    String[] sArray = {"a", "b"};
    

    You are creating an object of class String[], and putting two strings in it. There is an alternative way of writing it which makes that clearer:

    String[] sArray = new String[] {"a", "b"};
    

    You can cast references to arrays in much the same way as you can cast references to any other objects; you can always make a widening conversion (eg assigning an expression of type String[] to a variable of type Object[]), and you can make a narrowing conversion using an explicit cast (eg assigning an expression of type Object[] to a variable of type String[]). As with other objects, when you do an explicit case, the actual class of the object is checked at runtime, and if it doesn’t fit the desired type, a ClassCastException is thrown. As with other objects, the actual class of the object does not change when you cast. Only the type of the variable does.

    In your case, it looks like ks.getCertificateChain("ALIAS") returns a Certificate[]. It’s possible that the elements in it are instances of X509Certificate, but the array itself is still a Certificate[]. When you try to cast it to X509Certificate[], that fails.

    If you need a X509Certificate[], then what you will have to do is copy the contents of the array into a new array which is a X509Certificate[]. You can do that as tbk suggests, or you can call Arrays.copyOf, looking something like:

    Certificate[] Certificates = ks.getCertificateChain("ALIAS");
    X509Certificate[] x509Certificates = Arrays.copyOf(certificates, certificates.length, X509Certificate[].class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure how to explain it and can't seem to figure out why this
Not sure if I am asking this correctly (e.g. using widget/object in the correct
Not sure whats going on here, or what could be the integer in this
Not sure how to ask this, but i'll give it a try: I have
Not sure if this is a big deal. But wondering why when the site
Not sure if this is the correct Stack Exchange website but here goes.. A
Not sure this is a known issue. I’m using VS2012 RC (Ultimate), and Win8
Not sure if this is a security feature, an oversight, or me missing the
Not sure if this is possible: I want to use datatables to manipulate my
not sure what is wrong with this jquery code: http://jsfiddle.net/aCABM/ I want to to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.