开发者

Help me understand this pointer vs. value issue

开发者 https://www.devze.com 2022-12-12 04:53 出处:网络
I know this is a dumb question, but its really bugging me. Take the following: public <TParseable> LinkedList<TParseable> readAllParseable(

I know this is a dumb question, but its really bugging me.

Take the following:

public <TParseable> LinkedList<TParseable> readAllParseable(
            Parseable<TParseable> parseable, boolean close) throws IOException
{
   LinkedList<TParseable> list = new LinkedList<TParseable>();
    byte[] delim = parseable.getDelimiterValue();
    boolean skipNL = parseable.skipNewLines();

    while(ready())
    {
     byte[] data = readTo(delim);
     parseable.parse(data);

     System.out.println(parseable);

     list.add((TParseable)parseable);
    }
    return list;
}

The println statement outputs the expected to开发者_开发技巧String() value of parseable each time after the call to parseable.parse(data). However, the returned list has the correct number of elements, but they are all equal to the last value of parseable before the loop completed.

Is this because the list.add(xxx) parameter is passed by pointer rather than value?


You only ever have a single instance of parseable in the code you posted. When you call add(parseable) you are adding a reference ("pointer" isn't really correct in Java) to parseable in your list.

By calling it repeatedly, without changing what object parseable refers to, you are simply adding more references to the same object to your list.

New objects are only ever created by the new keyword.


You're putting in the same parseable object into the list each time. Each time parseable parses some data, it is being processed using the same parseable object.


the object parseable is the same through the entire method. I assume that when you call parseable.parse(data) it changes the internal data in parseable. Since you keep putting the same object in the list, you are just operating on the same object every iteration.


I think you conceptual problem is one of terminology. If I change "value" to "state", perhaps it will help clarify things ...

The println statement outputs the expected toString() state of parseable each time after the call to parseable.parse(data). However, the returned list has the correct number of elements, but they are all equal to the last state of parseable before the loop completed.

In reality, your program is using only one Parseable instance, and the method calls on that instance are changing its state.

Is this because the list.add(xxx) parameter is passed by pointer rather than value?

No. It is because the instance's state (as shown by toString()) is changing.

In fact, Java uses pass-by-value semantic for all parameters in method and constructor calls, irrespective of the type. The slightly confusing thing is that the "value" that is passed when you pass an Object / array in Java is a reference.

The three basic parameter passing mechanisms supported by programming languages are:

  • pass-by-value where you copy the value which might be a primitive value, a pointer / reference value, or (in some languages) a structured value. (In some languages, a value can be copied back on return, but that's just a logical extension of pass-by-value.)

  • pass-by-reference where you pass the address of a variable in the caller, allowing the callee to change that variable, and/or see the results of something else changing the variable.

  • pass-by-name which was a "clever" mechanism used in Algol-60 that turned out to be expensive to implement and too difficult for most programmers to use effectively.

I would stay away from using the terminology "pass by pointer". It is really just another way of saying "pass-by-value of a pointer" ... and if you try to think of it as something different, you only end up confused.

0

精彩评论

暂无评论...
验证码 换一张
取 消