| Author |
problem in array assignment
|
rohit shekhar
Ranch Hand
Joined: Mar 05, 2010
Posts: 31
|
|
we can do the following assignment without any error :
but it gives error in the following code
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
Your two examples are not at all the same - kind of an
apples versus teeth comparison.
Jim ... ...
|
BEE MBA PMP SCJP-6
|
 |
Harpreet Singh janda
Ranch Hand
Joined: Jan 14, 2010
Posts: 317
|
|
Primitives and primitive array behaves in different manner.
You can't assign one type of primitive array to other type of array but you can assign one type of primitive to other type of primitive (with some restrictions).
So when you are writing a[0]='b' this means you are assigning a value to 0th location of the array and it is legal because. int i = 'b' is legal but assigning a char array to int array reference is not legal.
|
 |
Prithvi Sehgal
Ranch Hand
Joined: Oct 13, 2009
Posts: 771
|
|
Hi,
Character values can implicitly promoted to integers. But that is not the case with array references itself.
Assignment a character value to an integer and assigning a character array to an integer or vice versa
are two entirely different scenario's.
Best Regards,
|
Prithvi/Beenish,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
|
 |
Stephen Davies
Ranch Hand
Joined: Jul 23, 2008
Posts: 352
|
|
Jim Hoglund wrote:Your two examples are not at all the same - kind of an
apples versus teeth comparison.
Jim ...  ...
Absolutely, and here you are trying to assign an array of type a to an array of type b. Array objects are not the same as primitives! In fact the behavior is common to most objects, for example the wrapper classes.
|
be a well encapsulated person, don't expose your privates, unless you public void getWife()!
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Jim Hoglund wrote: kind of an apples versus teeth comparison.
Interesting turn of phrase...
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Rajeev Rnair
Ranch Hand
Joined: Mar 22, 2010
Posts: 308
|
|
Both array types are different. You cannot assigna char[] to an int[]. However you can assign a char value to an int. Also you can assign a int value to a char when the int value is 0<= i<= 65535! (Keep in mind char value ranges from 0 to 65535)
Also in your second code you are NOT initializing the local variable int[] a. Local variables should be initialized before you start using it!
hope this is clear!
|
SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
|
 |
 |
|
|
subject: problem in array assignment
|
|
|