java 参数传递有两种数据类型:
1,原生数据类型 8种基本数据类型。原生数据类型传递后会对值进行传递,不会对外部传递的变量做改变。
2,传递对象引用。
在java中,对于方法的参数传递,不管是原始数据类型还是引用数据类型,一律是串值:pass by value。
public void changeString(String str){ String str = "abc";}public static void main(String[] args){ String str = "xyz"; new 对象().chengeString(str); System.out.PRintln(str); // 输出“xyz”}---------------------------
静态变量
public class StaticTest { private static StaticTest st = new StaticTest(); pirvate static int count1; private static int count2 = 0; private StaticTest(){ count1++; count2++; } public static StaticTest getInstance(){ return st; } public static void main(String[] arge){ StaticTest st = StaticTest.getInstance(); System.out.println("count1: " + st.count1); System.out.println("count2: " + st.count2); } }程序编译执行结果?
输出结果:
count1:1
count2:0
执行顺序:
先执行main方法
再执行静态化部分,顺序执行,从上到下。
先执行 private static StaticTest st = new StaticTest();
在执行构造函数,这时构造函数把 count1和count2都进行++操作,完后 count1 = 1,count2 = 1。
在执行 public static int count1; 由于这一行并没有显示的被赋值操作,所以这是count1 = 1, 构造器里的++操作。
在执行 public static int count2; 由于这一行有显示的赋值操作,又把count2 设置为 0 。
所以如上输出结果。
-----------------------------------------
继承与初始化块?
public class Test{ pubilc static void main(String[] arge){ new Child(); } }class Parent{ static{ System.out.println("parent static block"); } public Parent(){ System.out.println("parent constructor"); } }class Child extends Parent{ static{ System.out.println("child static block"); } public Child(){ System.out.println("child constructor"); }}程序输出?
parent static block
child static block
parent constructor
child constructor
先调用静态初始化块,父类 子类。
在调用构造器 ,父类 子类。
super(); 显示的调用父类的构造方法,super(参数); 显示的调用带参数的父类构造方法。
新闻热点
疑难解答