http://www.google.com/search?q=java+call+by+value
http://www.google.com/search?q=java+call+by+value
http://www.google.com/search?q=java+call+by+value
http://www.google.com/search?q=java+call+by+value
http://www.google.com/search?q=java+call+by+value
THREAD CLOSED
> I want to know,java is call by value and call by
> reference.
> Please give the the exact explanation with some
> example code.
So you don't know what call by value/reference means? Then why are you asking what calling mechanism Java uses? The reply won't mean anything to you. Anyway it's by-value both for assignments and for parameter passing.
this is simple when you call a method by argument like int a,int b or String in main class.
{
rollnum r1=new rollnum(100,200,"RAMESH");\\this is a constructor of rollnum class calling by value ,
// for more detail mail me on vivek_patel49@yahoo.com , or just wait i am busy now.
}
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's value without affecting values in the code that invoked the method. For example:
class PassByValue {
public static void main(String[] args) {
double one = 1.0;
System.out.println("before: one = " + one);
halveIt(one);
System.out.println("after: one = " + one);
}
public static void halveIt(double arg) {
arg /= 2.0;// divide arg by two
System.out.println("halved: arg = " + arg);
}
}
The following output illustrates that the value of arg inside halveIt is divided by two without affecting the value of the variable one in main:before: one = 1.0
halved: arg = 0.5
after: one = 1.0
You should note that when the parameter is an object reference, the object reference -- not the object itself -- is what is passed "by value." Thus, you can change which object a parameter refers to inside the method without affecting the reference that was passed. But if you change any fields of the object or invoke methods that change the object's state, the object is changed for every part of the program that holds a reference to it. Here is an example to show the distinction:
class PassRef {
public static void main(String[] args) {
Body sirius = new Body("Sirius", null);
System.out.println("before: " + sirius);
commonName(sirius);
System.out.println("after: " + sirius);
}
public static void commonName(Body bodyRef) {
bodyRef.name = "Dog Star";
bodyRef = null;
}
}
This program produces the following output: before: 0 (Sirius)
after: 0 (Dog Star)
Notice that the contents of the object have been modified with a name change, while the variable sirius still refers to the Body object even though the method commonName changed the value of its bodyRef parameter variable to null. This requires some explanation.
The following diagram shows the state of the variables just after main invokes commonName:
__
main()| |
sirius->| idNum: 0|
| name --+>"Sirius"
commonName()-->| orbits: null |
bodyRef|__|
At this point, the two variables sirius (in main) and bodyRef (in commonName) both refer to the same underlying object. When commonName changes the field bodyRef.name, the name is changed in the underlying object that the two variables share. When commonName changes the value of bodyRef to null, only the value of the bodyRef variable is changed; the value of sirius remains unchanged because the parameter bodyRef is a pass-by-value copy of sirius. Inside the method commonName, all you are changing is the value in the parameter variable bodyRef, just as all you changed in halveIt was the value in the parameter variable arg. If changing bodyRef affected the value of sirius in main, the "after" line would say "null". However, the variable bodyRef in commonName and the variable sirius in main both refer to the same underlying object, so the change made inside commonName is visible through the reference sirius.
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. If the Java programming language actually had pass-by-reference parameters, there would be a way to declare halveIt so that the preceding code would modify the value of one, or so that commonName could change the variable sirius to null. This is not possible. 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.
-- Arnold, K., Gosling J., Holmes D. (2006). The Java?Programming Language Fourth Edition. Boston: Addison-Wesley.
> rollnum r1=new rollnum(100,200,"RAMESH");\\this is
> a constructor of rollnum class calling by value ,
You're passing literals but that's not the same as call by value. Call by value is the parameter passing mechanism and it's used regardless of whether you're passing a variable or a literal.
> Call by value is the parameter passing
> mechanism and it's used regardless of whether you're
> passing a variable or a literal.
I think the semantics of PBV/PBR with method calling are only apparent when the developer passes a variable. The semantics aren't really applicable when passing a literal value, as a literal value isn't retained for inspection once the called method has returned.
~
> I think the semantics of PBV/PBR with method calling are only
> apparent when the developer passes a variable. The semantics
> aren't really applicable when passing a literal value, as a literal value
> isn't retained for inspection once the called method has returned.
It starts with an 'F' and ends with 'ortran' *ahem*
kind regards,
Jos ;-)
> I think the semantics of PBV/PBR with method calling
> are only apparent when the developer passes a
> variable. The semantics aren't really applicable when
> passing a literal value, as a literal value isn't
> retained for inspection once the called method has
> returned.
I think you're confusing the passing mechanisms themselves with what kind of passing each mechanism can handle. For example if I would make a pass at you I'm not sure you could handle it.
> > I think the semantics of PBV/PBR with method
> calling
> > are only apparent when the developer passes a
> > variable. The semantics aren't really applicable
> when
> > passing a literal value, as a literal value isn't
> > retained for inspection once the called method has
> > returned.
>
> I think you're confusing the passing mechanisms
> themselves with what kind of passing each mechanism
> can handle. For example if I would make a pass at you
> I'm not sure you could handle it.
but such a pass would have no value, and I doubt anyone would refer to it in the future, out of embarrassment or for legal reasons
> Hi! friends,
>
> I want to know,java is call by value and call by
> reference.
> Please give the the exact explanation with some
> example code.
You can look at it like this: Objects are passed by reference (a reference to the original object), simple data (int, chat, double, String) are passed by value.
int a=4;
Object foo=new Object();
void doit(int a, Object obj){
a=2;
foo.x=3;
}
after calling this function, a will still be 4, but the x value of obj will be 3.
but if in the function you do:
foo=new Object();
foo.x=2;
foo.x will not be changed, because after you do foo=new Object(), foo will not be a reference to the original object anymore...
> > Hi! friends,
> >
> > I want to know,java is call by value and call by
> > reference.
> > Please give the the exact explanation with some
> > example code.
> You can look at it like this: Objects are passed by
> reference (a reference to the original object),
> simple data (int, chat, double, String) are passed by
> value.
I guess you could look at it like that, but you'd be mistaken. java is pass-by-value. always. as is repeated here about fifty billion times a week
> > Hi! friends,
> >
> > I want to know,java is call by value and call by
> > reference.
> > Please give the the exact explanation with some
> > example code.
> You can look at it like this: Objects are passed by
> reference
No, they're not. References are passed by value. There's a difference.
> You can look at it like this: Objects are passed by
> reference (a reference to the original object),
> simple data (int, chat, double, String) are passed by
> value.
And that would be incorret.
> foo.x=3;
> ...
> after calling this function, a will still be 4, but
> the x value of obj will be 3.
Which has absolutely nothing to do with how parameters are passed.
> > foo.x=3;
> > ...
> > after calling this function, a will still be 4,
> but
> > the x value of obj will be 3.
>
> Which has absolutely nothing to do with how
> parameters are passed.
Absolutely nothing to do? No, you are wrong, or clarify why you think it has ABSOLUTELY nothing to do with passing parameters.
> > > foo.x=3;
> > > ...
> > > after calling this function, a will still be 4,
> > but
> > > the x value of obj will be 3.
> >
> > Which has absolutely nothing to do with how
> > parameters are passed.
>
> Absolutely nothing to do? No, you are wrong, or
> clarify why you think it has ABSOLUTELY nothing to do
> with passing parameters.
It may have something to do with passing parameters, but it has absolutely nothing to do with how parameters are passed.
> > > foo.x=3;
> > > ...
> > > after calling this function, a will still be 4,
> > but
> > > the x value of obj will be 3.
> >
> > Which has absolutely nothing to do with how
> > parameters are passed.
>
> Absolutely nothing to do? No, you are wrong, or
> clarify why you think it has ABSOLUTELY nothing to do
> with passing parameters.
The fact that a is still 4 is because a was passed by value.
However, the fact that foo.x became 3 is completely unrelated to how foo was passed. Foo was passed by value, but the same behavior would have been observed if it had been passed by reference.
(Note that foo should be obj.)
> > It may have something to do with passing
> parameters,
> > but it has absolutely nothing to do with how
> > parameters are passed.
>
> There is a contradiction in your paragraph.
No, there is a flaw in your reading comprehension.
> > > > foo.x=3;
> > > > ...
> > > > after calling this function, a will still be
> 4,
> > > but
> > > > the x value of obj will be 3.
> > >
> > > Which has absolutely nothing to do with how
> > > parameters are passed.
> >
> > Absolutely nothing to do? No, you are wrong, or
> > clarify why you think it has ABSOLUTELY nothing to
> do
> > with passing parameters.
>
>
> The fact that a is still 4 is because a was passed by
> value.
>
> However, the fact that foo.x became 3 is completely
> unrelated to how foo was passed. Foo was passed by
> value, but the same behavior would have been observed
> if it had been passed by reference.
>
>
> (Note that foo should be obj.)
Agreed.
What? Am I agreeing with jverd? Yes, sometimes miracles happen...
> I think you're confusing the passing mechanisms
> themselves with what kind of passing each mechanism
> can handle.
I don't think I'm confusing anything, but I'd be willing to listen to something that made sense (as opposed to the typical ad hominem absurdum you twitch into so easily). Whether PBV or PBR is used isn't really a factor when passing literals to a method. The semantics aren't realized until a variable is involved. Is there even a legal way to pass a literal to a PBR-type method in languages that support PBR? Such a thing doesn't make sense to me.
[EDIT] On a second read, I think I understand what you were saying. The emphasis was on the fact that Java is always PBV, not what was passed. Let me know if I still misunderstand your point.
> For example if I would make a pass at you I'm not sure you could handle it.
I would handle it with a polite refusal, but that's not relevant to the current conversation.
~
> Whether PBV or PBR is used
> isn't really a factor when passing literals to a
> method. The semantics aren't realized until a
> variable is involved. Is there even a legal way to
> pass a literal to a PBR-type method in languages that
> support PBR? Such a thing doesn't make sense to me.
>
Not sure about that.
As already mentioned Fortran was pass by reference. And that included literals.
And that meant that you could in fact change the literal value of '1' into '2'.
See the following legal C++ code. If it isn't passing a literal by reference then what is it doing?
typedef char const CHAR4[4];
void doit(CHAR4& p)
{
printf(p);
}
...
doit("Hi\n");
> Not sure about that.
Me either, which is why I asked. :o)
> As already mentioned Fortran was pass by reference.
> And that included literals.
My familiarity with Fortran extends to the fact that I know Fortran is a programming language whose name derives from "Formula Translation". I'll have to take your word for it.
> And that meant that you could in fact change the
> literal value of '1' into '2'.
That's a little creepy. Is there a practical use for this?
> See the following legal C++ code. If it isn't
> passing a literal by reference then what is it
> doing?
I'm not well-versed in C++; a string literal is a char[]?
~
>
> > And that meant that you could in fact change the
> > literal value of '1' into '2'.
>
> That's a little creepy. Is there a practical use for
> this?
Someone probably wrote something that used it but I am rather certain that they were just being clever rather than because it was needed.
Lots of fun for inexperience users who didn't pay attention though particularly since Fortran is procedural so long call chains make it easy to lose to orginal source.
>
> > See the following legal C++ code. If it isn't
> > passing a literal by reference then what is it
> > doing?
>
> I'm not well-versed in C++; a string literal is a
> char[]?
>
char const[]
> > char const[]
>
> Okay, thanks. What benefit is there in passing such a
> creature by reference (resource conservation, I
> presume - anything else)?
>
None that I know of. The example was just to demonstrate it.
Actually it might be less efficient. The literal would normally just be a memory location - an explicit value. As a reference it would have to pass a pointer for something. So the compiler would have to create another location to take the address of.
Expanding on that I suppose one could make a weird case that, for example, passing a 'long long' (in C/C+) literal by reference would be more efficient since it would take less stack space.
But it has to use that extra location then. Which would still take up room on the stack, but wouldn't have to be copied every time. Still seems really unlikely that it would be required.
As for why Fortran did that I would suppose it had to do with memory and processors. Memory was hideously expensive and CPU instructions were very limited. So instead of CPU explicit instructions for things like '1' the code actually had to put that value into a memory location. Every value had a memory location. Variables were just a variation on that. But that is just a wild guess on my part.
> > I think you're confusing the passing mechanisms
> > themselves with what kind of passing each
> > mechanism
> > can handle.
>
> [EDIT] On a second read, I think I understand what
> you were saying. The emphasis was on the fact that
> Java is always PBV, not what was passed. Let me know
> if I still misunderstand your point.
In #7 you claimed that the PBR/PBV distinction isn't applicable for certain kinds of data. That's wrong. The parameter passing mechanisms are what they are althought a certain mechanism may not allow for certain kinds of data being passed. You simply cannot assign everything to everything.
> ]> For example if I would make a pass at you I'm
> not sure you could handle it.
>
> I would handle it with a polite refusal, but that's
> not relevant to the current conversation.
Come on, losen up. My (funny I think -:) example shows how a mechanism (making passes) can be in place in spite of the fact that not all attempts to use it (my pass at you) can be successful.
> In #7 you claimed that the PBR/PBV distinction isn't applicable for certain kinds of data. That's wrong. The parameter passing mechanisms are what they are althought a certain mechanism may not allow for certain kinds of data being passed.
I hear what you're saying, and it makes sense to me. I was subsequently trying to hash out whether or not it makes sense to pass literals by reference at all. I still have the impression that for most cases, the practical use of PBR vs PBV boils down to whether or not the caller's value can be modified from within the method, but I recognize there are other applications (like what jschell demonstrated). My understanding of it may be limited to my experience with Java and my relative lack of experience with PBR mechanisms (VB, C#, etc.).
> Come on, losen up. My (funny I think -:) example
> shows how a mechanism (making passes) can be in place
> in spite of the fact that not all attempts to use it
> (my pass at you) can be successful.
I got the point, but the "you couldn't handle me" angle of your riposte has been overplayed for years, now. I just don't share the humor in it; no big deal.
~
If you're asking this question from the compiler's point of view, then the answer is that Java is pass-by-value.
If what you want to know is: 'if I change an attribute of an object passed as a parameter to a method, will that change be reflected in the original object?' then the answer is: yes, it will. In other words, passing an object to a method does not create a copy of the object.
The ability to modify objects passed as arguments to a method is what some people refer to as pass-by-reference, which, strictly speaking, is not a correct usage the term pass-by-reference.
If your background is in C++, then it is important to note that Java objects are not copied when passed as arguments to a method*. Passing an object in Java is like passing a pointer in C++. The value of the pointer, which is a memory address, is what gets passed. This means you get a handle on the original object and you can alter its properties. Some people call this pass-by-address.
Here is a good link on the topic: http://javadude.com/articles/passbyvalue.htm
*The C++ compiler automatically creates a copy constructor for every class (unless you create one yourself) and uses it to create a copy of an object whenever the object is passed as a parameter to a function or as a return value from a function. This object copying always occurs in C++ unless you use pointers or C++ references (a true pass-by-reference mechanism).
> If you're asking this question from the compiler's
> point of view, then the answer is that Java is
> pass-by-value.
From any reasonable point of view, the answer is that Java is pass-by-value. Objects aren't passed; reference values are passed, and they're passed by value.
~
> > If you're asking this question from the
> compiler's
> > point of view, then the answer is that Java is
> > pass-by-value.
>
> From any reasonable point of view, the answer is that
> Java is pass-by-value. Objects aren't passed;
> reference values are passed, and they're passed by value.
Yup, so it all boils down to the JVM just pushing 4 or 8 bytes on the
stack per parameter. (8 for the longs/doubles and 4 for all the others
including pointers to objects). Cute little machine it is; almost made
for a SPARC or an ARM.
kind regards,
Jos
ps. Not many people got certified last night: 73% of the members got laid! ;-)
What would be a language where an object is passed, and not the value of the reference? If an object is passed or not is just a personal opinion. Perhaps passing the value of the reference is correct, but passing an object is perfectly understandable as well.
Oh, thats Gosling defining the definitions again! Nobody is permitted to mention anything or any idea different from what Gosling thinks!
> Oh, thats Gosling defining the definitions again! Nobody is permitted
> to mention anything or any idea different from what Gosling thinks!
Sure you can define anything you want but not when it comes to Java:
the parameter passing mechanism is pass by value and all objects
are dynamic and only pointers to those objects are passed around.
btw Gosling just got that idea from C: the mother of all pass by value
languages. And BCPL was the grandmother.
kind regards,
Jos ( pass by name or pass by in-out anyone? ;-)
> What would be a language where an object is passed,
> and not the value of the reference? If an object is
> passed or not is just a personal opinion.
No.
In C++, for example, you can pass objects by value or by reference, and you can pass primitives by value or by reference, and you can pass pointers (which are roughly equivalent to Java references) by value or by reference.
> Perhaps
> passing the value of the reference is correct, but
> passing an object is perfectly understandable as
> well.
It's also wrong. It's like saying the sun revolves around the earth, rising in the east and setting in the west. You can look at it that way in a very limited context, but when you try to fit it into the bigger picture, the model falls down.
There's no reason to teach people to think of it as passing an object. Passing a reference by value is very simple and straightforward and it's correct.
> Oh, thats Gosling defining the definitions again!
> Nobody is permitted to mention anything or any idea
> different from what Gosling thinks!
That is pathetic. Every time somebody points out how wrong you are you piss and whine about them agreeing with Gosling.
Fact: Gosling is not perfect.
Fact: Gosling knows a lot about computre science--far more than you ever will
Fact: Java passes references by value and does not pass objects by reference, by standard CS definitions.
Fact: Your constant pathetic attempts at insulting people just because they agree with Gosling serves no purpose other than to make you look stupid.
> > What would be a language where an object is
> passed,
> > and not the value of the reference? If an object
> is
> > passed or not is just a personal opinion.
>
> No.
>
> In C++, for example, you can pass objects by value or
> by reference, and you can pass primitives by value or
> by reference, and you can pass pointers (which are
> roughly equivalent to Java references) by value or by
> reference.
Yes, but why? Because in C++ someone defined that you pass objects, and not the value of the reference. In java its the exact opposite: Gosling defined that you pass the value of the reference, and not the object. But my point is that they are all the same thing! So that is why I said its just a personal opinion. Or, if you know, then explain why in C++ the object is passed, and not the references value, and in java you pass the references value, and not the object (if sucha explanation really exists, which I doubt).
>
> > Perhaps
> > passing the value of the reference is correct, but
> > passing an object is perfectly understandable as
> > well.
>
> It's also wrong. It's like saying the sun revolves
> around the earth, rising in the east and setting in
> the west. You can look at it that way in a very
> limited context, but when you try to fit it into the
> bigger picture, the model falls down.
So, why in C++ saying that object is passed is correct?
> > Oh, thats Gosling defining the definitions again!
> > Nobody is permitted to mention anything or any
> idea
> > different from what Gosling thinks!
>
> That is pathetic. Every time somebody points out how
> wrong you are you piss and whine about them agreeing
> with Gosling.
>
> Fact: Gosling is not perfect.
>
> Fact: Gosling knows a lot about computre science--far
> more than you ever will
>
> Fact: Java passes references by value and does not
> pass objects by reference, by standard CS
> definitions.
>
> Fact: Your constant pathetic attempts at insulting
> people just because they agree with Gosling serves no
> purpose other than to make you look stupid.
You are free to think anything you want about my discussing style. And no, I do not think that I am insulting anybody here but Gosling. Actually, I dont even insulting Gosling really. I am just opining something about Gosling only. Not this time, sorry. You are trying to make my reply insulting, but thats not true. Please forget my past, and focus only in my current replies.
The first Fortran was non-re-entrant and non-recursive. There was no stack, no heap, and no difference between code and data segments and working memory.
This meant that the compiler could recursively expand statements to machine ops, and operate in close to a streaming manner with a minimum working memory, rather than more modern (post 1950s) approaches which require symbolic linking. Memory locations of literals, arrays, strings and so on were placed in the code where they were declared, and referenced by a simple symbol table mapping identifier to location. Subroutine calls were expanded in-line, much like macro processing in C. Hence the lack of re-entrant code. The language specification defined the layout of the data in memory, as you could alias variables across types, for example to bit-twiddle a floating point number you'd alias the variable as an integer.
It's legendary that when Algol introduced recursion, and the requirement for the runtime to support a stack, copying values from parameters to local storage, many implementers complained about the overhead at both compile and run times (much like some people complain about garbage collection). I'm not sure whether Algol has a heap as well - I've had to maintain some Fortran code, (and some assembler that called it, including calls into a point part way into a routine to reuse code without replicating the routine body, which was F77 - non-local gotos) but I've never touched Algol.
Pete
> > In C++, for example, you can pass objects by value or by reference,
> > and you can pass primitives by value or by reference, and you can
> > pass pointers (which are roughly equivalent to Java references) by
> > value or by reference.
>
> Yes, but why? Because in C++ someone defined that you pass
> objects, and not the value of the reference. In java its the exact
> opposite: Gosling defined that you pass the value of the reference,
> and not the object. But my point is that they are all the same thing!
No they're not: they're completely different languages; different languages,
different idiom and different definitions. Your question "why?" can
only be answered by "because the inventor of that language thought
it would be useful". Java's right isn't C++'s wrong nor vice versa.
If you want to use any language at all, the saying "when in Rome do as
the Romans do" applies and that includes idiom, source code layout,
naming conventions and best practice for that particular language.
kind regards
Jos
> > In C++, for example, you can pass objects by value
> or
> > by reference, and you can pass primitives by value
> or
> > by reference, and you can pass pointers (which are
> > roughly equivalent to Java references) by value or
> by
> > reference.
>
> Yes, but why? Because in C++ someone defined that you
> pass objects, and not the value of the reference.
It's because what's actually happening is different, and both C++'s and Java's definitions are standard in CS.
> In
> java its the exact opposite: Gosling defined that
> you pass the value of the reference, and not the
> object.
Nope.
Gosling decided which existing, well-defined mechanism to use. The way he chose matches the standard definition of PBV, so that's what it's called.
> But my point is that they are all the same
> thing!
You're wrong. They're not.
You can pass an object by reference in C++.
You can pass a pointer by value in C++.
The first one does not behave like Java's passing a reference by value. The second one does. The definitions of PBV and PBR used here are not Java-specific or C++ specific.
> So that is why I said its just a personal
> opinion.
And you were wrong.
> Or, if you know, then explain why in C++ the
> object is passed, and not the references value, and
> in java you pass the references value, and not the
> object (if sucha explanation really exists, which I
> doubt).
Like I said: PBV and PBR are standard CS definitions. What happens in Java matches the standard definition of PBV. When you pass an object by reference in C++, it matches the standard definition of PBR, and you get different behavior.
> So, why in C++ saying that object is passed is
> correct?
Because that's what happens in some cases.
There is a fundamental difference between passing an object by reference and passing a reference or pointer by value. You an do both in C++, and the latte one matches Java's passing of a reference by value and the standard CS definition.
> You are free to think anything you want about my
> discussing style. And no, I do not think that I am
> insulting anybody here but Gosling. Actually, I dont
> even insulting Gosling really. I am just opining
> something about Gosling only.
No. All you're doing is resorting to petty insults when you can't support your case.
> Not this time, sorry.
> You are trying to make my reply insulting, but thats
> not true. Please forget my past, and focus only in my
> current replies.
Your current replies are the same as your past: "If you don't agree with me you're just kissing Gosling's ***."
> Gosling decided which existing, well-defined
> mechanism to use. The way he chose matches the
> standard definition of PBV, so that's what it's
> called.
Yes, no doubt about that! You didnt get my point yet.
>
>
> > But my point is that they are all the same
> > thing!
>
> You're wrong. They're not.
>
> You can pass an object by reference in C++.
>
> You can pass a pointer by value in C++.
>
> The first one does not behave like Java's passing a
> reference by value. The second one does. The
> definitions of PBV and PBR used here are not
> Java-specific or C++ specific.
Forget the "pass by reference" mechanism that C++ has, and Java doesnt, for a while. Just concentrate in the "second one" as you say, the "pass by value". I know the difference between these two mechanisms, dont need to explain. Lets concentrate only in the "pass by value".
As you said, in C++ you pass objects by value, right? But in Java you cannot say such a thing, because in java you pass the reference of the object by value. So, I ask you again: why in C++ saying that you pass the object by value is correct, and in java its not correct?
> > You are free to think anything you want about my
> > discussing style. And no, I do not think that I am
> > insulting anybody here but Gosling. Actually, I
> dont
> > even insulting Gosling really. I am just opining
> > something about Gosling only.
>
> No. All you're doing is resorting to petty insults
> when you can't support your case.
>
>
>
> > Not this time, sorry.
> > You are trying to make my reply insulting, but
> thats
> > not true. Please forget my past, and focus only in
> my
> > current replies.
>
> Your current replies are the same as your past: "If
> you don't agree with me you're just kissing Gosling's
> ***."
No. You know the proof? If other person said that opinion about Gosling, you wouldnt think that. See? The problem is me, and not my replies, in your mind! So, please again, forget my past and just concentrate in my current replies.
> Forget the "pass by reference" mechanism that C++
> has, and Java doesnt, for a while. Just concentrate
> in the "second one" as you say, the "pass by value".
> I know the difference between these two mechanisms,
I rather doubt that.
> As you said, in C++ you pass objects by value, right?
You can. Not all methods do.
> But in Java you cannot say such a thing, because in
> java you pass the reference of the object by value.
Correct.
> So, I ask you again: why in C++ saying that you pass
> the object by value is correct, and in java its not
> correct?
In Java, no variable's or expression's value is ever an object. It is always only a primtive or a reference.
In C++, we have the above (although it's a C++ pointer, not a C++ reference, that matches a Java reference most closely) AND we can also have a variable's value be an object.
Passing an object by value in C++ is different from passing a pointer to that object by value.
Passing a reference by value in Java is like passing a pointer by value in C++. The notion of passing an object by value in Java is meaningless, because nothing's value is ever an object. To pass an object by value, something would have to have that object as its value, and that value--the object--would have to be copied. That happens in some cases in C++, but never in Java.
In Java, the value of the reference is copied and passed. By standard CS definitions this means that:
* The reference is passed by value.
* The object is not passed by value.
* The object is not passed by reference.
> > Your current replies are the same as your past:
> "If
> > you don't agree with me you're just kissing
> Gosling's
> > ***."
>
> No. You know the proof? If other person said that
> opinion about Gosling, you wouldnt think that. See?
Wrong.
If anybody did something that stupid, I would tell them how stupid they are.
> The problem is me, and not my replies, in your mind!
> So, please again, forget my past and just concentrate
> in my current replies.
If you start behaving better, and continue to behave better for a long time, then maybe I will. But right now, it's irrelevant, because you're being the same jerk you've always been.
See JosAHs reply 41. That explanation is simple and correct. Dont need to invent stories about sun, earth, east, west (ROFL!!!!), because all those things are just personal opinions of Gosling and the C++s inventor. The definitions are different because they are different. There isnt a "Computer Science" explanation that explains the difference.
> See JosAHs reply 41. That explanation is simple and
> correct.
Yes, I know. It's also not in conflict with what I'm saying.
> Dont need to invent stories about sun,
> earth, east, west (ROFL!!!!),
It was an analogy to show you how wrong you are. If you still can't understand, that's not my problem.
> because all those
> things are just personal opinions of Gosling and the
> C++s inventor.
Wrong.
> The definitions are different because
> they are different. There isnt a "Computer Science"
> explanation that explains the difference.
PBV and PBR are standard CS terms. Neithre Gosling nor Stroustrup defined them. They applied them, in a standard and consistent way, to what occurs in the languages they designed.
The PBV that happens in C++ is identical to the PBV that happens in Java, and both fit the standard CS definition.
Some people think that Java's passing a reference by value is like passing the object by reference. There are some surface similarities, but they're not the same. However, you seem to be saying that the *object* is passed by value, and that's even more ridiculous, and I explained why above.
> I hear what you're saying, and it makes sense to me.
> I was subsequently trying to hash out whether or not
> it makes sense to pass literals by reference at all.
This is the kind of questions programming language designers struggle with all the time. They eventually turn up as "rules" of the language, like "you can assign this to that but not to that, etcetera".
So there's no definite answer to what "makes sense" . It depends on what language you want.
Sometimes when people go against the tide and just allow what was previously untinkable a new "language paradigm" is born. So what's generally allowed in programming languages is a moving target really.
> I got the point, but the "you couldn't handle me"
> angle of your riposte has been overplayed for years,
> now. I just don't share the humor in it; no big
> deal.
Well, can you handle me? -:)
> Some people think that Java's passing a reference by value is like
> passing the object by reference. There are some surface similarities,
> but they're not the same. However, you seem to be saying that the
> *object* is passed by value, and that's even more ridiculous, and I
> explained why above.
Gosling and friends did obfuscate matters a bit when they started
calling pointers 'references'. When people see through that it isn't
a problem anymore: just PBV, that's all.
OTOH Stroustrup obfuscated a lot of things by using non-defined
copy ctors, non-defined assignment operators and all sorts of stuff
that happens behind your back just to get those object values passed
from here to there and back again ;-)
kind regards,
Jos
> Gosling and friends did obfuscate matters a bit when
> they started calling pointers 'references'.
Well they had to because Java references aren't pointers in the C++ sense. The C++/CLI language for example calls them "handles" instead.
Reference and handle both mean pointer to a managed heap. A C++ pointer on the other hand is an address, meaning it has a fixed location.
> > See JosAHs reply 41. That explanation is simple
> and
> > correct.
>
> Yes, I know. It's also not in conflict with what I'm
> saying.
>
>
> > Dont need to invent stories about sun,
> > earth, east, west (ROFL!!!!),
>
> It was an analogy to show you how wrong you are. If
> you still can't understand, that's not my problem.
To be sincere, I didnt even read that story closely, because thats just pathetic, sorry. The main reason that your story is pathetic is that your story doesnt apply in C++. You story is not a good explanation then.
>
>
> > because all those
> > things are just personal opinions of Gosling and
> the
> > C++s inventor.
>
> Wrong.
You mean that there is a difference between pass an object and pass the reference (in java) of the object, and such a difference is explained in Computer Science? Come on, you are kidding, right?
I know that Computer Science explains the difference between PBV and PBR. But there isnt an explanation that explains the difference between passing an object by value and passing the reference (in java) or the pointer (in C++) of the object by value in Computer Science, because those things are the result of personal opinions of Gosling and the C++s inventor.
>
> > The definitions are different because
> > they are different. There isnt a "Computer
> Science"
> > explanation that explains the difference.
>
> PBV and PBR are standard CS terms. Neithre Gosling
> nor Stroustrup defined them. They applied them, in a
> standard and consistent way, to what occurs in the
> languages they designed.
>
> The PBV that happens in C++ is identical to the PBV
> that happens in Java, and both fit the standard CS
> definition.
I know, please lets concentrate only in the "pass by value" for a while, that is something that java and C++ have in common.
> Some people think that Java's passing a reference by
> value is like passing the object by reference. There
> are some surface similarities, but they're not the
> same. However, you seem to be saying that the
> *object* is passed by value, and that's even more
> ridiculous, and I explained why above.
So, all the C++ programmers are ridiculous, because they pass objects by value all the time! Actually, ridiculous is your explanation!!!
> > Gosling and friends did obfuscate matters a bit
> when
> > they started calling pointers 'references'.
>
> Well they had to because Java references aren't
> pointers in the C++ sense.
They're also not references in the C++ sense.
> The C++/CLI language for
> example call them "handles" instead.
I've often thought that "handle" might be a better term. Almost anything but reference. I think pointer would've even been better. There'd still be some confusion, but I think it'd be less than the current PBV/PBR confusion.
> You mean that there is a difference between pass an
> object and pass the reference (in java) of the
> object, and such a difference is explained in
> Computer Science? Come on, you are kidding, right?
Because Java has call-by-value only, the parameter passing mechanism isn't a problem.
So The Java Programming Language suggests that passing an object could be used in everyday speech for passing an object reference which is formally correct.
> > I got the point, but the "you couldn't handle me"
> > angle of your riposte has been overplayed for years,
> > now. I just don't share the humor in it; no big deal.
>
> Well, can you handle me? -:)
It depends on what you mean by "handle", but I don't really care to find out regardless of the definition.
> So The Java Programming Language suggests that passing an object could be used in everyday speech for passing an object reference which is formally correct.
The VM spec uses the "passing an object" phrasing; I don't recall at the moment whether that's in the JLS or not. Informal discussions here on PBV/PBR tend to result in confusion, at least until a more formal understanding of the term is established. At least, that's been my experience.
~
> I've often thought that "handle" might be a better term. Almost anything
> but reference. I think pointer would've even been better. There'd still
> be some confusion, but I think it'd be less than the current PBV/PBR
> confusion.
I still like the phrase "memory address" but I've been a low level type
all of my life, so C/C++ pointers, Java references and what have you
handles are all memory addresses to me (and at that location even
more memory addresses may occur ;-)
kind regards,
Jos
> > > See JosAHs reply 41. That explanation is simple
> > and
> > > correct.
> >
> > Yes, I know. It's also not in conflict with what
> I'm
> > saying.
> >
> >
> > > Dont need to invent stories about sun,
> > > earth, east, west (ROFL!!!!),
> >
> > It was an analogy to show you how wrong you are.
> If
> > you still can't understand, that's not my problem.
>
> To be sincere, I didnt even read that story closely,
> because thats just pathetic, sorry. The main reason
> that your story is pathetic is that your story
> doesnt apply in C++. You story is not a good
> explanation then.
It's not intended to apply in C++. It's intended to show how your "you can view it this way" BS causes more confusion that it eliminates. It's actually a very good analogy, but you're too stupid to understand the point and too stubborn to examine anything that might disagree with you.
>
> >
> >
> > > because all those
> > > things are just personal opinions of Gosling and
> > the
> > > C++s inventor.
> >
> > Wrong.
>
> You mean that there is a difference between pass an
> object and pass the reference (in java) of the
> object, and such a difference is explained in
> Computer Science? Come on, you are kidding, right?
You're using very sloppy terminology. You also seem to be falling into the trap of not recognizing the distinction between what is passed and how something is passed. I don't really think you understand the basics well enough for me to even be able to explain it to you.
There is a standard CS definition of PBV. There is a standard CS definition of PBR. These defintions are not concerned with what you're passing, only how the passing occurs.
In C++, you can pass an object by value.
In C++, you can pass an object by reference.
In C++, you can pass a pointer (which is like a Java reference) to an object by value.
All three of those lead to very different behaviors. Passing an object by value is very different from passing a pointer by value which is different from passing an object by reference.
In java, you can pass a reference by value, which gives the same behavior as passing a pointer by value in C++.
If you could pass an object by value in Java, you'd get a different behavior, but you can't do that, so it's irrelevant.
> I know that Computer Science explains the difference
> between PBV and PBR. But there isnt an explanation
> that explains the difference between passing an
> object by value and passing the reference (in java)
> or the pointer (in C++) of the object by value in
> Computer Science, because those things are the result
> of personal opinions of Gosling and the C++s
> inventor.
You are making absolutely no sense.
* PBV and PBR are well-defined. You agree with that. Fine.
* Java is 100% purely PBV. As far as I can tell, you agree with that as well, which is good, because if you don't, you're wrong.
* Are you now saying that passing a reference by value in Java is the same as passing an object by value in Java? Thats utter nonsense. A reference and an object are two completely different things. Yes, Gosling defined what a reference is and what an object is. If that's your only point, then you're even more ridiculous than I thought. If you want to say that Java passes object's by value, you can say it, but it will be incorrect. It would be just as valid to say that Java passes left-handed cellists by gunny-sack.
You really don't seem to have any point at all.
> > Some people think that Java's passing a reference
> by
> > value is like passing the object by reference.
> There
> > are some surface similarities, but they're not the
> > same. However, you seem to be saying that the
> > *object* is passed by value, and that's even more
> > ridiculous, and I explained why above.
>
> So, all the C++ programmers are ridiculous, because
> they pass objects by value all the time! Actually,
> ridiculous is your explanation!!!
You are phenomenally stupid. Do you even bother to read what I write?
I have stated, several times, that one can pass an object by value in C++ and one cannot in Java. Do you really not understand the difference between passing an object by value and passing a pointer by value?
> > > Gosling and friends did obfuscate matters a bit when
> > > they started calling pointers 'references'.
> >
> > Well they had to because Java references aren't
> > pointers in the C++ sense.
>
> They're also not references in the C++ sense.
True. Java references aren't at all comparable to anything C++ has.
> > The C++/CLI language for
> > example call them "handles" instead.
>
> I've often thought that "handle" might be a better
> term. Almost anything but reference. I think pointer
> would've even been better. There'd still be some
> confusion, but I think it'd be less than the current
> PBV/PBR confusion.
Well maybe you should consider C++/CLI then -:)
Anyway you know my position. To introduce PBR in order to explain the Java parameter passing mechanism is far too sophisticated. A normal value assignment explains it better. But I won't argue this anymore.
mr. jverd, I do not want to argue with you, trying to make you seem stupid. Although you are doing that with me, I wont do the same thing with you. I do not know how to be more clear. I even think that you got my point, but you dont want to admit that.
In short, just answer yes or no, please:
For Gosling the references value and not the object is passed by value because he PERSONALLY thinks it is better, and the C++ inventor thinks that the object is passed by value because he PERSONALLY thinks its better. Correct? Yes or no.
> I still like the phrase "memory address" but I've
> been a low level type
> all of my life, so C/C++ pointers, Java references
> and what have you
> handles are all memory addresses to me (and at that
> location even
> more memory addresses may occur ;-)
Well, you're complacent and wrong. Although a reference is always pointing at something it's not always pointing at the same fixed location in memory.
> For Gosling the references value and not the object
> is passed by value because he PERSONALLY thinks it is
> better, and the C++ inventor thinks that the object
> is passed by value because he PERSONALLY thinks its
> better. Correct? Yes or no.
[url http://en.wikipedia.org/wiki/Mu_%28negative%29]Mu[/url].
Your question makes no sense, and shows a fundamental lack of understanding.
I have stated this several times, and you've ignored it, but I'll say it one last time:
A Java reference is like a C++ pointer, not a C++ reference.
In Java the reference is passed by value. This is true because the reference is what is copied.
In C++, you can pass a pointer by value. This is just like Java's passing a reference by value. Then pointer is copied.
In C++, you can also pass an object by value. The variable's value is an object and that object is copied.
There is nothing like this in Java. A variable's value is never an object, and no object is ever copied when being passed to a method. Therefore, to say that Java passes objects by value is incorrect. It's not just a difference of opinion or a different way of looking at it. It is incorrect. Java passes references by value, not objects. That is fact, not opinion.
> What would be a language where an object is passed,
C++
> and not the value of the reference?
Well, you can also pass the value of the pointer, as I explained earlier.
> If an object is
> passed or not is just a personal opinion.
Incorrect. It is a combination of standard CS definitions and the language definition and observed behavior. Java passes references, not objects. This is fact, not opinion.
> Perhaps
> passing the value of the reference is correct, but
> passing an object is perfectly understandable as
> well.
When two people who undersand Java's parameter passing are talking, they can refer to "passing an object" rather than "passing a reference to an object" as a shorthand. However, it is a bad idea to talk about "passing an object" to a newbie who's already confused and looking for clarification on the details of this specific issue. Doing that will just confuse things more.
> Your question makes no sense, and shows a fundamental
> lack of understanding.
Like your own.
> I have stated this several times, and you've ignored
> it, but I'll say it one last time:
>
> A Java reference is like a C++ pointer, not a C++
> reference.
No it's not. A Java reference is a pointer into a managed heap, something C++ is missing.
> > Your question makes no sense, and shows a
> fundamental
> > lack of understanding.
>
> Like your own.
No, I understand it quite well, thank you very much.
>
> >
> > I have stated this several times, and you've
> ignored
> > it, but I'll say it one last time:
> >
> > A Java reference is like a C++ pointer, not a C++
> > reference.
>
> No it's not. A Java reference is a pointer into a
> managed heap, something C++ is missing.
At the language level, a Java reference and a C++ pointer are very similar: They point to an object and you can dereference them to get to the object's members. Obviously you can do things with a C++ pointer that you can't with a Java reference, but there's no other construct in the C++ language that's closer to a Java reference. The main point is that a Java reference is much more like a C++ pointer than a C++ reference.
> > What would be a language where an object is
> passed,
>
> C++
>
> > and not the value of the reference?
>
> Well, you can also pass the value of the pointer, as
> I explained earlier.
>
>
> > If an object is
> > passed or not is just a personal opinion.
>
> Incorrect. It is a combination of standard CS
> definitions and the language definition and observed
> behavior. Java passes references, not objects. This
> is fact, not opinion.
>
>
>
> > Perhaps
> > passing the value of the reference is correct, but
> > passing an object is perfectly understandable as
> > well.
>
> When two people who undersand Java's parameter
> passing are talking, they can refer to "passing an
> object" rather than "passing a reference to an
> object" as a shorthand. However, it is a bad idea to
> talk about "passing an object" to a newbie who's
> already confused and looking for clarification on the
> details of this specific issue. Doing that will just
> confuse things more.
Ha! Ok, mr. jverd, you won. I simply forgot that an object is copied when it is passed by value in C++. But you were wrong as much as me, because now your "story" makes sense either in C++ or in Java, but you agreed with me when I said that your story doesnt apply in C++. You either didnt understand your own example, or didnt understand C++ very well. Now you understand, exactly like me, after some googling.
> > > Your question makes no sense, and shows a fundamental
> > > lack of understanding.
> >
> > Like your own.
>
> No, I understand it quite well, thank you very much.
It may be so but I suggest you take a more humble approach to what other people may understand. Or are you all-understanding?
> > No it's not. A Java reference is a pointer into a
> > managed heap, something C++ is missing.
>
> At the language level, a Java reference and a C++
> pointer are very similar: They point to an object and
> you can dereference them to get to the object's
> members. Obviously you can do things with a C++
> pointer that you can't with a Java reference, but
> there's no other construct in the C++ language that's
> closer to a Java reference. The main point is that a
> Java reference is much more like a C++ pointer than a
> C++ reference.
A C++ pointer is nothing like a Java reference.
Threre's no need for you and other amateurs to guess what C++ would look like in a managed environment like Java. This has already been devised in the C++/CLI language designed by H. Sutter with the (somewhat reluctant) assistance of B. Stroustrup.
> It may be so but I suggest you take a more humble
> approach to what other people may understand.
I will be sure to give your suggestion all the consideration it warrants.
> > At the language level, a Java reference and a C++
> > pointer are very similar: They point to an object
> and
> > you can dereference them to get to the object's
> > members. Obviously you can do things with a C++
> > pointer that you can't with a Java reference, but
> > there's no other construct in the C++ language
> that's
> > closer to a Java reference. The main point is that
> a
> > Java reference is much more like a C++ pointer than
> a
> > C++ reference.
>
> A C++ pointer is nothing like a Java reference.
Incorrect.
They are alike in the ways I pointed out above.
> Threre's no need for you and other amateurs to guess
> what C++ would look like in a managed environment
> like Java.
I'm not sure where you got the idea that I'm guesssing at that, or that I have even the faintest interest in it
> > It may be so but I suggest you take a more humble
> > approach to what other people may understand.
>
> I will be sure to give your suggestion all the
> consideration it warrants.
In that case I suggest you pay better attention to my suggestions.
> > A C++ pointer is nothing like a Java reference.
>
> Incorrect.
>
> They are alike in the ways I pointed out above.
No they're not. The C++/CLI language shows you're wrong.
> > > A C++ pointer is nothing like a Java reference.
> >
> > Incorrect.
> >
> > They are alike in the ways I pointed out above.
>
> No they're not. The C++/CLI language shows you're
> wrong.
So, you're claiming that the following are false:
* C++ pointers and Java references point to objects.
* You can access the objects' members by dereferencing the pointer/refernce.
Fascinating.
> > No they're not. The C++/CLI language shows you're
> > wrong.
>
> So, you're claiming that the following are false:
>
> * C++ pointers and Java references point to objects.
>
> * You can access the objects' members by
> dereferencing the pointer/refernce.
>
> Fascinating.
Yes isn't it.
Not that I understand what you're talking about but for a fact, a C++ pointer isn't anything like a Java pointer (or reference). Period.
> > > No they're not. The C++/CLI language shows
> you're
> > > wrong.
> >
> > So, you're claiming that the following are false:
> >
> > * C++ pointers and Java references point to
> objects.
> >
> > * You can access the objects' members by
> > dereferencing the pointer/refernce.
> >
> > Fascinating.
>
> Yes isn't it.
>
> Not that I understand what you're talking about but
> for a fact, a C++ pointer isn't anything like a Java
> pointer (or reference). Period.
There are several people whose opinion I respect far more than yours who disagree.
> a C++ pointer isn't anything like a Java
> pointer (or reference). Period.
As far as comparing both languages goes, a C++ pointer and a Java reference are as close to each other as it gets.
They are both direct implementations of the concept of indirection: a variable that can be used to access an object indirectly through a dereferencing operation; and to that end holds (some sort of) address of said object. A C++ pointer and a Java reference have exactly the same semantics in this fundamental regard.
The differences in C++'s pointers allowing arithmetic is irrelevant to their role in parameter passing; the operation of relevance is assignment initialization, and they are exactly the same (value copy). The difference between a managed or unmanaged heap is likewise irrelevant to parameter passing; and AFAIK there is nothing in the C++ language definition that mandates that pointer implementations map directly to physical addresses. Nor that a C++ heap must be unmanaged; there are GC'd implementations of C++ - and I mean C++, not "C++/CLI".
C++ smart pointers (such as in std and especially boost) are even closer to Java reference variables (no arithmetic and automatic destruction albeit deterministic), and they wrap a regular pointer. It would be impossible to implement them by wrapping a reference.
Another - or the same - point is this: you can directly "translate" any Java program to a C++ program by replacing Java reference variables with C++ (smart) pointer variables, with all relevant semantics intact. You cannot do so by translating Java references to C++ references; things just work differently. Conversely, you cannot "translate" all uses of C++ reference to Java references at all.
@ yamm: And it does not take much comparative sample code to figure this out.
> > a C++ pointer isn't anything like a Java pointer (or reference). Period.
>
> As far as comparing both languages goes, a C++ pointer and a Java
> reference are as close to each other as it gets.
I can confirm that for at least the Java 1.1.8 JVM (or 1.1.4 I forgot). The
company I worked for had a source licence for that JVM for no particular
reason (nobody did ever do anything with it). I scrutinized the sources
and indeed, just pointers all over the place pointing straight to those
Java objects.
kind regards,
Jos
>
> If your background is in C++, then it is important to
> note that Java objects are not copied when passed as
> arguments to a method*. Passing an object in Java is
> like passing a pointer in C++. The value of the
> pointer, which is a memory address, is what gets
> passed. This means you get a handle on the original
> object and you can alter its properties. Some people
> call this pass-by-address.
>
If your background is C++ or anything else for that matter it is important to note that there is no direct access to an object in java. So of course it is pointless to discuss how a object is passed.
Java developers however do use references. References are not objects, they are references. And references are passed by value.
> What would be a language where an object is passed,
> and not the value of the reference? If an object is
> passed or not is just a personal opinion.
The Java Language Specification specifically defines what a reference type is.
Now you, personally, might chose to ignore that. But many other people are going to take the position that the JLS has some say in how java works. And in particular they are going to presume that it is much more relevant than your personal opinion.
And then there is of course the years and countless written words in Computer Science which defines what pass by value versus pass by reference mean. And the vast consensus on those definitions. Again I believe that many people will accept those definitions rather than your own.
> Perhaps
> passing the value of the reference is correct, but
> passing an object is perfectly understandable as
> well.
>
Could be understandable except of course that jave doesn't do it.
> Oh, thats Gosling defining the definitions again!
> Nobody is permitted to mention anything or any idea
> different from what Gosling thinks!
You are free to provide your own definitions of anything you like. Just as the vast majority of people are free to discount your definitions because they do not match the accepted definitions nor standards.
Many people recognize that the benifit of precisely specified definitions and standards are less likely to lead to mis-understandings.
>
> Yes, but why? Because in C++ someone defined that you
> pass objects, and not the value of the reference. In
> java its the exact opposite: Gosling defined that
> you pass the value of the reference, and not the
> object. But my point is that they are all the same
> thing!
Wrong.
In C++ one can pass an object by value or by reference. Those most certainly are not the same thing. The language specification and syntax makes them specifically different.
That has nothing to do with java however, because you can't access an object in java. All you can do is access the reference type.
And java only allows pass by value.
> So that is why I said its just a personal
> opinion. Or, if you know, then explain why in C++ the
> object is passed, and not the references value, and
> in java you pass the references value, and not the
> object (if sucha explanation really exists, which I
> doubt).
What?
In C++ the language definition allows for two different passing models.
In Java it doesn't.
In C++ an object is directly accessible. In Java it isn't.
>
> >
> > > Perhaps
> > > passing the value of the reference is correct,
> but
> > > passing an object is perfectly understandable as
> > > well.
> >
> > It's also wrong. It's like saying the sun revolves
> > around the earth, rising in the east and setting in
> > the west. You can look at it that way in a very
> > limited context, but when you try to fit it into the
> > bigger picture, the model falls down.
>
> So, why in C++ saying that object is passed is
> correct?
>
That statement is incorrect.
In C++ you can pass and object either by value or by reference. Period.
That is how C++ works.
Contrast that with C and Java where only pass by value is allowed.
And in java you do not have access to the object. You only have access to a reference type.
> The first Fortran was non-re-entrant and
> non-recursive. There was no stack, no heap, and no
> difference between code and data segments and working
> memory.
>
> This meant that the compiler could recursively expand
> statements to machine ops, and operate in close to a
> streaming manner with a minimum working memory,
> rather than more modern (post 1950s) approaches which
> require symbolic linking. Memory locations of
> literals, arrays, strings and so on were placed in
> the code where they were declared, and referenced by
> a simple symbol table mapping identifier to location.
> Subroutine calls were expanded in-line, much like
> macro processing in C. Hence the lack of re-entrant
> code. The language specification defined the layout
> of the data in memory, as you could alias variables
> across types, for example to bit-twiddle a floating
> point number you'd alias the variable as an integer.
>
> It's legendary that when Algol introduced recursion,
> and the requirement for the runtime to support a
> stack, copying values from parameters to local
> storage, many implementers complained about the
> overhead at both compile and run times (much like
> some people complain about garbage collection). I'm
> not sure whether Algol has a heap as well - I've had
> to maintain some Fortran code, (and some assembler
> that called it, including calls into a point part way
> into a routine to reuse code without replicating the
> routine body, which was F77 - non-local gotos) but
> I've never touched Algol.
>
>
Cool history lesson.
> As you said, in C++ you pass objects by value, right?
> But in Java you cannot say such a thing, because in
> java you pass the reference of the object by value.
> So, I ask you again: why in C++ saying that you pass
> the object by value is correct, and in java its not
> correct?
>
Because C++ allows you to access the object directly. Java doesn't.
The following would represent this in C++.
passByValue(MyClass t);
passByReference(MyClass& t);
MyClass c; // This is the object, the entire object.
passByValue(c); // Compiler does pass by value
passByReference(c); // Compiler does pass by reference
> See JosAHs reply 41. That explanation is simple and
> correct. Dont need to invent stories about sun,
> earth, east, west (ROFL!!!!), because all those
> things are just personal opinions of Gosling and the
> C++s inventor. The definitions are different because
> they are different. There isnt a "Computer Science"
> explanation that explains the difference.
You misunderstood that reply.
Neither language invented passing mechanisms. Both languages one or more passing mechanisms.
> I know that Computer Science explains the difference
> between PBV and PBR. But there isnt an explanation
> that explains the difference between passing an
> object by value and passing the reference (in java)
> or the pointer (in C++) of the object by value in
> Computer Science, because those things are the result
> of personal opinions of Gosling and the C++s
> inventor.
>
I suspect the real problem is that you think reference and object are the same thing.
They are not.
> In short, just answer yes or no, please:
>
> For Gosling the references value and not the object
> is passed by value because he PERSONALLY thinks it is
> better, and the C++ inventor thinks that the object
> is passed by value because he PERSONALLY thinks its
> better. Correct? Yes or no.
The short answer is that you do not understand the difference between object and reference.
You might want to rethink this discussion once you do understand the difference.
> Well, you're complacent and wrong. Although a
> reference is always pointing at something it's not
> always pointing at the same fixed location in memory.
The reference value is always the same until re-assignment occurs. At least that is how the Sun VM works (and certainly any other VM would have a hard time not doing that.)
That value does in fact point to a fixed location conceptually. (Excluding some flag bits.) The fixed location represents a structure and the contents of that structure can change. It is that data that references the actual object location.
So yes it is always pointing to a fixed location.
> > I have stated this several times, and you've
> ignored
> > it, but I'll say it one last time:
> >
> > A Java reference is like a C++ pointer, not a C++
> > reference.
>
> No it's not. A Java reference is a pointer into a
> managed heap, something C++ is missing.
Either it is a pointer or it isn't.
The fact that the heap objects can move is irrelevant to whether it is a pointer or not.
Not to mention of course that one can implement a managed heap in C++ (I have done so as a matter of fact.) And using it does not require anything other than normal syntax (via the joys of operator overloading.)
>
> Not that I understand what you're talking about but
> for a fact, a C++ pointer isn't anything like a Java
> pointer (or reference). Period.
Nothing at all?
You should file a bug then against the JLS because certainly the following from section 4.3.1 must be wrong then....
"The reference values (often just references) are pointers to these objects,..."
Perhaps you could suggest in the bug exactly what a reference is since it isn't a pointer.