Java pass by value

Source: Internet
Author: User
package com.alogrithm.test2;public class Refs  {   public static void main(String[] args)  {      MyPoint p1 = new MyPoint(10, 20);      MyPoint p2 = new MyPoint(30, 40);      System.out.println("Before swap() p1 is " + p1 + ", and p2 is " + p2);      swap(p1, p2);      System.out.println("After swap() p1 is " + p1 + ", and p2 is " + p2);      System.out.println("----------------------------------------------");      System.out.println("Before change_1(), p1 is " + p1);      change_1(p1);      System.out.println("After change_1(), p1 is " + p1);      System.out.println("----------------------------------------------");      System.out.println("Before change_2(), p1 is " + p1);      change_2(p1);      System.out.println("After change_2(), p1 is " + p1);      System.out.println("----------------------------------------------");       return;   }   public static void swap(MyPoint arg1, MyPoint arg2)  {      MyPoint temp;      temp = arg1;      arg1 = arg2;      arg2 = temp;      System.out.println("In swap() p1 is " + arg1 + ", and p2 is " + arg2);   }   public static void change_1(MyPoint arg1)  {      arg1 = new MyPoint(100, 100);      System.out.println("In change_1() p1 is " + arg1);   }   public static void change_2(MyPoint arg1)  {      arg1.x = 100;      arg1.y = 101;      System.out.println("In change_1() p1 is " + arg1);   }}class MyPoint  {      public Integer x, y;      public MyPoint(Integer arg_x, Integer arg_y)  {         x = arg_x;         y = arg_y;      }      public String toString()  {         return "MyPoint[x=" + x + ",y=" + y + "]";      }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.