Results 1 to 12 of 12
  1. #1
    becky is offline Member
    Join Date
    Jan 2009
    Posts
    13
    Rep Power
    0

    Default pointers and wrapper classes

    hi

    since there are no pointers in java (like in C/C++) i read somewhere in a forum to use wrapper classes instead.
    but somehow i can't get it working with wrapper classes:

    Java Code:
    public class test1 {
    
        public test1() {
            Integer a=new Integer(1);
            test2(a);
            System.out.println(a);
        }
    
        private void test2(Integer a) {
            Integer b;
            b=a;
            b++;
        }
    
        public static void main(String[] args) {
            new test1();
        }
    }
    the output of the variable "a" is always 1.
    but it should be 2 because of the "b++;" in the method "test2(Integer a)".
    can you help me to get this working, please?

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    you have to return b; in test2. and set a = test2(a); in test1.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    becky is offline Member
    Join Date
    Jan 2009
    Posts
    13
    Rep Power
    0

    Default

    i know. this would be a solution but i want to solve this problem with wrapper classes. because a return with 2 values isn't possible.
    here an example with 2 return values:
    Java Code:
    public class test1 {
    
        public test1() {
            Integer a=new Integer(1);
            Integer a1=new Integer(5);
            test2(a,a1);
            System.out.println(a);
            System.out.println(a1);
        }
    
        private void test2(Integer a,Integer a1) {
            Integer b;
            Integer b1;
            b=a;
            b1=a1;
            b++;
            b1++;
        }
    
        public static void main(String[] args) {
            new test1();
        }
    }
    but somehow this doesn't work...

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    oh sorry, read too fast and missed the wrapper part.
    you need to do something like this to return two values:
    Java Code:
    public class test1 {
    
        public test1() {
            Integer a=new Integer(1);
            Integer a1=new Integer(5);
            [B]MyWrapper w = test2(a,a1);
            a = w.a;
            a1 = w.b;[/B]
            System.out.println(a);
            System.out.println(a1);
        }
    
        private [B]MyWrapper[/B] test2(Integer a,Integer a1) {
            Integer b;
            Integer b1;
            b=a;
            b1=a1;
            b++;
            b1++;
            [B]return new MyWrapper(b,b1);[/B]
        }
    
        public static void main(String[] args) {
            new test1();
        }
    }
    
    [B]class MyWrapper{
      public Integer a;
      public Integer b;
      
      public MyWrapper(Integer a, Integer b){
        this.a = a;
        this.b = b;
      }
    }[/B]
    or you could just make a and a1 into a field instead of a variable. or use an array.
    Last edited by angryboy; 02-03-2009 at 12:35 AM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    the point the op is trying to understand is pass by reference. for example, if you create an array int[] arr = { 0, 3 }; and call something like modify(arr); where modify() does arr[0] = 5;, then in the original method a System.out.println(arr[0]); will print "5"

    anyways, here is what happens.
    Integer a = new Integer(1);
    ...
    Integer b = a;
    b++;

    the question is, what does b++ do?

    b++ --> b = b + 1;

    that is, a new Integer is created, and b now references a new Integer == 2, while the reference to a is still == 1

    if you think this is confusing, wait til you have fun with Strings and the constant pool.
    Last edited by emceenugget; 02-03-2009 at 01:17 AM. Reason: clarification

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Cool npe

    In general a primitive such as an int, may be done as you ready by wrapping it in a class, that is what class Integer and so on are. You then get a "reference" ( okay, pointer, whatever, an address the compiler tracks ) by doing

    Java Code:
    aClassOfSomeKind acosk = new aClassOfSomeKind();
    and one may pass in various stuff between the ()

    I think Java could move at a Glaical Pace someday if it would thaw on this abhorrence of how the underlying machine actually works. The equivalent of #define NULL 0 in Java is trapped with a NullPointerException which is the same as trying to get at an object that was never done as my code snippet. There is no zero as a null in Java, we use an explictly coded and definte boolean datatype, distinct from numerics.

    Ditto char.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  7. #7
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    The Integer class holds it's value as final. You can not change it. What you want is a wrapper class that doesn't do that. It's easy enough to make your own class for that:

    Java Code:
    class IntWrapper {
       public int val;  // poor programming practice in general
    }
    
    public static void main(String[] args) {
       IntWrapper wrapItUp = new IntWrapper();
       wrapItUp.val = 3;
    
       addOne(wrapItUp);
    
       System.out.println("The value is now 4 --->"+wrapItUp.val);
    }
    
    public static void addOne(IntWrapper wrapItUp) {
      wrapitUp.val++;
    }

  8. #8
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    i think everyone is confused by the OP. becky, do you want to make the code work, or is it that you want to learn about pass-by-value/reference stuff?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  9. #9
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    Funny example linked to wrappers:
    Java Code:
            Integer i11 = 1;
            Integer i12 = 1;       
            Integer i21 = 201;
            Integer i22 = 201;
            System.out.println(i11 == i12);        
            System.out.println(i21 == i22);
    Output of this:
    true
    false

  10. #10
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    WT.Heck! oh man thats so good. you should have saved it for Quiz Time.
    Please do explain why?
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  11. #11
    becky is offline Member
    Join Date
    Jan 2009
    Posts
    13
    Rep Power
    0

    Default

    becky, do you want to make the code work, or is it that you want to learn about pass-by-value/reference stuff?
    i just wanted to learn something about references.

  12. #12
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Oh good, Kaiser's back - where's Norm?

    A reference is actually something that comes from how the machine works. Early on ( by todays timeframe ) it became simper to track where something was in the machine rather than moving it directly - what a reference is ( simplified ) the location of something - some data structure ( class in Java terminology )

    When one codes int, that is conceptually a number in a register, when you code Integer aNumber = new Integer( 2542 ); what happens is the .... ( I'm gonna get hammered by masters ) machine generates some additional instructions ... uh it gets too deep.

    A reference is tantamount to the dictionary definition of a reference.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Java pointers? How to...
    By Krooger in forum New To Java
    Replies: 4
    Last Post: 11-04-2008, 09:30 PM
  2. Secure FTP Wrapper 3.0.2
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-29-2008, 07:05 PM
  3. wrapper classes
    By sireesha in forum New To Java
    Replies: 5
    Last Post: 12-11-2007, 10:45 PM
  4. Pointers
    By ravian in forum New To Java
    Replies: 5
    Last Post: 11-28-2007, 02:49 PM

Posting Permissions

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