/**
* @(#)c1.java
*
*
* @author
* @version 1.00 2009/2/28
*/public class c1 {
/**
* Creates a new instance of <code>c1</code>.
*/
public c1() {
}
static void m1(int b[]){
int c[]={7,8,9,10};
b=c;
}
static void p1(int t[]){
int i=0;
for(i=0;i<t.length;i++)
{
System.out.println(t[i]);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int a[]={1,2,3,4};
p1(a);
System.out.println("----------");
m1(a);
p1(a);
}
}
结果:
1,2,3,4
1,2,3,4/**
* @(#)c1.java
*
*
* @author
* @version 1.00 2009/2/28
*/public class c1 {
/**
* Creates a new instance of <code>c1</code>.
*/
public c1() {
}
static void m1(int b[]){
int c[]={7,8,9,10};
// b=c;
for(int i=0;i<b.length;i++){
b[i]=c[i];
}
}
static void p1(int t[]){
int i=0;
for(i=0;i<t.length;i++)
{
System.out.println(t[i]);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int a[]={1,2,3,4};
p1(a);
System.out.println("----------");
m1(a);
p1(a);
}
}结果:
1,2,3,4
7,8,9,10
----------------------------------------------请问红色代码中
a=c
和
b[i]=c[i];
有什么区别的-----------------------------------------------------------------------
* @(#)c1.java
*
*
* @author
* @version 1.00 2009/2/28
*/public class c1 {
/**
* Creates a new instance of <code>c1</code>.
*/
public c1() {
}
static void m1(int b[]){
int c[]={7,8,9,10};
b=c;
}
static void p1(int t[]){
int i=0;
for(i=0;i<t.length;i++)
{
System.out.println(t[i]);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int a[]={1,2,3,4};
p1(a);
System.out.println("----------");
m1(a);
p1(a);
}
}
结果:
1,2,3,4
1,2,3,4/**
* @(#)c1.java
*
*
* @author
* @version 1.00 2009/2/28
*/public class c1 {
/**
* Creates a new instance of <code>c1</code>.
*/
public c1() {
}
static void m1(int b[]){
int c[]={7,8,9,10};
// b=c;
for(int i=0;i<b.length;i++){
b[i]=c[i];
}
}
static void p1(int t[]){
int i=0;
for(i=0;i<t.length;i++)
{
System.out.println(t[i]);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int a[]={1,2,3,4};
p1(a);
System.out.println("----------");
m1(a);
p1(a);
}
}结果:
1,2,3,4
7,8,9,10
----------------------------------------------请问红色代码中
a=c
和
b[i]=c[i];
有什么区别的-----------------------------------------------------------------------
解决方案 »
- JDBC执行SQL语句出错
- 三元运算符输出结果问题
- 线程题目
- JSP对MYSQL数据库进行插入操作,总失败!
- 我想要个用阴阳历转换函数,急用!!!
- 有没有人用java写过LDAP相关的?
- 难题~~~~~~~~
- !!!运行applet时出现java.security.AccessControlException: access denied ?????????!!!!!!1
- 我要在applet中显示一个tif文件,显示时需要有滚动条,运行时出现如下错误,救我!!!
- c/s方式,生成统计图形
- java 子类继承的成员的修饰符是否改变
- 当List<T>.isempty()==true,如何确定T的类型?
java中数组也是引用类型,所以直接写b或者c都是它本身的引用,
我举一个例子,把内存空间比作一个房间,而引用就相当于打开这个房间的钥匙。
值传递是这么一回事,把此内存空间连大小和值,复制一份和原始内存中一模一样的,就像你新开了一个和原始房间一模一样的房间,但是你在新房间里做任何事都不会影响到原始房间。引用传递是这么一回事,他相当于复制一把钥匙,也就是引用的副本给某人,也就是多个引用指向同一个对象,这样其中一个引用改变了对象的状态会反应到其他引用身上。在你第一个例子中:static void m1(int b[]){
int c[]={7,8,9,10};
b=c;
}
他只是复制一个引用b,但是此时你并没有通过b引用对对象进行任何操作,也就是你只是拿了房间钥匙但是没有打开房间,改变房间里的状态。此时,你把b引用指向了一个新的对象,相当于把之前那把钥匙丢掉,换了另一个房间的钥匙,引用虽然改变了,但是对象的状态并没有改变,所以也不会影响到其他的引用身上。第二个例子中:static void m1(int b[]){
int c[]={7,8,9,10};
// b=c;
for(int i=0;i <b.length;i++){
b[i]=c[i];
}
} 这里你已经通过引用b对数组对象进行了操作,相当于你进入了房间,改变了房间的状态,那么当另一个引用或者说另一个人进入这个房间时。当然看到的就是改变后的房间的状态。
/**
* Creates a new instance of <code>c1 </code>.
*/
public c1() {
}
static int[] m1(int b[]){
int c[]={7,8,9,10};
b=c;
return b;
}
static void p1(int t[]){
int i=0;
for(i=0;i <t.length;i++)
{
System.out.println(t[i]);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int a[]={1,2,3,4};
p1(a);
System.out.println("----------");
int[] c=m1(a);
p1(c);
}
}
结果
1 2 3 4
5 6 7 8
知道为什么么
/**
* Creates a new instance of <code>c1 </code>.
*/
public c1() {
}
static int[] m1(int b[]){
int c[]={7,8,9,10};
b=c;
return b;
}
static void p1(int t[]){
int i=0;
for(i=0;i <t.length;i++)
{
System.out.println(t[i]);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int a[]={1,2,3,4};
p1(a);
System.out.println("----------");
int[] c=m1(a);
p1(c);
}
} 对比
public class c1 {
/**
* Creates a new instance of <code>c1 </code>.
*/
public c1() {
}
static void m1(int b[]){
int c[]={7,8,9,10};
b=c;
}
static void p1(int t[]){
int i=0;
for(i=0;i <t.length;i++)
{
System.out.println(t[i]);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int a[]={1,2,3,4};
p1(a);
System.out.println("----------");
m1(a);
p1(a);
}
} 结果为什么不同