Re: pass by reference



"angelochen960@xxxxxxxxx" <angelochen960@xxxxxxxxx> wrote in news:0a4ad57b-
60d2-4b7a-93c5-e53b20628c81@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:

Hi guys,

Thanks for all the answers, I'm kind of surprised that a simple
question got so many answers! is this statement correct:

Everything in Java are passed by value, in the case of an object, a
reference to the object is passed, which is a value too, updating the
fields in the reference does reflect the changes. anyway, maybe I
should not try to understand java in a C++ manner.

but i can't help, String is a object, so a reference should be the one
passed, and why we can not update the field in the String object? and
then where is the field of the String?



Yes, I was wrong in my

public void myFunc(String s) {
s = "123";
}

because that compiles exactly as

public void myFunc(String s) {
s = new String("123");
}


If the String class had a method setValue() we could write

public void myFunc(String s) {
s.setValue("123");
}

but it does not, and we can't.

Some kind of a wrapper class is needed with the methods required.

.



Relevant Pages

  • Re: passing value from jcombobox to other classes?
    ... public void setX(String x) { ... // the reference to an object of A ... Run it by "java C" and see what happens. ...
    (comp.lang.java.programmer)
  • LONG: Java student getting String Exception/String Index errors
    ... Hello, all, I'm back with more questions from my Java I class. ... and length' to edit a single long string of text. ... public void setTextFind{ ... errors indicate I'm using a number that's too large for substring ...
    (comp.lang.java.help)
  • Re: programming concepts > specific languages
    ... >> public static void manipulateObject(Object objectToManipulate) { ... >> the reference to get the address of an particular field instance (named x ... was showing how Java would behave if it were pass by reference. ... But I can't manipulate the string! ...
    (comp.programming)
  • Re: Tired of 100s of stupid Getter/Setter methods
    ... >> It is a common academic exercise, ... > String, I get a String back out. ... The reference is not typecast to ... It's like programming in Java with only Object references, ...
    (comp.lang.java.programmer)
  • Xml parser and character encoding
    ... I am new to java and I run a short program processing xml files. ... private String CData; ... public void startElement(String namespaceURI, String localName, String ...
    (comp.lang.java.programmer)