int and Integer


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: int and Integer

  1. #1
    Andrew McCallum Guest

    int and Integer


    I come from a C/C++ background.

    I want to write a function where I pass in an int argument and the function
    modifies the value. (in C, void func(int &arg1, int &arg2) )

    From what I understand, basic types (int, long, char) are passed by value,
    and Objects are passed by reference.

    Therefore I can't use int, I should use an Integer object. But my function
    doesn't seem to update the original object.


    public void func(Integer arg1, Integer arg2, String arg3)
    {
    arg1 = Integer.valueOf(arg3);
    arg2 = new Integer(arg1 + 1);
    }

    Also, I seems really hard to update the value of an Integer object. Shouldn't
    there be a arg1.Set(int i) function somewhere?

    Thanks,
    Andrew

  2. #2
    Nikhil Guest

    Re: int and Integer


    Andrew,
    Do you mind to clear the objective of your function.

    Nikhil

    "Andrew McCallum" <Andrew.McCallum@Objective.com> wrote:
    >
    >I come from a C/C++ background.
    >
    >I want to write a function where I pass in an int argument and the function
    >modifies the value. (in C, void func(int &arg1, int &arg2) )
    >
    >From what I understand, basic types (int, long, char) are passed by value,
    >and Objects are passed by reference.
    >
    >Therefore I can't use int, I should use an Integer object. But my function
    >doesn't seem to update the original object.
    >
    >
    >public void func(Integer arg1, Integer arg2, String arg3)
    >{
    > arg1 = Integer.valueOf(arg3);
    > arg2 = new Integer(arg1 + 1);
    >}
    >
    >Also, I seems really hard to update the value of an Integer object. Shouldn't
    >there be a arg1.Set(int i) function somewhere?
    >
    >Thanks,
    >Andrew



  3. #3
    Paul Clapham Guest

    Re: int and Integer

    All parameters to Java methods are passed by value. For primitive types
    such as int, the value of the input parameter is passed. For objects, the
    value of the input parameter is also passed, and this value is a reference
    to the object. It is true that inside the method, you may change the
    object, but you can only do that by calling one of the object's methods to
    do that.

    In your code, "arg2" receives a reference to an Integer object. The code
    "arg2 = new Integer(arg1 + 1)" does not change that object. Instead, it
    causes arg2 to refer to a newly created object. And it has no effect on the
    input parameter, which continues to refer to the original object -- because
    parameters are passed by value.

    And no, you are correct that there are no functions that change the internal
    state of an Integer object. You could create your own MutableInteger class
    if you wanted.

    The bottom line of this is, don't try to change parameters in Java methods.
    This practice is of questionable taste in any programming language, as it
    can lead to side-effects and inconsistencies. If you want to return
    something from a method, let it be the result of the method:

    public Integer func(Integer arg1, String arg3)
    {
    arg1 = Integer.valueOf(arg3);
    return new Integer(arg1 + 1);
    }

    Andrew McCallum <Andrew.McCallum@Objective.com> wrote in message
    news:39c9a079$1@news.devx.com...
    >
    > I come from a C/C++ background.
    >
    > I want to write a function where I pass in an int argument and the

    function
    > modifies the value. (in C, void func(int &arg1, int &arg2) )
    >
    > From what I understand, basic types (int, long, char) are passed by value,
    > and Objects are passed by reference.
    >
    > Therefore I can't use int, I should use an Integer object. But my

    function
    > doesn't seem to update the original object.
    >
    >
    > public void func(Integer arg1, Integer arg2, String arg3)
    > {
    > arg1 = Integer.valueOf(arg3);
    > arg2 = new Integer(arg1 + 1);
    > }
    >
    > Also, I seems really hard to update the value of an Integer object.

    Shouldn't
    > there be a arg1.Set(int i) function somewhere?
    >
    > Thanks,
    > Andrew




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center