java short,integer,long performance

前端 未结 6 677
我在风中等你
我在风中等你 2021-01-01 23:27

I read that JVM stores internally short, integer and long as 4 bytes. I read it from an article from the year 2000, so I don\'t know how true it is now.

For the newe

6条回答
  •  有刺的猬
    2021-01-02 00:10

    long  64 –9,223,372,036,854,775,808 to 9 ,223,372,036,854,775,807 
    int   32 –2,147,483,648 to 2,147,483,647 
    short 16 –32,768 to 32,767 
    byte   8 –128 to 127 
    

    Use what you need, I would think shorts are rarely used due to the small range and it is in big-endian format.

    Any performance gain would be minimal, but like I said if your application requires a range more then that of a short go with int. The long type may be too extremly large for you; but again it all depends on your application.

    You should only use short if you have a concern over space (memory) otherwise use int (in most cases). If you are creating arrays and such try it out by declaring arrays of type int and short. Short will use 1/2 of the space as opposed to the int. But if you run the tests based on speed / performance you will see little to no difference (if you are dealing with Arrays), in addition, the only thing you save is space.

    Also being that a commentor mentioned long because a long is 64 bits. You will not be able to store the size of a long in 4 bytes (notice the range of long).

提交回复
热议问题