"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
SCJP 1.4
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Originally posted by StuartF:
Strings are immutable... they do not behave like other objects. If you want a mutable string (which will give you the behaviour you expected), have a look at the StringBuffer class.
Arguments are copies, so the "s" in each method is a local variable (each distinct from the other)
Strings are immutable... they do not behave like other objects. If you want a mutable string (which will give you the behaviour you expected), have a look at the StringBuffer class.
IBM 286, SCJP, SCWCD, EIEIO
Originally posted by Rick Portugal:
... Only if "s" is a primitive...
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
IBM 286, SCJP, SCWCD, EIEIO
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Originally posted by Rick Portugal:
Let me ask you all a personal question. Be honest. Do you ever feel that Java is unnecessarily complex?
I am an application developer at a bank. I don't want to have to worry about when Java will pass by value or pass by reference. I just want my programs to work.
Does anyone here feel like using Java for application development is overkill?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Joel McNary:
Now, what is the expected result? The problem is that there are three possibilities, but only two terms to describe them!
Pass-by-value-of-object provides the "Value is 10" result.
Pass-by-value-of-reference provides the "Value is 20" result.
Pass-by-reference provides the "Value is 30" result.
Note that Java does not pass objects; it passes references. It might help to think that while an object is a composite value, the reference is a primitive.
IBM 286, SCJP, SCWCD, EIEIO
Originally posted by Rick Portugal:
a copy of the aTest object was made and it was sent to the method by value)
IBM 286, SCJP, SCWCD, EIEIO
I don't want to have to worry about when Java will pass by value or pass by reference. I just want my programs to work.
Does anyone here feel like using Java for application development is overkill?
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
SCJP, SCWCD
IBM 286, SCJP, SCWCD, EIEIO
Originally posted by Rick Portugal:
You can't create error-free systems quickly when you need to worry about a lot of subtleties and side-effects. Ideally Java would always pass parameters the same way. Maybe that means it should have no primitives. Also, I don't understand why String is immutable.
Originally posted by Rick Portugal:
Ideally Java would always pass parameters the same way.
Also, I don't understand why String is immutable.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
SCJP 1.4
That's a contradiction.Originally posted by Rick O'Shay:
>> Do you ever feel that Java is unnecessarily complex?
Never. Are there areas that could be simplified? Yes.
IBM 286, SCJP, SCWCD, EIEIO
Originally posted by Rick Portugal:
That's a contradiction.
Originally posted by Rick Portugal:
I don't understand what you mean. The aTest object was passed to the method by reference (or, for you purists, a copy of the aTest object was made and it was sent to the method by value). It had a member variable called "a" with the value of 10.
42
Originally posted by Jeroen Wenting:
What I find very disturbing is that someone who claims to have passed SCJP and SCWCD doesn't understand that Java uses pass by value exclusively...
42
I think it's important to bear in mind that passing *a* reference is not the same thing as "pass-by-reference". Pass-by-reference (not available in Java) simply means that the method gets an alias to the parameter, as opposed to a copy of it. Changes made to the variable within the method affect the value of the original argument as well. Here's an example of pass-by-reference from C#:How can any language be "pass by reference" if it doesn't pass references by value?
Originally posted by Jeroen Wenting:
it's far from a semantic difference.
If objects were passed by reference one could change the reference to another object in a method and have that change reflected outside the method.
If (as it is) object references are passed by value this is impossible and the change in reference is local to the method.
Reference Data Types
The non-primitive data types in Java are objects and arrays. These non-primitive types are often called "reference types" because they are handled "by reference" -- in other words, the address of the object or array is stored in a variable, passed to methods, and so on. By comparison, primitive types are handled "by value" -- the actual primitive values are stored in variables and passed to methods.
In C, you can manipulate a value by reference by taking its address with the & operator, and you can "dereference" and address with the * and -> operators. These operators do not exist in Java: the primitive types are always passed by value; arrays and objects are always passed by reference.
Originally posted by Jeroen Wenting:
What I find very disturbing is that someone who claims to have passed SCJP and SCWCD doesn't understand that Java uses pass by value exclusively...
And people complain that employers don't value our certification, well if people with such misconceptions can get them I have to agree with those employers.
Originally posted by Alan Google:
Thanks for clearing that up.
Originally posted by Rick O'Shay:
you should consider a different career path
Originally posted by Jeroen Wenting:
What I find very disturbing is that someone who claims to have passed SCJP and SCWCD doesn't understand that Java uses pass by value exclusively...
Originally posted by Rick O'Shay:
That there are specific areas where complexity could be reduced doesn't mean Java is needlessly complex.
IBM 286, SCJP, SCWCD, EIEIO
I don't think so, as the object is not getting passed at all. It's the object's reference that is getting passed, and the object's reference is passed "by value".In general, if you want to change X, you need to pass X by reference, right? But couldn't we say that we're passing the object itself by reference?
Here's what Mr. Flanagan says in the Third Edition...However Mr. Flanagan's words echo my understanding.
I've said that java handles arrays and objects "by reference." Don't confuse this with the phrase "pass by reference." "Pass by reference" is a term used to describe the method-calling conventions of some programming languages. In a pass-by-reference language, values -- even primitive values -- are not passed directly to methods. Instead, methods are always passed references to values. Thus, if the method modifies its parameters, those modifications are visible when the method returns, even for primitive types.
Java does not do this; it is a "pass by value" language. However, when a reference type is involved, the value that is passed is a reference. But this is not the same as pass-by-reference. If Java were a pass-by-reference language, when a reference type was passed to a method, it would be passed as a reference to the reference.
Flanagan, D. (1999). Java In A Nutshell. p. 74. Sebastopol, CA: O'Reilly & Associates, Inc.
For this, you can turn to none other than the creator of Java...Perspective: I know how computers work and how Java works, I'm just trying to get my terminology current and standard.
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|