今天无意中发现的一个错误, 不知道是我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的问题。 现在还不知道。我先不说我编译进的提示错误。 各位先看看能找到错误不?
最后送分送分还是送分。

解决方案 »

  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();修改成下面的
    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());另外一个出错的问题也是一样的
      

  2.   

    不能在int变量调用intValue。里面自动unbox,不会自动box,所以调用intValue出错了。 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"));        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"));
      

  3.   

    好久没弄JAVA,现在都没个环境,无知了。
      

  4.   

    好奇怪啊,为什么b1和b3显示错误,b2
    没错。好好看看。。好像蛮有意思的
      

  5.   

    错误找到了,自己的观点::
    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报错。。嘻嘻继续
      

  6.   

    b3似乎也是一样的问题,。。
    解决完毕。。
    good luck
      

  7.   

    什么IDE呀,LZ能说明白点?NetBeans吗?
      

  8.   

    eclipse 马上会报错b1和b3
    Cannot invoke intValue() on the primitive type int
      

  9.   

    Integer.valueOf("1") << 7中<<是什么个意思呀?
      

  10.   

    放假几天没看。不对。
    原因的确是自动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);
     
      

  11.   


    这只是在开发的时候遇到的问题。 上面只是随手写的测试代码。 主要问题是Integer对象的自动box的位移操作。现在项目做的是通过一个协议的进行数据解解析。 而代理是用C写的。传过来的数据在时候要一BIT一BIT的处理。结果就发现了这个问题。
      

  12.   

    byte b1 = (byte) new Integer((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();
      

  13.   

    byte b3 = (byte) (new Integer(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());
      

  14.   

    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());
      

  15.   


    Integer.valueOf("1") << 7 的值不是null?
      

  16.   

    Integer.valueOf("1") << 7  是左位移吧,,一位就乘2,最后结果是2的7次方=。=
      

  17.   


    对于b1和b3的赋值是有问题的,应该按照b2的赋值方式做才对.来接分了...
      

  18.   


    对于b1和b3的赋值是有问题的,应该按照b2的赋值方式做才对.来接分了...
      

  19.   

    Integer.valueOf("1") << 7的结果是null??????????
    还有您的byte好像越界了
      

  20.   

    无错误代码: O(∩_∩)O~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()));
      

  21.   

    就是要实现这样的结果
    byte b = (byte) c;
    分开来写 可读性比较高。
      

  22.   

    两个,一个英文的,一个翻译的 
    http://apicode.gicp.net/class.do?api=selectByfatherIndex&father=255
    http://apicodecn.gicp.net/class.do?api=selectByfatherIndex&father=255
      

  23.   


    对头 Integer.valueOf("1") << 7 = null。
      

  24.   

    一直没有明白楼主 要对对象进行操作,而不是数据,int型数据操作“<<”才有意义,楼主用的Integer是对象,没有任何意义
      

  25.   

    Integer可以进行移位操作码?
    学习下。
      

  26.   


    ....   Integer 有BOX UNBOX。  但这里好像没 UNBOX才会为null。
      

  27.   

    Integer.valueOf(1) << 7;
    应该没问题,这里可以UNBOX
      

  28.   


    我这用的java6.10 IDE是idea9结果是null。