Results 1 to 6 of 6

Thread: คำถามเกี่ยวกับ passing parameter ครับ

  1. #1


    คือ ผมอยากรู้ว่า การ passing parameter ใน java มีกี่แบบครับ

    แล้วถ้ามี

    - การ passing แบบ by reference แล้ว ต้องส่งยังไงครับ

    - การส่งทั้ง object(instance) ถือว่าเป็นการส่งแบบ reference รึป่าวครับ

    - นอกจากการส่ง by reference โดยส่งเป็น object แล้ว ยังมีวิธีอื่นอีกรึเปล่าครับ

    รบกวนผู้ที่รู้ด้วยนะครับ ขอตัวอย่างด้วยนะครับ
    ขอบคุณมากครับ


  2. #2


    คือ ผมอยากรู้ว่า การ passing parameter ใน java มีกี่แบบครับ
    [/b]
    Java is always pass-by-value.


    - การ passing แบบ by reference แล้ว ต้องส่งยังไงครับ
    [/b]
    ข้อนี้คงไม่ต้องตอบแล้ว


    - การส่งทั้ง object(instance) ถือว่าเป็นการส่งแบบ reference รึป่าวครับ
    [/b]
    You pass that reference by value. That is pass by value, not pass by reference.


    - นอกจากการส่ง by reference โดยส่งเป็น object แล้ว ยังมีวิธีอื่นอีกรึเปล่าครับ
    [/b]
    No.


    *** ถ้ายัง ไม่ get เชิญ ห้อง Chat

  3. #3
    Senior Member
    Join Date
    Sep 2007
    Location
    Laos
    Posts
    192


    <div class='quotetop'>QUOTE
    คือ ผมอยากรู้ว่า การ passing parameter ใน java มีกี่แบบครับ[/b]
    Java is always pass-by-value.[/b][/quote]

    I think there are 2 ways.

    1. By value: arguments that are the primitive data type will be sent to be the parameter in Pass By Value

    2. By reference: arguments that are the instances of any class will be sent to be the parameter in Pass By Reference



  4. #4


    2. By reference: arguments that are the instances of any class will be sent to be the parameter in Pass By Reference
    [/b]
    ผมเข้าใจว่ามันเป็นแบบนี้มากกว่านะ
    You pass that reference (instance of class) by value. That is pass by value, not pass by reference.

    ไม่ใช่ pass by reference แต่ทำงานเหมือนกับ pass by reference

    เอาละพูดอย่างเดียวมันก็ไม่น่าเชื่อถือ เอางี้ refer by Sun Java Tutorial

    มีคนเข้าใจเรื่องนี้ผิดกันเยอะ อยากให้เข้าใจกันให้ถูกต้อง

  5. #5
    Senior Member
    Join Date
    Sep 2007
    Location
    Laos
    Posts
    192


    ei ei.. So sorry.. what i think is wrong...

    Credit for yawmark

    All parameters to methods are passed "by value." In other words, values of parameter variables in a method are copies of the values the invoker specified as arguments. If you pass a double to a method, its parameter is a copy of whatever value was being passed as an argument, and the method can change its parameter&#39;s value without affecting values in the code that invoked the method. For example:

    [code] class PassByValue {

  6. #6


    Refer by Sun Java Tutorial

    Passing Reference Data Type Arguments

    Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object&#39;s fields can be changed in the method, if they have the proper access level.

    For example, consider a method in an arbitrary class that moves Circle objects:

    public void moveCircle(Circle circle, int deltaX, int deltaY) {
    // code to move origin of circle to x+deltaX, y+deltaY
    circle.setX(circle.getX() + deltaX);
    circle.setY(circle.getY() + deltaY);

    //code to assign a new reference to circle
    circle = new Circle(0, 0);
    }
    [/b]
    Let the method be invoked with these arguments:

    moveCircle(myCircle, 23, 56)
    [/b]
    Inside the method, circle initially refers to myCircle. The method changes the x and y coordinates of the object that circle references (i.e., myCircle) by 23 and 56, respectively. These changes will persist when the method returns. Then circle is assigned a reference to a new Circle object with x = y = 0. This reassignment has no permanence, however, because the reference was passed in by value and cannot change. Within the method, the object pointed to by circle has changed, but, when the method returns, myCircle still references the same Circle object as before the method was called.

    อะแถม

    http://javadude.com/articles/passbyvalue.htm

Similar Threads

  1. Replies: 0
    Last Post: 06-10-2009, 02:02 PM
  2. คำถามเกี่ยวกับ VB6
    By marzumi in forum Visual Basic
    Replies: 2
    Last Post: 05-09-2009, 09:51 AM
  3. Web attack by parameter pollution
    By Gen0TypE in forum Hacking, Exploit Articles/Tutorial/Techniques
    Replies: 1
    Last Post: 21-05-2009, 07:22 PM
  4. Replies: 2
    Last Post: 16-05-2009, 01:59 PM
  5. Replies: 1
    Last Post: 26-02-2004, 07:15 PM

Members who have read this thread : 0

Actions : (View-Readers)

There are no names to display.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •