aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assigning a char array to an int array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assigning a char array to an int array" Watch "Assigning a char array to an int array" New topic
Author

Assigning a char array to an int array

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

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

Joined: Mar 12, 2011
Posts: 808
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

Joined: Jan 17, 2011
Posts: 179
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

Joined: Oct 02, 2005
Posts: 430

Yes i knew that and that is why i was not happy about the above
Thanks you both.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

both int[] and char[] are different object;there is no relationship.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

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
Sheriff

Joined: Sep 28, 2004
Posts: 18848
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
wood burning stoves
 
subject: Assigning a char array to an int array