Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Pass By Reference Doesn't Work For Wrapper Classes??  (Read 2545 times)
0 Members and 1 Guest are viewing this topic.
Offline 2NickPick

Senior Newbie





« Posted 2011-02-17 10:05:16 »

Here's a curve ball, maybe someone can explain it to me.
Apparently someone decided Wrapper classes won't act like references, but like values?

Program Output:
Quote
intital primitive int: 6
value returned by method call: 100
after method call: 6

inital Wrapper value: 12
value returned by method call: 100
after method call: 12

initial Atomic value: 24
value returned by method call: 100
after method call: 24

correct method primitive: 30
value returned by method call: 100
after method call: 100

Process completed.


Source Code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
public class TestPassByRef
{
   public static void main( String [] args )
   {
      /* not supposed to work. Pass By Value */
      int i = 6;
      System.out.println( "intital primitive int: " + i );
      changeInt( i );
      System.out.println( "after method call: " + i );

      /* Doesn't work. Pass By Ref acts like Pass By Value*/
      Integer wrapperI = new Integer( 12 );
      System.out.println( "\ninital Wrapper value: " + wrapperI );
      changeInteger( wrapperI );
      System.out.println( "after method call: " + wrapperI );

      /* Doesn't work also. */
      AtomicInteger ai = new AtomicInteger( 24 );
      System.out.println( "\ninitial Atomic value: " + ai );
      changeAtomicInteger( ai );
      System.out.println( "after method call: " + ai );

      /* what should be done */
      int i2 = 30;
      System.out.println( "\ncorrect method primitive: " + i2 );
      i2 = changeInt( i2 ); //assign return value of method to original integer
     System.out.println( "after method call: " + i2 );

   }

   private static Integer changeInt( int i )
   {
      i = 100;
      System.out.println( "value returned by method call: " + i );
      return i;
   }

   private static Integer changeInteger( Integer i )
   {
      i = 100;
      System.out.println( "value returned by method call: " + i );
      return i;
   }

   private static AtomicInteger changeAtomicInteger( AtomicInteger i )
   {
      i = new AtomicInteger( 100 );
      System.out.println( "value returned by method call: " + i );
      return i;
   }

}
Offline gouessej
« Reply #1 - Posted 2011-02-17 10:22:42 »

Hi!

You don't understand how Java works. In this example:
1  
2  
3  
4  
5  
6  
private static AtomicInteger changeAtomicInteger( AtomicInteger i )
    {
        i = new AtomicInteger( 100 );
        System.out.println( "value returned by method call: " + i );
        return i;
    }

The global reference of the object is copied as i is passed as an argument. When you do i = new AtomicInteger( 100 ), you set another local reference, you don't change the global reference which means that the value of i does not change once you get outside your method, it is logical. You should rather use a setter to modify the value contained in AtomicInteger, look at the documentation of this class.

Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #2 - Posted 2011-02-17 10:35:10 »

As gouessej says, java passes everything by value - passing an object passes the value of the object reference.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gouessej
« Reply #3 - Posted 2011-02-17 10:45:04 »

As gouessej says, java passes everything by value - passing an object passes the value of the object reference.
That is right, you have better sumed up what I meant. The reference of the object is copied when this kind of argument is used as a method parameter.

Offline 2NickPick

Senior Newbie





« Reply #4 - Posted 2011-02-19 08:40:23 »

So how exactly would I go about changing the global reference rather than creating a new local reference?

I see where the break is...
 
It seemed to me that when I say

changeInteger( wrapperI );

that I'm passing a pointer to an Integer object into the method and inside the method, the parameter i is pointing to the same thing as global variable wrapperI. So when I changed i inside of the method it should also change wrapperI.

BUT, when I said i = new Integer( 100 ), i no longer pointed to what wrapperI pointed to. And I'm assuming ( by looking at the API ) that Integer is immutable, therefore there is no way to change its value without creating a new instance of Integer and assigning to the previous Integer object reference. So it is impossible to change an integer( of any kind) by passing it into a method. You HAVE to assign it in its original scope.

Tell me if I'm wrong.
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #5 - Posted 2011-02-19 10:05:40 »

Yes, java.lang.Integer is immutable. You would have to define your own mutable integer wrapper class.
Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #6 - Posted 2011-02-19 10:26:35 »

1  
2  
3  
public class IntWrapper {
public int i;
}
Offline gouessej
« Reply #7 - Posted 2011-02-19 12:57:24 »

So how exactly would I go about changing the global reference rather than creating a new local reference?

I see where the break is...
 
It seemed to me that when I say

changeInteger( wrapperI );

that I'm passing a pointer to an Integer object into the method and inside the method, the parameter i is pointing to the same thing as global variable wrapperI. So when I changed i inside of the method it should also change wrapperI.

BUT, when I said i = new Integer( 100 ), i no longer pointed to what wrapperI pointed to. And I'm assuming ( by looking at the API ) that Integer is immutable, therefore there is no way to change its value without creating a new instance of Integer and assigning to the previous Integer object reference. So it is impossible to change an integer( of any kind) by passing it into a method. You HAVE to assign it in its original scope.

Tell me if I'm wrong.
Rather use your own wrapper with your own write accessor (setInt(int i)).

Autoboxing does not ease these things. When you do i = 100, it is like doing i = new Integer(100); because i is an Integer whereas 100 is an int.

Anyway your way of doing was not right, you should have looked for a modifier in the API to change the enclosed int in the Integer instance. As Integer is immutable, there is no such modifier and you should create your own class.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (24 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!