Is it faster (or better) to declare an array inline in Java?

前端 未结 7 1190
慢半拍i
慢半拍i 2021-01-11 17:20

Consider the following two method calls that are nearly equivalent. Take note of the way the byte array is declared and allocated on both.

void Method1()
{
          


        
相关标签:
7条回答
  • 2021-01-11 18:07

    Both are the same but I would not recommend the second one as it is difficult to read to write and debug. Imagine you find this:

    byte [] bytearray = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    

    Could you tell if the size of the array is correct? Yes, but imagine with is:

    byte [] bytearray = new byte[25];
    

    It is far more easy.

    And imagine the case the array length is 100!

    0 讨论(0)
提交回复
热议问题