• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array Confusion  RSS feed

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me that why we have to use
code=java][myDogs[0]=new Dog();[]
beacuse I am confusing with all this stuff while comparing with integer type array
full code is..
 
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dog[] mydogs=new Dog[3];
This allocates three references to Dog objects and initializes them to null. That means that mydogs[0] will return a null reference instead of a reference to an actual Dog object.
 
Sheriff
Posts: 4930
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piyush Chaudhary,

Welcome to JavaRanch.

One important point to remember, please UseCodeTags (<- link) when you post code. It makes your code more readable.
I have added them for you this time. Doesn't it look better now?
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why we don't use the same process in integer type of array? its just simple going in int type
int arr[]=new int[0];
thats all, we don't have to mention
arr[0]=new int();
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, Liutauras Vilda.. Actually I am new in this forum. That's why its going some bitter.
 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean integer or Integer? The two are different.
You cannot write new int(); because an int does not have a constructor. You can write new int[123]; and get a 123‑element array, but that is different from what you wrote.
By the way, the type of an array of ints is int[], so it is poor style to write
int arr[]...
and good style to write
int[] arr...
 
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the coderanch!!!

Actually you are getting a little bit confused. See if you make an array and if that array is holding primitive data type then it will be assigned accordingly like if it is int type then it will hold int data (it is not a reference variable ,that you will assign an object to it)
int [] arr=new int[3];
The above will only create an array which is able to hold int type data
Dog [] mydogs=new Dog[3];
The above will create an array which has reference variable
So you can assign object to it

mydogs[0]=new Dog();
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now some additional information apart from your confusion
Everything in java except primitive data type's are treated as object like array
And as java has made programmer free from defining size at compile time you can assign memory at runtime by "new" keyword
As it treat array as object so there must be some class defining blueprint of it.Proxy class are used for this purpose
 
Piyush Chaudhary
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to All.. Especially Sachin Tripathi. You got it right thats why I got it correct.. Thanks
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote: . . .Proxy class are used for this purpose
Where did you get that from?
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java array are objects so there must be some class defining blueprint of it
I have been taught proxy classes does that,which implement serializable and extends Object
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!