zoukankan      html  css  js  c++  java
  • Java的值传递机制

    变量的赋值

    /*
     *
     * 关于变量的赋值:
     *
     *  如果变量是基本数据类型,此时赋值的是变量所保存的数据值。
     *  如果变量是引用数据类型,此时赋值的是变量所保存的数据的地址值。
     *
     */
    public class ValueTransferTest {
    
        public static void main(String[] args) {
    
            System.out.println("***********基本数据类型:****************");
            int m = 10;
            int n = m;
    
            System.out.println("m = " + m + ", n = " + n);
    
            n = 20;
    
            System.out.println("m = " + m + ", n = " + n);
    
            System.out.println("***********引用数据类型:****************");
    
            Order o1 = new Order();
            o1.orderId = 1001;
    
            Order o2 = o1;//赋值以后,o1和o2的地址值相同,都指向了堆空间中同一个对象实体。
    
            System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " +o2.orderId);
    
            o2.orderId = 1002;
    
            System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " +o2.orderId);
    
        }
    
    }
    
    class Order{
    
        int orderId;
    
    }
    
    输出结果:
    
    ***********基本数据类型:****************
    m = 10, n = 10
    m = 10, n = 20
    ***********引用数据类型:****************
    o1.orderId = 1001,o2.orderId = 1001
    o1.orderId = 1002,o2.orderId = 1002
    
    

    参数是基本类型

    /*
     * 方法的形参的传递机制:值传递
     * 
     * 1.形参:方法定义时,声明的小括号内的参数
     *   实参:方法调用时,实际传递给形参的数据
     * 
     * 2.值传递机制:
     * 如果参数是基本数据类型,此时实参赋给形参的是实参真实存储的数据值。
     * 如果参数是引用数据类型,此时实参赋给形参的是实参存储数据的地址值。
     * 
     */
    public class ValueTransferTest1 {
    	public static void main(String[] args) {
    		
    		int m = 10;
    		int n = 20;
    		
    		System.out.println("m = " + m + ", n = " + n);
    		//交换两个变量的值的操作
    //		int temp = m ;
    //		m = n;
    //		n = temp;
    		
    		ValueTransferTest1 test = new ValueTransferTest1();
    		test.swap(m, n);
    		
    		System.out.println("m = " + m + ", n = " + n);
    		
    		
    	}
    	
    	
    	public void swap(int m,int n){
    		int temp = m ;
    		m = n;
    		n = temp;
    	}
    }
    
    输出结果:
    m = 10, n = 20
    m = 10, n = 20
    
    分析:
          swap方法执行之后,会弹出栈,调用swap方法后面的输出语句,输出的是main方法中的m、n的值,
    

    参数是引用类型

    public class ValueTransferTest2 {
    	
    	public static void main(String[] args) {
    		
    		Data data = new Data();
    		
    		data.m = 10;
    		data.n = 20;
    		
    		System.out.println("m = " + data.m + ", n = " + data.n);
    		
    		//交换m和n的值
    //		int temp = data.m;
    //		data.m = data.n;
    //		data.n = temp;
    		
    		ValueTransferTest2 test = new ValueTransferTest2();
    		test.swap(data);
    		
    		
    		System.out.println("m = " + data.m + ", n = " + data.n);
    		
    	}
    	
    	public void swap(Data data){
    		int temp = data.m;
    		data.m = data.n;
    		data.n = temp;
    	}
    	
    }
    
    class Data{
    	
    	int m;
    	int n;
    	
    }
    
    输出结果:
              m = 10, n = 20
              m = 20, n = 10
    
    

    练习:

    public class TransferTest3 {
          public static void main(String args[]) {
                TransferTest3 test = new TransferTest3();
                test.first();
          }
          public void first() {
                int i = 5;
                Value v = new Value();
                v.i = 25;
                second(v, i);
                System.out.println(v.i);
          }
          public void second(Value v, int i) {
                i = 0;
                v.i = 20;
                Value val = new Value();
                v = val;
                System.out.println(v.i + " " + i);
          } 
    }
    
    class Value {
          int i = 15;
    }
    
    输出结果:
          15 0 20
    

  • 相关阅读:
    优化SQL查询:如何写出高性能SQL语句
    提高SQL执行效率的16种方法
    Spring Ioc DI 原理
    java内存泄漏
    转:js闭包
    LeetCode Best Time to Buy and Sell Stock III
    LeetCode Best Time to Buy and Sell Stock with Cooldown
    LeetCode Length of Longest Fibonacci Subsequence
    LeetCode Divisor Game
    LeetCode Sum of Even Numbers After Queries
  • 原文地址:https://www.cnblogs.com/ITHSZ/p/13223201.html
Copyright © 2011-2022 走看看