1.例子:
public class ArrayOfPrimitives{
public static void main(String[] args){
int[] a1={1,2,3,4,5};
int[] a2;
a2=a1;
for(int i=0;i<a2.length;i++)
a2[i] = a2[i]+1;
for(int i=0;i<a1.length;i++)
print("a1["+i+"]= "+a[i]);
}
}
//输出:
a1[0] = 2;
a1[1] = 3;
a1[2] = 4;
a1[3] = 5;
a1[4] = 6;
a2= a1;复制了一个引用,相当于a2和a1是相同数组的别名,指向同一个数组,那么对a2的修改也是对a1的修改。
对象的初始化顺序是什么?
1.先变量。 在变量中,先初始化静态变量,后是非静态变量。注意:无论创建多少对象,静态数据都只占一份存储区域,静态代码只执行一次。2.后方法。 无论创建多少个对象,静态数据都只占一份存储区域,静态代码只执行一次
示例代码package cn.hdu.li;
import sun.tools.jconsole.Tab;
import java.util.Scanner;
class Bowl{
Bowl(int marker){
System.out.println("Bowl(" + marker + ")");
}
void f1(int marker) {
System.out.println("f1(" + marker + ")");
}
}
class Table{
static Bowl bowl1 = new Bowl(1);
Table(){
System.out.println("Table()");
bowl2.f1(1);
}
void f2(int marker) {
System.out.println("f2(" + marker + ")");
}
static Bowl bowl2 = new Bowl(2);
}
class Cupboard{
Bowl bowl3 = new Bowl(3);
static Bowl bowl4 = new Bowl(4);
Cupboard() {
System.out.println("Cupborad()");
bowl4.f1(2);
}
void f3(int marker) {
System.out.println("f3(" + marker + ")");
}
static Bowl bowl5 = new Bowl(5);
}
public class Welcome {
public static void main(String[] args) {
System.out.println("Creating new Cupboard() in main");
new Cupboard();
System.out.println("Creating new Cupboard() in main");
new Cupboard();
table.f2(1);
cupboard.f3(1);
}
static Table table = new Table();
static Cupboard cupboard = new Cupboard();
}输出:Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupborad()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupborad()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupborad()
f1(2)
f2(1)
f3(1)