Use of method parameters in Java se learning

Source: Internet
Author: User

This article is a summary of the study on the web and its own little practice, thank you for your selfless sharing.

The Java programming language does not use reference calls to objects, in fact, object references are Value Passing .

In the Java programming language, the use of method parameters:

1, a method can't Modify the parameters of a basic data type (i.e. numeric and Boolean);

2, a method can change the state of an object parameter;

3. A method cannot be implemented so that the object argument references a new object.


Here is the test code

/p>

Package Se;public class Paramtest {/** * <p> * </p> * @author Zhangjunshuai * @date 2014-10-29 Morning 10:42:39 * @p Aram args */public static void main (string[] args) {///1, Experiment one SYSTEM.OUT.PRINTLN ("Testing Triplevalue");d ouble percent = 10; System.out.println ("before:percent=" +percent); Triplevalue (percent); System.out.println ("after:percent=" +percent);//2, Experiment two System.out.println ("\ntesting triplevalue Object"); StringBuffer percentd = new StringBuffer ("Start ——————"); System.out.println ("before:percent=" +percentd); Triplevalue (PERCENTD); System.out.println ("after:percent=" +percentd);//2.1, experiment two test string Object System.out.println ("\ntesting triplevalue Object "); String percents = "string start ——————"; System.out.println ("before:percent=" +percents); Triplevalue (percents); System.out.println ("after:percent=" +percents);//3, Experiment 3system.out.println ("\ntesting triplesalary:"); Employee Harry = new Employee ("Harry", 50000); System.out.println ("before:salary=" +harry.getsalary ()); Triplesalary (Harry); System.out.println ("AfteR:salary= "+harry.getsalary ()),//4, experimental 4system.out.println (" \ntesting swap: "); Employee A = new Employee ("Alice", 7000); Employee B = New Employee ("Bob", 6000); System.out.println ("before:a=" +a.getname ()); System.out.println ("before:b=" +b.getname ()), swap (A, b); System.out.println ("after:a=" +a.getname ()); System.out.println ("after:b=" +b.getname ());} A method cannot modify a string, public static void Triplevalue (String x) {x = x+ ("test"); System.out.println ("End of method:x =" +x);} public static void Triplevalue (StringBuffer x) {x = x.append ("test"); System.out.println ("End of method:x =" +x);} A method cannot modify the parameters of a base data type public static void Triplevalue (double x) {x = 3 * x; System.out.println ("End of method:x =" +x);} A method can change the state of an object parameter public static void Triplesalary (Employee x) {x.raisesalary (200); System.out.println ("End of Method:x=" +x.getsalary ());} A method cannot be implemented so that the object argument references a new object public static void swap (Employee x,employee y) {System.out.println ("before of method:x=" + X.getname ()); System.out.println ("Before of Method:y=" +y.getname ()); EmPloyee temp = x;x= Y;y = temp; System.out.println ("End of Method:x=" +x.getname ()); System.out.println ("End of Method:y=" +y.getname ());}} Class Employee {private string name;private double salary;public Employee (String n,double s) {name = N;salary = S;} public void Raisesalary (double i) {double raise = salary*i/100;salary + = Raise;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public double getsalary () {return salary;} public void Setsalary (double salary) {this.salary = salary;}}


Use of method parameters in Java se learning

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.