• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Could you please tell what happens internally if i will say short temp[] = new short[0]

 
Ashuthosh san
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I know this may seems to be very simple but i am getting confused. But i want to know if i declare like this

short temp[] = new short[0]

what happens internally. Is temp will be cosidered as an empty list.


Regards
Asha
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its length is 0 means its having 0 elements,
and we cont put put the value also like temp[0]=2; it will give OutOfBoundException
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its length is 0 means its having 0 elements,
and we cont put the value also like temp[0]=2; it will give OutOfBoundException

Its like we have a temp object of type array of Short , But not have any elements(0 elements) in that array

if you see arrays in K&B SCJP Book, you will get good idea
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
short temp[] = new short[0]

Javais strictly typed language all arrays are checked at runtime specifying the index inside the brackets gives you the ability to hold of type values that you had declared.when you specify [0] its nothing but it is not capable to hold values its like an empty container of no use when you trying to assign the value during run time it through runtime exception
 
Ashuthosh san
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you!!

My requirement was to pass an empty short array to a method, so short temp[] = new short[0] is nothing but creating an empty array. Is it correct?

Thanks
Asha
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Its an array but of no use, means it can take only 0 elements
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So yes, it is an empty array. And empty arrays can still be useful - as parameters to methods. Especially in Collection.toArray(Object[]) - the type of the passed array determines the type of the returned array.
 
Ashuthosh san
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okie Rob, Thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic