Java面试宝典-2

传递与引用

package practice;

public class Test13 {
	public static void test(boolean test){
		test =! test;
		System.out.println("In Test Method test="+test);
	}
	  public static void main(String[] args) {
		
		  boolean test =true;
		  System.out.println("Before Test Method test="+test);
		  test(test);
		  System.out.println("After Test Method test="+test);
	}
	

}

Before Test Method test=true
In Test Method test=false
After Test Method test=true


以参数的形式传递简单类型的变量时,实际上是将参数的的值作为一个副本传入方法函数的,那么在函数中不管怎么改变参数的值,也只是改变了副本的值,而不是源值


package practice;

public class Test14 {
	public static void test(StringBuffer str){
		str.append(",world");
	}
   public static void main(String[] args) {
	StringBuffer str =new StringBuffer("hello");
	 test(str);
	 System.out.println(str);
}
}

hello,world


传递对象类型的变量时,通过引用副本(复制的钥匙)找到地址(仓库)并修改地址中的值,也就修改了对象

package practice;

public class Test14 {
	public static void test(String str){
		str=str+",world";
	}
   public static void main(String[] args) {
	String str =new String("hello");
	 test(str);
	 System.out.println(str);
}
}

hello

String也是对象类型,为何还是没有改变? 

这是由于String是final类型的,所以其实test方法里的str=str+“,world” 不是外面的str 也就是两个对象而不是同一个所以输出结果没变(个人拙见)


package practice;

public class Test14 {
	public static String test(String str){
		str=str+",world";
		return str;
	}
    public static void main(String[] args) {
	 String str =new String("hello");
	  str= test(str);
	 System.out.println(str);
}
}

hello,world



package practice;

public class Test15 {
   public static void main(String[] args) {
	Test15 t=new Test15();
	t.first();
}

public void first() {
	// TODO Auto-generated method stub
	 int i=5;
	 Value v=new Value();
	 v.i=25;//通过引用的副本改变了对象的值为25
	 second(v,i);
	 System.out.println(v.i);
}
public void second(Value v,int i){
	i=0;//5-->0
	v.i=20;//通过引用的副本改变了对象的值为25-->20
	Value val=new Value();
	v=val;//引用的副本指向了新的Object
	System.out.println(v.i+" "+i);//新的对象的i值 和second方法中的i值
}
}


class Value{
	public int i=15;
}

15 0
20

为什么输出的结果是20



输入输出流

输入一个数,计算平方,立方

package practice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test16 {
  
  public static void main(String[] args) {
	Result result =new Result();
	System.out.println("请输入一个整数:");
	int a=InputData.getInt();
	result.print(a);
}
}

class Result{
	void print(int d){
		System.out.println(d+"的平方:"+d*d);
		System.out.println(d+"的立方:"+d*d*d);
	}
}
class InputData{
	private static String s="";
	public static int getInt(){
		input();
		return Integer.parseInt(s);
	}
	public static void input(){
		BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
		try {
			s=bu.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值