怎样改变对象Integer的值? Integer x=new Integer(5); x=new Integer(x.intValue()+1 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 难道非要借助new 才能改变对象的值,有没有更直接的办法? Integer这中wrapper class一旦分配了值,是不能如你所想的,不能改变值 Java的这种做法令我很不习惯,C++被改得莫名其妙 public class HelloWorld { public static void test(Integer x, Integer y) { x=new Integer(x.intValue()+1); y=new Integer(y.intValue()+1); } public static void main(String args[]) { System.out.println("HelloWorld"); for (int i=0;i<args.length;i++) System.out.println(args[i]); Integer x=new Integer(5); Integer y=new Integer(8); test(x,y); System.out.println(x.intValue()); System.out.println(y.intValue()); } } 结果为什么仍x=5,y=8? // 上面为什么?应该:public class HelloWorld { public static int x,y; public static void test() { x++; y++; } public static void main(String args[]) { System.out.println("HelloWorld"); for (int i=0;i<args.length;i++) System.out.println(args[i]); x=5; y=8; test(); System.out.println(x); System.out.println(y); } } java是按值传递,用基本类型的对象封装器无法改变原值!《java核心技术 卷I》134页有详述。 那象Integer这样的对象有何用途? java static修饰符什么意思? 新手求助:操作数据库时从表里只能取到最后一条记录 求助~SWT中dnd拖拽图标的问题 如何在非构造时期设置JTree的根节点 JNative中参数的问题。 ASII如何转化为字符? 这句程序什么意思? 请问哪有《J2EE编程指南(1.3版)》下载? 强烈CSDN建议开《操作系统开发》论坛! 不能运行之问题 十进制到十六进制的转换 60分 可加~~~~ 当前日期为:2001-12-23,为什么new date(2001,12,23).getTime()与new Date().getTime()不相等?搞不懂呀?
有没有更直接的办法?
C++被改得莫名其妙
{
public static void test(Integer x, Integer y)
{
x=new Integer(x.intValue()+1);
y=new Integer(y.intValue()+1);
}
public static void main(String args[])
{
System.out.println("HelloWorld");
for (int i=0;i<args.length;i++)
System.out.println(args[i]);
Integer x=new Integer(5);
Integer y=new Integer(8);
test(x,y);
System.out.println(x.intValue());
System.out.println(y.intValue());
}
}
结果为什么仍x=5,y=8?
应该:
public class HelloWorld
{
public static int x,y; public static void test()
{
x++;
y++;
}
public static void main(String args[])
{
System.out.println("HelloWorld");
for (int i=0;i<args.length;i++)
System.out.println(args[i]);
x=5;
y=8;
test();
System.out.println(x);
System.out.println(y);
}
}