朱海:一个小java代码,望高手指点

来源:百度文库 编辑:神马品牌网 时间:2024/04/27 22:42:56
int x=6
Integer a=new Integer();
a=(new Integer());
a.intValue;
System.out.println(a);
我想用Integer里的intValue方法取出值
初学者,问都不会问了,见笑了

兔九哥有个地方笔误了应该是
Integer a=new Integer(x);
System.out.println(a.intValue());

1. int x=6
2. Integer a=new Integer();
3. a=(new Integer());
4. a.intValue;
5. System.out.println(a);
能看出来你是想使用intValue来取值,但是这么写是不行的,在之前,你需要先好好学习一下方法的返还值得概念。
1句定义了一个名为x的整型变量,值是6。
2句生成了一个空的整型对象,用a来引用。
3句不知道你想干什么,你又用a来引用了一个新生成的空的对象,同时,原来引用的那个对象丢失,等待垃圾回收。
4句用intValue取出了里面的值,但是并没有赋给任何变量,于是丢失了。
5句直接输出了a的值,显然是不对的。
正确的语句如下:
int x=6;
Integer a=new Integer(a);
System.out.println(a.intValue);

。。。说的对!!!!哈哈