-1

This may be a case of semantics but as a practical matter I've been finding that Java absolutely acts sometimes in a pass-by-reference manner as opposed to pass-by-value. For example:

public String example() {
    SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

    String sResult = "Example:";
    GregorianCalendar CAL = new GregorianCalendar(2016, 0, 1);  //January 1, 2016
    sResult += "\nBegin: CAL = " + f.format(CAL.getTime());
    passTo(CAL);
    sResult += "\nEnd: CAL = " + f.format(CAL.getTime());
    return sResult;
}

public void passTo(GregorianCalendar CAL) {
    CAL.add(CAL.DAY_OF_YEAR, 56);
}

Executing example() will result in:

Example: 
Begin: CAL = 2016-01-01 
End: CAL = 2016-02-26

Clearly, the passTo method has taken the CAL object as an argument, manipulated it, and affected the original object contained within example(). How can anyone argue that only a value was passed if the original object has been altered?

This would fit the definition of pass-by-reference as commonly understood in programming.

Palladium
  • 65
  • 7
  • Java **is** ***always*** pass by value. However, the value of a reference type (i.e. an `Object`) **is** a *reference*. To put it another way, `CAL` is not a `GregorianCalendar`, `CAL` is a reference to a `GregorianCalendar`. – Elliott Frisch Jul 12 '16 at 21:55
  • _This would fit the definition of pass-by-reference as commonly understood in programming._ No. _Pass by reference_ is a concept that applies to variables, not objects. The concept and the name has nothing to do with Java's reference values. – Sotirios Delimanolis Jul 12 '16 at 21:58
  • This is complete logical failure on your part. An argument was passed to a method, the ,method's actions altered the original object. This is the very definition of passing by reference. This is a case of very very poor semantics which I think you will find many programmers will take issue with. – Palladium Jul 12 '16 at 21:59
  • _Java passes references by value. Java passes references by value. Java passes references by value._ Repeat it in your mirror every day until it sticks. – Louis Wasserman Jul 12 '16 at 22:00
  • Here's a quick way to see that even in your example, Java is pass-by value: add `CAL = new GregorianCalendar(2016, 0, 1);` as the first line of your `passTo` function. You'll see that "Begin:" and "End:" now print the same things. Were Java pass-by-reference, this would have modified the value in the calling function as well. It's important to understand that "Pass-by-value" does NOT mean "References do not exist" or "There is no way to pass a reference" - it just means that the actual thing passed is always a value (even if it's the value of a reference) – Edward Peters Jul 12 '16 at 22:01
  • @Edwin Peters, "it just means that the actual thing passed is always a value (even if it's the value of a reference) " - thank you for writing this and you are correct. My conclusion is also correct, this a case of semantics. As an absolutely practical matter the effect is pass by reference and it's important for coders new to Java understand that any object passed to a method may be altered in an unpredictable manner. – Palladium Jul 12 '16 at 22:04
  • The concept of pass-by-reference describes how variables used as arguments are evaluated. The word _reference_ is unrelated to Java's reference values. – Sotirios Delimanolis Jul 12 '16 at 22:05

1 Answers1

-1

Have you checked other answers?

In any case. You're passing a reference to the object by value. Similar to C's passing a pointer. The reference is copied, but since it references a memory location, you can still modify stuff on that memory location.

Compare that to primitive datatypes (int, boolean, etc.), which are not objects.

Koos Gadellaa
  • 1,220
  • 7
  • 17