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

  • Home
  • SEARCH
  • 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 6247045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:47:53+00:00 2026-05-24T12:47:53+00:00

Can someone explain why the last assignment in the code below is not valid

  • 0

Can someone explain why the last assignment in the code below is not valid

class ScjpTest extends BounceObject implements Bouncable{

static int ac = 5;
static char ab = 'd';

static int[] a = new int[]{1,2,3};
static int[] b = new int[]{1,2,3};
static char[] c = new char[]{'a','b'};

public static void main(String[] args){  
    a = b;
    b = a;

    ac = ab;  //This is accepted
    a = c;    //This is rejected
}

}

The compiler complains with the following error

ScjpTest.java:10: incompatible types
found   : char[]
required: int[]
                a = c;
                    ^
1 error

The following is also accepted

class Animal{}
class Horse extends Animal{]

Animal[] animals = new Animal[2];
Horse[] horses = new Horses[2];

animals = horses;

Why can i then not assign a char array to an int array?

  • 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-05-24T12:47:54+00:00Added an answer on May 24, 2026 at 12:47 pm

    You can assign a char variable to an int one, because the compiler applies a broadening conversion. The basis being that an int is larger than a char and as such there is no chance of loss of information in the conversion. If you tried to assign your int to your char though, you would notice the compiler rejects your code until you put in a specific typecast. This is because narrowing conversions almost always involve a loss of data, and as such the compiler requires you, the programmer, to explicitely indicate this is what you intend to do.

    The same principle applies to objects. A broadening conversion is allowed implicitely, a narrowing conversion requires a typecast. Note that at no time can you convert objects that aren’t related to each other by super-class/sub-class hierarchy.

    Number n = null;
    Integer i = null;
    
    n = i ;         // Allowed without casting, Number is superclass of Integer
    i = n;          // Compiler error, Integer is sub-class of Number
    i= (Integer)n;  // Allowed due to the type-cast
    

    In the example above, Number is a superclass of Integer. Assigning an instance of Integer to instance of Number is allowed without typecast, since this is a ‘broadening’ conversion. Assigning an instance of Number to instance of Integer requires a specific cast since this is a narrowing conversion (Number could have represented a Float, or some other subclass). These rules carry over to arrays. So, you can do the following:

    Number[] numArr = null;
    Integer[] intArr = null;
    
    numArr = intArr;
    intArr = numArr;              //Compile error
    intArr = (Integer[]) numArr;
    

    For primitive arrays, there is no broadening conversion done on the array elements, even in cases where you think that it might make sense (ie, char to int, or byte to char etc). While this might not seem to make sense, if you look at the object analogue it becomes clearer why:

    Double[] doubleArr = null;
    Integer[] intArr = null;
    
    doubleArr = intArr ;      // Compile error, Double,Integer are sibling classes
    doubleArr = (Double[]) intArr;  // Compile error, same reason
    intArr = doubleArr;             // Compile error, same reason
    intArr = (Integer[]) doubleArr; // Compile error, same reason
    

    You can not cast a Double to an Integer, or vice-versa, they are completely different classes from the point of view of the compiler (as different as String and Float would be, for example).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain why the last line prints out -1? It happens when copy
Can someone explain the difference between .last() and :last ? I can't seem to
can someone explain why this jquery selector is not working, I've worked around the
Can someone explain why this code doesn't work as expected? I would expect it
Can someone explain the mechanics of a jump table and why is would be
Can someone explain to me the advantages of using an IOC container over simply
Can someone explain this result to me. The first test succeeds but the second
Can someone explain what exactly the string 0 but true means in Perl? As
Can someone explain what are the benefits of using the @import syntax comparing to
Can someone explain how to use if-then statements and for loops in Makefiles? I

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.