账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    一道JAVA面试题,关于int和Integer,String和StringBuffer,有点不太懂。
    45
    0

    题目是这样。

    public class T_Three {
        public void increment(int a, Integer b, String c, StringBuffer d) {
            a++;
            b++;
            c = c + " world";
            d.append(" world");
        }
    
        public static void main(String[] args) {
            int a = 0;
            Integer b = new Integer(0);
            String c = "hello";
            StringBuffer d = new StringBuffer("hello");
            new T_Three().increment(a, b, c, d);
            System.out.println("a=" + a + " b=" + b + " c=" + c + " d=" + d);
        }
    }
    

    问输出的结果
    a=? b=? c=? d=?

    然后我在IDE上试了一下。输出的结果是:

    a=0 b=0 c=hello d=hello world

    请问是为什么呢?为什么a和b的自增没有增加?为什么String没有拼接,为什么StringBuffer的append却起了作用?

    Stringbuffer我个人理解是append操作的就是传进来的对象,所以对其起了作用。
    c没有拼接是因为操作的是函数自身的c,并不影响外面的c。
    可是Integer和int我就不太懂了。

    不知道我理解对不对,希望各位指导一下。

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • Favorite.莉 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部