Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning a char array to an int array

 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is quoted from K&S page 232



Two questions

1 - Is the statement "a variable of type byte, short or char can be explicitly promoted" correct? I thought that since all these are smaller than int, they are implicitly converted/promoted.
2 - Does the above mean that for array references the compiler will reject the assignment even if the compiler usually implicitly casts the values?

EDIT
---
Ok i lied. I had three questions actually.
3 - Does it also mean that a primitive array object can only be assigned to another array object that is of the same type?
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O. Ziggy wrote:1 - Is the statement "a variable of type byte, short or char can be explicitly promoted" correct? I thought that since all these are smaller than int, they are implicitly converted/promoted.

I think it is just referring to the fact that you can write this:
2 - Does the above mean that for array references the compiler will reject the assignment even if the compiler usually implicitly casts the values?

Yes.
3 - Does it also mean that a primitive array object can only be assigned to another array object that is of the same type?

Yes.
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a small note, keep in mind that this is different from how class arrays work:

An array of Child[] can be assigned to a reference variable pointing to an array of Parent[].

But maybe you already knew that

// Andreas
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i knew that and that is why i was not happy about the above
Thanks you both.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
both int[] and char[] are different object;there is no relationship.
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:both int[] and char[] are different object;there is no relationship.


I thought that they could be assigned because a char can be widened to an int just like a Horse can be widened to an Animal.
 
Henry Wong
author
Marshal
Pie
Posts: 20992
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O. Ziggy wrote:
I thought that they could be assigned because a char can be widened to an int just like a Horse can be widened to an Animal.


A char can be implicitly cast to an int because a char can be converted to an int with no loss of range. Neither a char or an int are objects, so it is nothing like how objects are widening. It may be a good idea to treat them differently.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic