The Java programming language always uses call by value

This is an excerpt of the book “Core Java, Vol. I – Fundamentals”, that shows (and clarifies) how the Java programming language always uses call by value when passing parameters to a method.

“The Java programming language always uses call by value. That means that the method gets a copy of all parameter values. In particular, the method cannot modify the contents of any parameter variables that are passed to it.
For example, consider the following call:

double percent = 10;
harry.raiseSalary(percent);

No matter how the method is implemented, we know that after the method call, the value of percent is still 10.

Let us look a little more closely at this situation. Suppose a method tried to triple the value of a method parameter:

public static void tripleValue(double x) // doesn’t work
{
    x = 3 * x;
}

Let’s call this method:

double percent = 10;
tripleValue(percent);

However, this does not work. After the method call, the value of percent is still 10. Here is what happens:
1. x is initialized with a copy of the value of percent (that is, 10).
2. x is tripled—it is now 30. But percent is still 10.
3. The method ends, and the parameter variable x is no longer in use.

There are, however, two kinds of method parameters:

  • Primitive types (numbers, boolean values)
  • Object references

You have seen that it is impossible for a method to change a primitive type parameter. The situation is different for object parameters. You can easily implement a method that triples the salary of an employee:

public static void tripleSalary(Employee x) // works
{
    x.raiseSalary(200);
}

When you call

harry = new Employee(. . .);
tripleSalary(harry);

then the following happens:
1. x is initialized with a copy of the value of harry, that is, an object reference.
2. The raiseSalary method is applied to that object reference. The Employee object to which both x and harry refer gets its salary raised by 200 percent.
3. The method ends, and the parameter variable x is no longer in use. Of course, the object variable harry continues to refer to the object whose salary was tripled.

As you have seen, it is easily possible —and in fact very common— to implement methods that change the state of an object parameter. The reason is simple. The method gets a
copy of the object reference, and both the original and the copy refer to the same object.

Many programming languages (in particular, C++ and Pascal) have two methods for parameter passing: call by value and call by reference. Some programmers (and unfortunately even some book authors) claim that the Java programming language uses call by reference for objects. However, that is false. Because this is such a common misunderstanding, it is worth examining a counterexample in detail.

Let’s try to write a method that swaps two employee objects:

public static void swap(Employee x, Employee y) // doesn’t work
{
    Employee temp = x;
    x = y;
    y = temp;
}

If the Java programming language used call by reference for objects, this method would work:

Employee a = new Employee("Alice", . . .);
Employee b = new Employee("Bob", . . .);
swap(a, b);
// does a now refer to Bob, b to Alice?

However, the method does not actually change the object references that are stored in the variables a and b. The x and y parameters of the swap method are initialized with copies
of these references. The method then proceeds to swap these copies.

// x refers to Alice, y to Bob
Employee temp = x;
x = y;
y = temp;
// now x refers to Bob, y to Alice

But ultimately, this is a wasted effort. When the method ends, the parameter variables x and y are abandoned. The original variables a and b still refer to the same objects as they
did before the method call.

This discussion demonstrates that the Java programming language does not use call by reference for objects. Instead, object references are passed by value. Here is a summary of what you can and cannot do with method parameters in the Java programming language:

  • A method cannot modify a parameter of primitive type (that is, numbers or boolean values).
  • A method can change the state of an object parameter.
  • A method cannot make an object parameter refer to a new object.

The program in Listing 4–4 demonstrates these facts. The program first tries to triple the value of a number parameter and does not succeed:

Testing tripleValue:
Before: percent=10.0
End of method: x=30.0
After: percent=10.0

It then successfully triples the salary of an employee:

Testing tripleSalary:
Before: salary=50000.0
End of method: salary=150000.0
After: salary=150000.0

After the method, the state of the object to which harry refers has changed. This is possible because the method modified the state through a copy of the object reference.

Finally, the program demonstrates the failure of the swap method:

Testing swap:
Before: a=Alice
Before: b=Bob
End of method: x=Bob
End of method: y=Alice
After: a=Alice
After: b=Bob

As you can see, the parameter variables x and y are swapped, but the variables a and b are not affected.”

This was one of my favorite parts in this book, because of the way it demonstrates these concepts in such a clear way.

Asterisk 1.8.6.0 Now Available

The Asterisk Development Team announces the release of Asterisk 1.8.6.0.
This
release is available for immediate download at
http://downloads.asterisk.org/pub/telephony/asterisk/

The release of Asterisk 1.8.6.0 resolves several issues reported by the
community and would have not been possible without your participation.
Thank you!

The following is a sample of the issues resolved in this release:

* Fix an issue with Music on Hold classes losing files in playlist when
realtime
is used.
(Closes issue ASTERISK-17875. Reported by David Cunningham. Patched
by Igor
Goncharovsky)

* Resolve a potential crash in chan_sip when utilizing auth= and
performing a
‘sip reload’ from the console.
(Closes issue ASTERISK-17939. Reported by wdoekes. Patched by Richard
Mudgett)

* Address some improper sql statements in res_odbc that would cause an
update
to fail on realtime peers due to trying to set as “(NULL)” rather than an
actual NULL.
(Closes issue ASTERISK-17791. Reported by marcelloceschia. Patched by
Tilghman
Lesher)

* Resolve issue where 403 Forbidden would always be sent maximum number
of times
regardless to receipt of ACK.
(Patched by Richard Mudgett)

* Resolve issue where if a call to MeetMe includes both the dynamic(D) and
always request PIN(P) options, MeetMe will ask for the PIN two times:
once for
creating the conference and once for entering the conference.
(Patched by Kinsey Moore)

* Fix New Zealand indications profile based on
http://www.telepermit.co.nz/TNA102.pdf
(Closes issue ASTERISK-16263. Reported, Patched by richardf)

* Segfault in shell_helper in func_shell.c
(Closes issue ASTERISK-18109. Reported by Michael Myles, patched by
Richard
Mudgett)

For a full list of changes in this release, please see the ChangeLog:

http://downloads.asterisk.org/pub/telephony/asterisk/ChangeLog-1.8.6.0

Thank you for your continued support of Asterisk!

Book Review: Core Java, Vol. I – Fundamentals

After being a PHP programmer for some time now, I’ve faced the need of learning a serious programming language instead of a scripting language (Note: not that there’s something bad about PHP, it is just great).

I wanted a no-nonsense book, aimed to experienced programmers. Not that I’m a PHP ninja, but I’m definitely not a newbie. I searched the Internet for some book suggestions and decided to try: Core Java, Vol. I – Fundamentals

In short, this books delivers what it promises. It’s a masterpiece of information written in a way that you are not able to read a single page without learning something from it, and yet, you do not get asphyxiated with too much information. It has a well worked balance between the information and the way it is presented. The authors don’t assume that you know anything about Java, but they expect you to be a “serious programmer”.

Do not try to use this as your first programming book. It will not teach you how to program, it will teach you Java, serious Java.

By the publication of this article I’m reading the 5th chapter (what a masterpiece!), and I’ll be updating this post according to my progress with the book.

MariaDB 5.2.8 released

The MariaDB project would like to announce the immediate availability
of MariaDB 5.2.8, the latest iteration of the existing 5.2 series
release.

The release notes are available from:

http://kb.askmonty.org/en/mariadb-528-release-notes

A complete changelog is available from:

http://kb.askmonty.org/en/mariadb-528-changelog

Sources, binaries and packages can be downloaded from:

http://downloads.askmonty.org/mariadb/5.2.8/

Debian and Ubuntu packages are available from repositories. A
sources.list generator can be found at:

http://downloads.askmonty.org/mariadb/repositories/

MariaDB code hosting is on Launchpad:

https://launchpad.net/maria

The project always strives for quality, but in reality nothing is
perfect. Please take time to report any issues you encounter.

https://bugs.launchpad.net/maria

Feel free to join the maria-discuss and/or maria-developers groups on
Launchpad, or use #maria on Freenode IRC. And please, enjoy 5.2.8!

Asterisk 1.6.2.20 Now Available

The Asterisk Development Team announces the release of Asterisk 1.6.2.20. This
release is available for immediate download at
http://downloads.asterisk.org/pub/telephony/asterisk/

The release of Asterisk 1.6.2.20 resolves a regression that was introduced just
prior to the release of Asterisk 1.6.2.19.

* Fix reload crash caused by destroying default parking lot.
(Closes issue ASTERISK-18103. Reported by 808blogger. Patched by jrose.

For a full list of changes in this release, please see the ChangeLog:

http://downloads.asterisk.org/pub/telephony/asterisk/releases/ChangeLog-1.6.2.20

Thank you for your continued support of Asterisk!

Sun CEO explicitly endorsed Java’s use in Android

Hello people,

I would like to share this article as I found it of really high relevance to everyone that would like to use Java as a programming language:

“If you believe Oracle’s patent lawsuits against Google for its use of Java in Android, Google has stolen not just patented ideas but directly copied Java code. In short, Google is a red-handed thief and should pay Oracle over a billion in damages. There’s just one little problem with this portrayal of Google as an intellectual property (IP) bandit. When Android first came out, Sun CEO Jonathan Schwartz, then Java’s owner, greeted the news with “heartfelt congratulations.” Whoops.

CNET News: Oracle scrubs site of embarrassing Java blog

While Schwartz’s blog post has since been erased by Oracle, Groklaw found the page and has republished it In his note, Schwartz not only congratulates Google “on the announcement of their new Java/Linux phone platform, Android.,” he goes on to announce that “Sun is the first platform software company to commit to a complete developer environment around the platform, as we throw Sun’s NetBeans developers platform for mobile devices behind the effort. We’ve obviously done a ton of work to support developers on all Java based platforms, and we’re pleased to add Google’s Android to the list.”

So, there you go, Java’s owner at the time not only welcomed Android’s use of Java, they were actively supporting it with development tools. So, what do you have to say now Oracle?”

Continue reading at ZDNET