Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default change variable given as argument to function

    in pascal I can do this...
    Java Code:
    procedure addTen(var num: integer);
    begin
        num := num + 10;
    end;
    
    //(in the main function somewhere...)
    var i: integer;
    begin
        i := 5;
        addTen(i);
        // i will now have 15 in it
    end;
    like in the pascal example above I want to be able to (in java) give a function a variable and let it edit that variable as if that function was part of the function that called it. This may sound confusing.

    to sum up, I don't want to have to do this:
    i := addTen(i);// (pascal)
    OR IN JAVA:
    i = addTen(i);

    I want to just do this:
    addTen(i);

    How can I do the same in java?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    In Java a variable used in a method call will never have its value changed as a result of that method call.

    If the variable is of reference type (object, including array) you can dereference it and call some method of the object or assign to an array element. But it would not be idiomatic Java to do this merely to avoid writing "foo=whatever(foo);".
    Last edited by pbrockway2; 07-27-2011 at 07:33 PM.

  3. #3
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks for the quick reply, when I am using pascal I usually use this as a simple way to save memory and avoid memory leaks, it's also a shame that I cannot use pointers to achieve this like I could in C or C++.

    Oh well, thanks again for your help.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Danieljabailey View Post
    Thanks for the quick reply, when I am using pascal I usually use this as a simple way to save memory and avoid memory leaks, it's also a shame that I cannot use pointers to achieve this like I could in C or C++.
    Java isn't C nor C++ nor Pascal; you can't pass anything by reference in C either and you can do things in Java you can't do in C nor Pascal; C++ is quite a different beast, i.e. you can do too much in that language, that's why people make such a mess with it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I usually use this as a simple way to save memory and avoid memory leaks
    I was only describing the semantics of method calls and I don't really know about the memory cost of doing things this way. Off hand I can't think why it would be more expensive (but, as I said, I don't know much about how the runtime runs). For memory leaks Java programmers tend to rely on garbage collection.

    In general terms the Java language is marked by its resolute determination not to concern itself with memory.

    it's also a shame that I cannot use pointers to achieve this
    Whether you use pointers to achieve this is up to you. The values that are passed for reference types are pointers (JLS 4.3.1) which is why I mentioned them.

    But I was serious in suggesting that such a technique is not very Java-looking. In particular, assigning a value to a variable and calculating with or using that value are two quite different things. One of the ways that Java keeps things simple is to reflect this difference by having two different syntactic structures for them: the assignment operators (for assigning values to variables), and method calls and other operators (for calculating with or using values).

    Java actually follows C in this respect rather than being contrasted as you suggest. The difference of how pointers are separated from any direct connection with memory (ie no pointer arithmetic) does not affect the method calling semantics. Both are only and always pass by value.

    Knowing that a variable will not change its value as a side effect of its use in a method call or other expression is something you can rely on. If a variable foo has a crazy value you know you can look for the line where an assignment operator assigned that crazy value.

    As a general rule it is good to keep things simple. This rule is proven by the fact that the exception - the one pair of operators that do alter a variable's value (by hallowed tradition) - are the ones that are commonly misunderstood.
    Last edited by pbrockway2; 07-28-2011 at 12:55 AM.

Similar Threads

  1. Function as argument
    By MarsTeam in forum Advanced Java
    Replies: 2
    Last Post: 06-05-2010, 09:33 PM
  2. Function as argument?
    By martypapa in forum AWT / Swing
    Replies: 2
    Last Post: 02-07-2010, 11:02 AM
  3. Setting a variable in JSTL for use in a JSP function
    By MikeStringfellow in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-19-2009, 05:48 PM
  4. Replies: 0
    Last Post: 08-21-2008, 02:26 PM
  5. Function in a variable
    By beserie in forum New To Java
    Replies: 4
    Last Post: 01-07-2008, 04:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •