今天无意中发现的一个错误, 不知道是我IDE的问题,还是JAVA自己的问题。看看几行平时不在意思的代码:
int c = Integer.valueOf("1") << 7 +
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1");
byte b = (byte) c;
byte b1 = (byte) (Integer.valueOf("1") << 7 +
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1")).intValue(); byte b2 = (byte) (Integer.valueOf("1").intValue() << 7 +
Integer.valueOf("1").intValue() << 6 +
Integer.valueOf("1").intValue() << 5 +
Integer.valueOf("1").intValue() << 4 +
Integer.valueOf("1").intValue() << 3 +
Integer.valueOf("1").intValue() << 2 +
Integer.valueOf("1").intValue() << 1 +
Integer.valueOf("1").intValue()); byte b3 = (byte) ((Integer.valueOf("1") << 7 +
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1")).intValue());
问题描述:
我用的IDE是 IDEA9。 代码本身不会提示错误。编译是我出现错误。(而不是运行错误,IDE没有提示,而且看起来也没有什么出错的)。有可能是我IDE的问题。 现在还不知道。我先不说我编译进的提示错误。 各位先看看能找到错误不?
最后送分送分还是送分。
int c = Integer.valueOf("1") << 7 +
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1");
byte b = (byte) c;
byte b1 = (byte) (Integer.valueOf("1") << 7 +
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1")).intValue(); byte b2 = (byte) (Integer.valueOf("1").intValue() << 7 +
Integer.valueOf("1").intValue() << 6 +
Integer.valueOf("1").intValue() << 5 +
Integer.valueOf("1").intValue() << 4 +
Integer.valueOf("1").intValue() << 3 +
Integer.valueOf("1").intValue() << 2 +
Integer.valueOf("1").intValue() << 1 +
Integer.valueOf("1").intValue()); byte b3 = (byte) ((Integer.valueOf("1") << 7 +
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1")).intValue());
问题描述:
我用的IDE是 IDEA9。 代码本身不会提示错误。编译是我出现错误。(而不是运行错误,IDE没有提示,而且看起来也没有什么出错的)。有可能是我IDE的问题。 现在还不知道。我先不说我编译进的提示错误。 各位先看看能找到错误不?
最后送分送分还是送分。
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1")).intValue();修改成下面的
byte b1 = (byte) (Integer.valueOf("1") << 7 +
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1").intValue());另外一个出错的问题也是一样的
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1");
byte b = (byte) c;
byte b1 = (byte) (Integer.valueOf("1") << 7 +
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1")); byte b2 = (byte) (Integer.valueOf("1").intValue() << 7 +
Integer.valueOf("1").intValue() << 6 +
Integer.valueOf("1").intValue() << 5 +
Integer.valueOf("1").intValue() << 4 +
Integer.valueOf("1").intValue() << 3 +
Integer.valueOf("1").intValue() << 2 +
Integer.valueOf("1").intValue() << 1 +
Integer.valueOf("1").intValue()); byte b3 = (byte) (Integer.valueOf("1") << 7 +
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1"));
没错。好好看看。。好像蛮有意思的
Integer.valueOf("1")返回的是Integer类型
c是int,因此可以知道b1中Integer.valueOf("1") << 7 +
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1"
是Int型,Int是没有intValue()这个方法的,这是Integer的方法,查过javadoc后找到的解决了b1报错。。嘻嘻继续
解决完毕。。
good luck
Cannot invoke intValue() on the primitive type int
原因的确是自动box unbox的问题。Integer.valueOf("1") << 7 编译的时候不会出错是因为 Integer.valueOf("1")会自动unbox。 但是测试过后它并没有自动unbox。Integer.valueOf("1") << 7的结果是null。 我想他是对对象做了移位操作才会出现null,所以我判定Integer.valueOf("1")在位操作运算是并没有进行自动unbox。 正确的写法应该是:Integer.valueOf("1").intValue() << 7。
最后还要注意+与<<的优先级问题:
byte b = (Integer.valueOf(request.getParameter("sysRebootFlag")).intValue() << 7) +
(Integer.valueOf(request.getParameter("sysFactorySettingFlag")).intValue() << 6) +
(Integer.valueOf(request.getParameter("sysDevIpFlag")).intValue() << 5) +
(Integer.valueOf(request.getParameter("sysSaveConfigFlag")).intValue() << 4) +
(Integer.valueOf(request.getParameter("wirelessComputerServerIpFlag")).intValue() << 3) +
(Integer.valueOf(request.getParameter("wirelessComputerServerPortFlag")).intValue() << 2) +
(Integer.valueOf(request.getParameter("sysDescrFlag")).intValue() << 1) +
(Integer.valueOf(request.getParameter("sysPowerOutputTypeFlag")).intValue())); flag[1] = (byte) ((Integer.valueOf(request.getParameter("sysCommunicationTypeFlag")).intValue() << 7) +
(Integer.valueOf(request.getParameter("sysAlarmDetectSliceFlag")).intValue() << 6) +
(Integer.valueOf(request.getParameter("sysOutOfMaxMaxLimitPowerOffFlag")).intValue() << 5);
这只是在开发的时候遇到的问题。 上面只是随手写的测试代码。 主要问题是Integer对象的自动box的位移操作。现在项目做的是通过一个协议的进行数据解解析。 而代理是用C写的。传过来的数据在时候要一BIT一BIT的处理。结果就发现了这个问题。
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1"))).intValue();
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1")).intValue());
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1"))
.intValue());Integer.valueOf("1") << 7 的值不是null?
对于b1和b3的赋值是有问题的,应该按照b2的赋值方式做才对.来接分了...
对于b1和b3的赋值是有问题的,应该按照b2的赋值方式做才对.来接分了...
还有您的byte好像越界了
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1");
byte b = (byte) c; byte b1 = (byte)(Integer.valueOf("1") << 7 +
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1").intValue()); byte b2 = (byte) (Integer.valueOf("1").intValue() << 7 +
Integer.valueOf("1").intValue() << 6 +
Integer.valueOf("1").intValue() << 5 +
Integer.valueOf("1").intValue() << 4 +
Integer.valueOf("1").intValue() << 3 +
Integer.valueOf("1").intValue() << 2 +
Integer.valueOf("1").intValue() << 1 +
Integer.valueOf("1").intValue()); byte b3 = (byte) ((Integer.valueOf("1") << 7 +
Integer.valueOf("1") << 6 +
Integer.valueOf("1") << 5 +
Integer.valueOf("1") << 4 +
Integer.valueOf("1") << 3 +
Integer.valueOf("1") << 2 +
Integer.valueOf("1") << 1 +
Integer.valueOf("1").intValue()));
byte b = (byte) c;
分开来写 可读性比较高。
http://apicode.gicp.net/class.do?api=selectByfatherIndex&father=255
http://apicodecn.gicp.net/class.do?api=selectByfatherIndex&father=255
对头 Integer.valueOf("1") << 7 = null。
学习下。
.... Integer 有BOX UNBOX。 但这里好像没 UNBOX才会为null。
应该没问题,这里可以UNBOX
我这用的java6.10 IDE是idea9结果是null。