Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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 1405 times)
0 Members and 1 Guest are viewing this topic.
Offline 2NickPick

Senior Newbie





« Posted 2011-02-17 11: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

JGO Ninja


Medals: 33
Projects: 1


TUER


« Reply #1 - Posted 2011-02-17 11: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
« League of Dukes »

Senior Member


Projects: 1


Used to be bleb


« Reply #2 - Posted 2011-02-17 11: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

JGO Ninja


Medals: 33
Projects: 1


TUER


« Reply #3 - Posted 2011-02-17 11: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 09: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
« League of Dukes »

Senior Member


Projects: 1


Used to be bleb


« Reply #5 - Posted 2011-02-19 11: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 11:26:35 »

1  
2  
3  
public class IntWrapper {
public int i;
}
Offline gouessej

JGO Ninja


Medals: 33
Projects: 1


TUER


« Reply #7 - Posted 2011-02-19 13: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (72 views)
2013-05-17 21:29:12

alaslipknot (83 views)
2013-05-16 21:24:48

gouessej (113 views)
2013-05-16 00:53:38

gouessej (108 views)
2013-05-16 00:17:58

theagentd (119 views)
2013-05-15 15:01:13

theagentd (108 views)
2013-05-15 15:00:54

StreetDoggy (152 views)
2013-05-14 15:56:26

kutucuk (176 views)
2013-05-12 17:10:36

kutucuk (173 views)
2013-05-12 15:36:09

UnluckyDevil (182 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.167 seconds with 20 queries.