我是新手,个位高手我想请问一下java中关于参数的一些问题?比如怎样传参?它的传递有几种方法?参数到底怎样用?为什么要用参数?........请个位高手帮帮忙  及及及????

解决方案 »

  1.   

    Parameters looks like a pre-condition to the method. For example:public int addInt (int i){   i ++;   return i;
    }
    If you want to increase i, you need an int parameter. Right? So you can write i in the parameter list.So how to pass the parameter? --> Just write all the type of variables in the parameter list. That's OK!
      

  2.   

    关于参数传递的方法,可以看看这:
    http://jewyi.itpub.net/post/28094/257456
      

  3.   

    英语不好,看不大懂.
    不过看到了"In Java, everything is pass-by-value"
      

  4.   

      我觉得楼主应该不是计算机专业出生而学JAVA的吧,我认为你要解决,理解好关于参数的问题.还是自己要找本书静下心了看看,其实比较简单,但是一两句话是肯定讲不明白的,即使能讲,那你也看不懂!所以建议刚开始还是要先熟悉下基础知识,不要一上来就想问几个问题搞明白.
      

  5.   

    在java中只有一种传值方式(按照值传递)
      

  6.   

    其实引用也就是传地址,地址也就是一个value。自然可以说java都是以value传递.
      

  7.   

    In Java, everything is pass-by-value
    Q: Can you provide some authoritative quotation to support the saying "In Java, everything is pass-by-value"?
    A: 
    From The Java Programming Language, by James Gosling et al. 3rd edition (pg. 56): 
    quote: 
    --------------------------------------------------------------------------------Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory. The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.--------------------------------------------------------------------------------From The Java Tutorial quote: 
    --------------------------------------------------------------------------------Pass by Value In Java methods, arguments are passed by value.
    When invoked, the method receives the value of the variable passed in. When the
    argument is of primitive type, pass-by-value means that the method cannot change
    its value. When the argument is of reference type, pass-by-value means that the
    method cannot change the object reference, but can invoke the object's methods
    and modify the accessible variables within the object. This is often the
    source of confusion--a rogrammer writes a method that attempts to modify the
    value of one its arguments and the method doesn't work as expected. Let's look
    at such method and then investigate how to change it so that it does what the
    programmer originally intended. --------------------------------------------------------------------------------Quiz question from Sun:
    In Java programming, all method parameters are passed by value.
    Answer: true In Java, everything is pass-by-value, unless you think Dr. James Gosling forgot how he invented Java, AND Sun does not know how Java works.If we all agree those are not the cases. Then the question should change to that how to understand the fact "In Java, everything is pass-by-value".If you go up-and-down a little, you will find a lot of help here. Two examples to prove: Example one:(from:http://bobcat.webappcabaret.net/javachina/faq/07.htm#pas_Q2)// PassByValueTest.java
    public class PassByValueTest { 
      public static void main(String [] args) { 
        String arr[]=new String[2]; 
              
        arr[0]="hello"; 
        arr[1]="hi"; 
              
        // nothing will change here
        swap(arr[0], arr[1]); 
        System.out.println(arr[0] + ", " + arr[1]); //hello, hi
              
        // two Strings are actually swapped
        swap(arr, 0, 1); 
        System.out.println(arr[0] + ", " + arr[1]); //hi, hello
      }
            
      // useless swap method, since you swap the copies 
      // of two strings' reference inside the method 
      // Strings outside are not affected.
      public static void swap(String s1,String s2){ 
        String tmp = null;
               
        tmp  = s1; 
        s1   = s2; 
        s2   = tmp; 
      } 
            
      // real practical swap here
      // swap array elements with two indices
      // very useful for sorting algorithm, etc.
      // reference of arr will never change (pass-by-value)
      // but the contents of arr will change permanently
      // since the copy of arr reference still refer to the same object.
      public static void swap(String arr[], int ix1, int ix2) { 
        String tmp = null; 
              
        tmp      = arr[ix1]; 
        arr[ix1] = arr[ix2]; 
        arr[ix2] = tmp; 
      } 

    Example two(my code): public class ParamTest { /**
    * @param args
    * @author Jianming
    * @version 1.0
    */ public static void main(String[] args){
    /*
    * Test one:Methods can't modify numeric parameters
    */System.out.println("Testing tripleValue:");
    double percent = 10;
    System.out.println("Before: percent = " + percent);
    tripleValue(percent);
    System.out.println("After: percent = " + percent);/*
    * Test two: Methods can modify the state of object parameters
    */System.out.println("nTesting tripleSalary:");
    Employee harry = new Employee("Harry", 5000);
    System.out.println("Before: salary = " + harry.getSalary());
    tripleSalary(harry);
    System.out.println("After:salary = " + harry.getSalary());/*
    * Test three:Methods can't attach new objects to object parameters
    */System.out.println("nTesting swap:");
    Employee a = new Employee("Jianming", 10000);
    Employee b = new Employee("James", 20000);
    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()); }public static void tripleValue(double x){
    x = 3 * x;
    System.out.println("End of method:x = " + x);
    }public static void tripleSalary(Employee x){
    x.raiseSalary(200);
    System.out.println("End of method:salary = " 
    + x.getSalary());
    }public static void swap(Employee x, Employee y){
    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{
    public Employee(String n, double s){
    name = n;
    salary = s;
    }public String getName(){
    return name;
    }public double getSalary(){
    return salary;
    }public void raiseSalary(double bypercent){
    double raise = salary * bypercent / 100;
    salary += raise;
    }private double salary;
    private String name; } The result: Testing tripleValue:
    Before: percent = 10.0
    End of method:x = 30.0
    After: percent = 10.0 Testing tripleSalary:
    Before: salary = 5000.0
    End of method:salary = 15000.0
    After:salary = 15000.0 Testing swap:
    Before: a = Jianming
    Before: b = James
    End of method: x = James
    End of method: y = Jianming
    After: a = Jianming
    After: b = JamesNote: (1):Methods can't modify the parameters of primitive type (2):Methods can modify the state of object parameters,but methods can't attach new objects to object parameters an example to explain the question: 电视机和遥控器可以很形象的描叙和解释这个问题。 
    可以把遥控器看作是电视机的一个引用拷贝,只要电视机存在,也就是用遥控器对准一台电视机,按遥控器上面的各种按扭(function)可以对电视机产生各种影响,但是你换一个遥控器对电视机来说是不会产生影响的,电视机并不会因为遥控器换了而变成别的电视机。同时遥控器也可以不对准原来的电视机,转去对准别的电视机,这对原来的电视机也是不会产生影响的。一台电视机可以有多个遥控器,并且只要某个遥控器对准了这台电视机,这个遥控器就可以通过它上面的按扭改变电视机的状态。 a picture to show how it works:
    End.
      

  8.   

    当你在做一件事情,或者处理一个问题的时候,你会发现你缺少了点儿什么。那么你就可以通过“传递参数”的方式,添加你的重要工具。
    比如要踢某人,kickPerson();
    但是如果就这么写,那就不知道踢谁了。
    所以,你就需要传递一个参数进去。
    kickPerson(String yourself);哎,问题太高深,还是期待老师来讲解。
      

  9.   

    在java中只有一种传值方式(按照值传递) 
      

  10.   

    java 只有一种传递参数的方式,那就是 传值
      

  11.   

    做个简单的比喻.
    void buy(String fruit)
    {
        System.out.println(fruit);
    }
    String father = "pear";
    String mother = "apple";
    buy(father);
    buy(mother);
    老爸(father)叫去买梨,老妈(mother)叫去买苹果.
    去到水果店,跟老板说:老板(void buy(String fruit))
    我要梨(buy(father)),老板走过来,给你梨(System.out.println(fruit),注意,你得到的是梨)
    我要苹果(buy(mother)),老板走过来,给你苹果(System.out.println(fruit),注意,你得到的是苹果)
    你付了钱,买了梨又买了苹果.
    不知道你明白没有....