• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

a statement on primitive array from K&B

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can any one clarify me the part of below statement on primitive arrays from K&B.

"if you declare an int array, the reference variable you declare can be reassigned to any int array of any size, but cannot be reassigned to anything that is not an int array, including int value"
from this I have understood
int[] i;
int[] i1=new int[6];
byte[] b=new byte[6];
==> i=i1; //valid statement.
==> i=b; // invalid bcos though byte can fit in int, in arrays it is not possible.
here it continues the statement---
"including int value" putting me in confusion state.
explanation will be appreciated.


Thanks
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


if you declare an int array, the reference variable you declare ... cannot be reassigned to ... an ... int value


So

is not possible. I think, it's that easy :-)
[ May 21, 2007: Message edited by: Sasha Ruehmkorf ]
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

madhu

including int value means

int[] a=new int[10];
int k=5;
a=k;//line1

or
a=10;//line 2
we can't do like this.
Both line1 and line2 are illegal statements.

Thanks
Anil Kumar
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sasha,Anil

oh it is a simple and basic statement in arrays.
but i think that is not worth of mentioning along with this statement,
thats why I thought it means something else.
anyway thanks for your replies.

Thanks
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi madhu,

The reason that you can NOT assign primitive to array lies to the fact that arrays are objects even they are declared as primitives the array itself is an object and therefore it is not possible to assign primitives to objects.

Cheers,
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Omer has clearly said.

Madhu - Just to make you sure,


"if you declare an int array, the reference variable you declare can be reassigned to any int array of any size, {but cannot be reassigned to anything that is not an int array, including int value}"



Actually the phrase "including an int value" should be appended for the second part of the sentence and not the first part!

In K&B book also this information (as omer said) would be given as "eventhough it may be tempting you that a byte can very well fit into an int, but it is not possible in Arrays!".

HtH.
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic