免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 2906 | 回复: 25
打印 上一主题 下一主题

请问这段代码的输出为什么会是这样??谢谢 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-05-08 19:37 |只看该作者 |倒序浏览
package com.wondershow.cha6;


public class test {

    static Integer intg = new Integer(30);
   
    public static void main(String[] args) {
            test m = new test();
            Integer intg = new Integer(10);
            Integer intg1 = new Integer(20);
            int i = 0;;
           
            System.out.println( "i=" + i + " intg=" + intg + " intg1=" + intg1);
            m.method(intg,intg1,i);
            System.out.println( "i=" + i + " intg=" + intg + " intg1=" + intg1);
           
    }
   
    public void method(Integer a, Integer b, int c){
                a = new Integer(intg.intValue() + 10);
                b = test.intg;
                c += 10;
    }
}

输出
i=0 intg=10 intg1=20
i=0 intg=10 intg1=20




我的疑问是:intg和intg1不是对象么,java中对象变量不是传引用调用的么??

论坛徽章:
0
2 [报告]
发表于 2005-05-08 20:52 |只看该作者

请问这段代码的输出为什么会是这样??谢谢

Java里所有的参数都是传值的。

论坛徽章:
0
3 [报告]
发表于 2005-05-08 20:52 |只看该作者

请问这段代码的输出为什么会是这样??谢谢

两行打印是一样的,你可以,在method里把intg.intValue()打印出来看看,和在a的操作前,把a的值与操作后的值打印出来看看结果

论坛徽章:
0
4 [报告]
发表于 2005-05-08 21:42 |只看该作者

请问这段代码的输出为什么会是这样??谢谢

小小的改了一下程序
public class test {

   static Integer intg = new Integer(30);
   
   public static void main(String[] args) {
    test m = new test();
    Integer intg = new Integer(15);
    Integer intg1 = new Integer(20);
    int i = 0;;
   
    System.out.println( "i=" + i + " intg=" + intg + " intg1=" + intg1);
    m.method(intg,intg1,i);
    System.out.println( "i=" + i + " intg=" + intg + " intg1=" + intg1);
   
   }
   
   public void method(Integer a, Integer b, int c){
a = new Integer(intg.intValue() + 10);
System.out.println(a);
b = test.intg;
System.out.println(b);
c += 50;
System.out.println(c);
   }
}
运行结果为?
i=0 intg=15 intg1=20
40
30
50
i=0 intg=15 intg1=20
在运行时,intg已经指向了一个新的值,为什么用的还是初始的值啊?

论坛徽章:
0
5 [报告]
发表于 2005-05-08 22:03 |只看该作者

请问这段代码的输出为什么会是这样??谢谢

原帖由 "hisoka" 发表:
{
intg已经指向了一个新的值,为什么用的还是初始的值啊

论坛徽章:
0
6 [报告]
发表于 2005-05-08 22:36 |只看该作者

请问这段代码的输出为什么会是这样??谢谢

原帖由 "hisoka" 发表:
{
    test m = new test();
    Integer intg = new Integer(15);
    Integer intg1 = new Integer(20);
    int i = 0;;
   
    System.out.println( "i=" + i + " intg=" + intg + " intg1=" + int..........

Java里所有的参数都是传值的!

论坛徽章:
0
7 [报告]
发表于 2005-05-09 11:45 |只看该作者

请问这段代码的输出为什么会是这样??谢谢

越来越糊涂了,java里边不是分基本类型和引用类型么?
记得2种参数的传递是不一样的啊~

论坛徽章:
0
8 [报告]
发表于 2005-05-09 12:53 |只看该作者

请问这段代码的输出为什么会是这样??谢谢

没有区别,Java里的所有对象都是传值的。但是对于对象来说,你可以操作他的数据成员,这是object和primitive的区别。

论坛徽章:
0
9 [报告]
发表于 2005-05-09 19:35 |只看该作者

请问这段代码的输出为什么会是这样??谢谢

楼上的老兄,我的疑问和hisoka一样,你能不能说的清楚一点,谢谢!最好举个例子

论坛徽章:
0
10 [报告]
发表于 2005-05-09 20:37 |只看该作者

请问这段代码的输出为什么会是这样??谢谢

这个,好像没法解释的更具体了。就是java的规定,所有的参数都是传值的?不知道你的具体的疑问是什么?大家深入讨论一下?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP