java 64 bit integer_Java 64bits - Integer vs Short

tl;dr

Unless you have a very special use-case, just use Integer (or int).

Just use Integer

For most common business-oriented Java apps, common practice is to just use the 32-bit int or Integer without much further thought, unless you know you will have values over 2 billion in which case use long or Long. On modern conventional hardware, there is little reason to fret over using short/Short.

Special cases

But since you asked “is there anything else to take into consideration at time to chose”, here are three special cases: enforcing limits, porting C code, and alternative hardware.

Enforce limit

If you want to enforce the limits of a short or Short, use those types. Choose these when you know you should have only values less than approximately 32,000, and want the compiler or runtime JVM to enforce that.

For example, add one to 101:

short oneOhOnePlusOne = ( (short) 101 + (short) 1 ) ;

System.out.println( "oneOhOnePlusOne: " + oneOhOnePlusOne ) ;

102

Next, try to exceed the limit to see the compiler enforce the limit.

short shortMaxPlusOne = ( Short.MAX_VALUE + (short) 1 ) ;

System.out.println( "shortMaxPlusOne: " + shortMaxPlusOne ) ;

error: incompatible types: possible lossy conversion from int to short

Porting C code

These various numeric types were likely put in Java to make easier to port C code, easier both both practically and psychologically. The inventors of Java were well aware that during that era most every attempt at a new programming language failed with the criticism that it was “not like C”. Thus Objective-C (an inspiration for Java), and thus the monstrosity that is C++.

So, indeed, if you are porting C code, use the matching type to replicate behavior.

By the way… Technically, C does not actually define the size of its numeric types. In practice virtually every implementation of C uses the sizes as seen in Java.

FYI, even the most modern languages such as Swift and Kotlin have built-in numeric types for 8, 16, 32, and 64 bit integers.

More restrictive hardware

If there is any chance your app might run on other hardware not based on x86-64, then you may want to use these types. Alternate implementations of Java for such hardware might optimize better for the smaller types.

Primitive versus Object

Array of primitives are preferable for saving memory allocated

First of all, don't stress out on this. Don't fall into the trap of premature optimization. On conventional hardware with most common apps, any savings of memory on using primitives versus objects and arrays versus collections will be insignificant. Use the type (primitive/object) and structure (array/collection) appropriate to your coding context.

My practice:

In my code, objects.

Use only objects (Integer, not int) within my own classes. I have two reasons. Firstly, I am among the camp of Object fans that wish Java were pure OOP without any primitives. (Indeed, there is research ongoing to see if primitives can virtually disappear in far-future versions of Java.) Secondly, I find when I used a primitive, I end up needing to use it in a context demanding objects, such as with collections.

With others’ code, use primitives if they do.

Outside my own classes, I do not force the issue, as undue auto-boxing is senseless. If external code (not my own) is using primitives, I use primitives in that context.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值