LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 12-03-2010, 09:40 AM   #1
resetreset
Senior Member
 
Registered: Mar 2008
Location: India
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,320

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, 12:58 PM   #2
tsg
Member
 
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:

Code:
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");
    System.out.println(t);
    myMethod1(t);
    System.out.println(t);
}
will print:
Code:
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:

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

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

Code:
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, 11:29 PM   #3
gd2shoe
Member
 
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, 01:16 AM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
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

PS:
"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 01:18 AM.
 
  


Reply


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 09:35 PM
netmask doesn't make sense ioncristi LinuxQuestions.org Member Intro 1 01-14-2006 12:23 PM
How does this make sense? Trinity22 Linux - General 2 03-30-2005 01:45 AM
Does this make sense? (RPM Q:) admstng Mandriva 4 12-30-2004 08:49 AM
Ya sure this make sense Edward78 General 2 09-23-2003 04:00 PM


All times are GMT -5. The time now is 12:53 AM.

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