如题,变量的赋值操作和使用有什么区别呢?给一个变量赋值是不是对这个变量的使用?如果不是原因是什么?

解决方案 »

  1.   

    个人理解变量的赋值不算使用,变量的赋值可以理解为变量的初始化,变量的使用可以理解为参与了某些运算或者方法的调用,就是说既然涉及到使用,就应除了变量本身还有第三方变量或者算法参与,使用了某个变量必然对整个程序产生了一定影响,这才叫使用,比如变量String s,调用了s.split方法,这算使用,一个未曾使用过的变量String s 给它赋值 String s = "aa";这个操作对程序没啥影响,也没有第三方代码调用,虽然对这个变量进行了操作,这不能算是使用
      

  2.   


    int i=0;
    for(int j=0;j<10;j++,i++){
        i=i;
    }
    System.out.println(i);
    看着个例子,输出i 为10,表明赋值也是被使用,赋值是使用的一种情况,由于i++,使用后加一,可以得出这个结论
      

  3.   


    public class Test{
           static{      
                 x=9;
           }
           static int x;
    }这样能通过编译,也能运行,
    public class Test{
           static{      
                System.out.println(x);
           }
           static int x;
    }这样却无法通过编译,这是什么原因?
      

  4.   


    public class Test{
           static{      
                 x=9;
           }
           static int x;
    }这样能通过编译,也能运行,
    public class Test{
           static{      
                System.out.println(x);
           }
           static int x;
    }这样却无法通过编译,这是什么原因?
      

  5.   


    public class Test{
           static{      
                 x=9;
           }
           static int x;
    }这样能通过编译,也能运行,
    public class Test{
           static{      
                System.out.println(x);
           }
           static int x;
    }这样却无法通过编译,这是什么原因?
      

  6.   


    public class Test{
           static{      
                 x=9;
           }
           static int x;
    }这样能通过编译,也能运行,
    public class Test{
           static{      
                System.out.println(x);
           }
           static int x;
    }这样却无法通过编译,这是什么原因?对于静态变量和实例变量,在没有声明之前,只可以存在在赋值运算符(=)的左边.
    对于局部变量,必须先声明,才能赋值,然后才能使用.
      

  7.   


    public class Test{
           static{      
                 x=9;
           }
           static int x;
    }这样能通过编译,也能运行,
    public class Test{
           static{      
                System.out.println(x);
           }
           static int x;
    }这样却无法通过编译,这是什么原因?
    输出语句里面的变量x你没有定义就直接使用了。编译就过不去了,程序是从上到下运行的。
      

  8.   

    在一个不存在继承的类中:初始化static变量 → 执行static初始化快 → 初始化普通成员变量(如果有赋值语句) → 执行普通初始化块 → 构造方法static  int x;  // 相当于给自己孩子起个名,但是孩子并没有出声x=5; //相当于孩子已经取名 ,并且孩子已经出生了