• Post Reply Bookmark Topic Watch Topic
  • New Topic

initialise byte array to null  RSS feed

 
Sarone Thach
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
can someone tell me the fastest way to set all the values of a byte [] to null?
I would like an alternative to the below code if there is any.

thanks.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sarone,
Only object references can be null; bytes can be zero, though, as you obviously know, and that's what you meant.
In your code snippet, the loop is completely unnecessary; when you allocate the array, all the elements are guaranteed to be zero already.
If you have an array whose elements have already been set and you want to zero them again, you could use the static method java.util.Arrays.fill:
Arrays.fill(buffer, 0);
 
Sarone Thach
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes! that's exactly what i want. gee there are so many shortcuts in java. cool, thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!