Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-03-2010, 08:40 AM   #1
Senior Member
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
What sense does this make (Javascript)?

Quoting from my Javascript book:

(for function arguments):
Scalar arguments are passed by value, which means that a copy of the argument is made for processing in the function, and changes to the argument in the function won't be reflected back to the calling program. Objects though, are passed by reference: any changes to them in the function are reflected in the calling program.

What sense does this make? Why have they done this?
Old 12-03-2010, 11:58 AM   #2
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
It actually ends up making more sense when you understand what's going on. When you pass a scalar value, it only passes the value. When you pass an Object, you are actually passing a reference to the object, which won't be changed by the function. The object the reference points to, though, can be changed. It's a lot like passing pointers to variables in C except that Java hides some of the details. It also doesn't do what you think it will a lot of times. For instance:

public void myMethod1(String s) {
    s = new String("This is a new String");

public void myMethod2() {
    String t = new String("This is the first String");
will print:
This is the first String
This is the first String
because myMethod1 is making s point at a new String object rather than changing the object s references. Because the parameters are passed by value, t in myMethod2 is not altered.

On the other hand, if we do this instead:

public void myMethod1(StringBuffer s) {
    s.append(" This is a new String");

public void myMethod2() {
    StringBuffer t = new StringBuffer("This is the first String");
we'll get this output:

This is the first String
This is the first String This is a new String
because the Object t points to is being altered, not t itself.
1 members found this post helpful.
Old 12-03-2010, 10:29 PM   #3
Registered: Jun 2004
Location: Northern CA
Distribution: Debian
Posts: 835

Rep: Reputation: 49
Java != JavaScript
JavaScript is a misnomer.

(I believe the answer is the same, but your examples include syntax he may be unfamiliar with.)
Old 12-04-2010, 12:16 AM   #4
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Q: What sense does this make? Why have they done this?
A: Because (if you stop to think about it), it's the ONLY approach that makes sense. Unless you want to add "copy constructors" (and everything that goes with it) to Javascript

"Javascript" is emphatically NOT "Java", as gd2shoe already pointed out. But in this instance, they happen to behave similarly.

Last edited by paulsm4; 12-04-2010 at 12:18 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
This just doesn't make any sense! FreezEy Debian 8 04-18-2006 08:35 PM
netmask doesn't make sense ioncristi Member Intro 1 01-14-2006 11:23 AM
How does this make sense? Trinity22 Linux - General 2 03-30-2005 12:45 AM
Does this make sense? (RPM Q:) admstng Mandriva 4 12-30-2004 07:49 AM
Ya sure this make sense Edward78 General 2 09-23-2003 03:00 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:34 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration