aspose file tools*
The moose likes Beginning Java and the fly likes Could you please tell what happens internally if i will say short temp[] = new short[0] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Could you please tell what happens internally if i will say short temp[] = new short[0]" Watch "Could you please tell what happens internally if i will say short temp[] = new short[0]" New topic
Author

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

Ashuthosh san
Ranch Hand

Joined: Jan 28, 2009
Posts: 35
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

Joined: Feb 18, 2008
Posts: 289

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

-Chiru
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 289

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

Joined: Apr 06, 2009
Posts: 296
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


Creativity is nothing but Breaking Rules
Ashuthosh san
Ranch Hand

Joined: Jan 28, 2009
Posts: 35
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

Joined: Feb 18, 2008
Posts: 289

No. Its an array but of no use, means it can take only 0 elements
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ashuthosh san
Ranch Hand

Joined: Jan 28, 2009
Posts: 35
Okie Rob, Thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Could you please tell what happens internally if i will say short temp[] = new short[0]