Results 1 to 9 of 9
  1. #1
    sudarson is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default Problem regarding function

    Hi all,
    I am in a trouble about functions in java.
    I want to reassign a value of a variable passed from another class. this is just like pointer does in C or C++. This is possible in java that I know but I don't know how.

    here is the pseudocode

    public class read{

    public read() { }

    public void value(int a, int b) {
    a = 10;
    b = 20;
    }
    }

    public class test{

    test () { }

    public static void main (String[] args) {
    int k = 0;
    int l = 1;
    read rd = new read (k,l);
    rd.value(k,l);
    // here I want that k =10, l = 20
    }
    }

    Again:
    e.g.
    PixelGrabber(Image img, int x, int y, int w, int h, int pix[], int off, int scansize);

    Here the dimension of the array pix[] is w*h, and we can use the value of each element of the array pix[].
    The question is how, because there is no concept of pointer in java.
    PixelGrabber use the interface ImageConsumer, that I have seen.
    Is it any role in this regard?

    Please let me know whether I am clear to interested persons or not?
    Thanks in advanced.

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

    Default

    I want to reassign a value of a variable passed from another class.

    In Java all a method or constructor receives is a value. There is simply no way that a variable can have its value changed as a result of being used as an argument in a method call. This never happens.

    Java Code:
    public static void main(String[] args) {
        int foo = 666;
        System.out.println("foo is " + foo);
        change(foo);
        System.out.println("foo is still " + foo); 
    }
    	
    private static void change(int i) {
        i = 0;
    }
    ------------------------------------

    Exactly the same thing applies if the variable in question is an array. The value of an array variable is a reference (or pointer) to an array. That value will never change as a result of the variable being used as an argument in a method call.

    Of course although the value of the variable does not change, the contents of the array being pointed to may change.

    Java Code:
    public static void main(String[] args) {
        int[] arr = new int[] {666, 555, 444};
        System.out.println("arr is " + arr);
        System.out.println("arr contents are " + Arrays.toString(arr));
        change(arr);
        System.out.println("arr still is " + arr);
        System.out.println("arr contents are now " + Arrays.toString(arr));
    }
    	
    private static void change(int[] array) {
        array[0] = -array[0];
        for(int i = 1; i < array.length; i++) {
            array[i] = 0;
        }
    }

    The PixelGrabber example you posted is like this. The caller of the constructor would pass a reference or pointer to an array of ints and, if they happened to use a variable for this that variable would not change its value. The contents of the array can (and probably do) change as the PixelGrabber instance does its work.

    -----------------------------

    The question is how, because there is no concept of pointer in java.

    I don't know about concepts of pointers. But there are actual pointers.

    They aren't pointers to memory locations, nor do they support pointer arithmetic. But, still they point.

    Java Code:
    int[] arr = new int[] {666, 555, 444};

    arr is now a variable whose value points to an array. If arr is used in a method or constructor call that value may well to used to access the array and change its contents in the usual ways.
    Last edited by pbrockway2; 02-25-2011 at 09:46 PM.

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

    Default

    When I posted that I made a revealing slip and talked about the PixelGrabber constructor being passed an array of ints. We often talk like that, but it is important to realise that in Java what is passed is a reference to an array of ints. Unlike other languages we always work with references to objects and never with the objects themselves.

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default Second example

    PBrockWay2 is totally right.

    I made a second example, conforming to the function in the question of the OP (sudarson).
    Like expected: you can change a self-made class (MyInt).
    It doesn't work with Integers (Wrapper-class). I guess that because of the unboxing/boxing in Java 5/6 the local c and d in the method are copies at a new location in memory (I didnt check it in an older Java version though). Maybe someone else can explain this better.

    Java Code:
    package byref;
    
    public class ChangeByRef {
    	ChangeByRef () {
    		testChangingInteger();
    		testChangingMyInt();
    		testIntegerAddition();
    	}
    
    	public static void main (String[] args) {
    		new ChangeByRef();
    	}
    	private void testChangingInteger(){
    		Integer k = new Integer( 5 );
    		Integer l = 5;
    		Integer m = 5;
    		Changer rd = new Changer ();
    		rd.value( k, l );
    		m += 7;
    		System.out.println( "k = " + k  + ", l = " + l + ", m = " + m );
    		//result: k = 5, l = 5, m = 12
    	}
    	private void testChangingMyInt(){
    		MyInt n = new MyInt( 0 );
    		MyInt p = new MyInt( 1 );
    		Changer rd = new Changer ();
    		rd.value( n, p);
    		System.out.println("n = " + n.getValue() + ", p = " + p.getValue() );
    		// here I want that n =10, p = 20, and that's what I get
    		
    	}
    	private void testIntegerAddition(){
    		Integer A,B,C;
    		A = new Integer( 3 );
    		B = new Integer( 4 );
    		C = A + B;
    		System.out.println( "C = " + C  );
    		//result: C = 7
    	}
    }
    class Changer{
    	public Changer() { }
    
    	public void value( Integer c, Integer d ){
    		c += 7;
    		d += 11;
    	}
    	public void value( MyInt a, MyInt b ) {
    		a.setValue( 10 );
    		b.setValue( 20 );
    	}
    
    }
    
    class MyInt{
    	private int value;
    	public MyInt( int theValue ){
    		value = theValue;
    	}
    	public void setValue(int theValue){
    		value = theValue;
    	}
    	public int getValue(){
    		return value;
    	}
    }

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

    Default

    A couple of things:

    First I mentioned before that in Java "There is simply no way that a variable can have its value changed as a result of being used as an argument in a method call". In some langauges this is not the case and the value of a variable can change. That's because a reference (of some kind) to the variable is passed and this goes by the name of "pass by reference".

    I think the package name (and even the class name, perhaps) is unfortunate because it might suggest that such a mechanism (pass the variable by reference) is at work here. And it isn't. "There is simply no way that a variable can have its value changed as a result of being used as an argument in a method call" and that's because the method only ever receives the value of the variable: a semantics known as pass by value.

    Of course you can call your package whatever you want! But I wouldn't want people to think it explemplified anything but pass by value.

    ---------------------------

    As regards boxing/unboxing:

    Regarding the value(Integer c, Integer d) method of Changer:

    the local c and d in the method are copies at a new location in memory

    In an expression like "rd.value( k, l );" the method receives exactly the values k and l have. (These values being references to Integers). If you meant that the method would create new Integers and reassign c and d to refer to them, that's wrong. c and d have exactly the values k and l have ie they refer to exactly the same Integers.

    Rather the problem is with what happens in the lines

    Java Code:
    c += 7;
    d += 11;

    The full details of what is going on here are unimportant: the main thing is that as a result of these lines c and d end up referring to new Integers. The original ones (references to which were passed to the method) are left untouched.

    ----------------------------

    Here's a code example. In Java we have no way of getting the reference value of a variable - the value of the Integer variable that is passed to a method. Here I will use System.identityHashCode() as a surrogate for that value.

    Java Code:
    public class Eg {
        public static void main(String[] args) {
            Integer foo = 666;
            System.out.println("1.foo starts off having reference value #" + System.identityHashCode(foo));
            System.out.println("The reference is to an Integer representing " + foo);
    		
            increment(foo);
    		
            System.out.println("4.In main() after increment has returned");
            System.out.println("foo still has reference value #" + System.identityHashCode(foo));
            System.out.println("(Cos the value of a variable will *never* change as a");
            System.out.println("result of being used as an argument in a method call)");
            System.out.println("The reference is to an Integer representing " + foo);
        }
    	
        private static void increment(Integer i) {
            System.out.println("2.The method receives a reference value #" + System.identityHashCode(i));
            System.out.println("which is a reference to an Integer representing " + i);
    		
            System.out.println("3.Then the increment happens");
            i += 1;
            System.out.println("And now the method's reference value is #" + System.identityHashCode(i));
            System.out.println("which is a reference to an Integer representing " + i);
        }
    }

    [edit] Changed the class name because boxing is only part of what is going on in the i+=1 line. The most fundemental thing about the Integer class is that it is immutable: that is the value represented by an Integer will never change. Consequently if increment() is going to do any incrementing it has to create a new Integer one way or another.
    Last edited by pbrockway2; 02-26-2011 at 03:36 AM.

  6. #6
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I'm not going too much into semantics (saying that the value of a reference is passed, which in Java is not retrievable as a memory-address), but I don't agree with your first statement: after all, the arguments n and p are changed by the call to value(MyInt, MyInt). Resulting in 10 and 20 as the OP tried to achieve, I don't make this up.

    Your diagnosis of what happens to the Integers looks like mine, i.m.h.o.

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

    Default

    the arguments n and p are changed by the call to value(MyInt, MyInt)

    That statement is ambiguous: it could mean either that the values of n and p are changed, or it could mean that the state of the objects referenced by those variables is changed. We use names both ways in English. As a result of an earthquake a house may certainly change: but a mailman will still deliver mail to whatever remains of its letterbox. That's because the writing on the front of the envelope does not change its reference value however much the thing referenced changes its state.

    In fact the values of the variables n and p are not changed. That's because "There is simply no way that a variable can have its value changed as a result of being used as an argument in a method call".

    The state of (roughly, content of) the objects referenced by those variables is changed.

    I won't post the code analogous to that of my previous post, but examining the values of getValue() and identityHashValue() before, after, and during the method calls will bear out the two previous statements about what is going on.

    --------------------------------

    The OP asked how an int array can be effective as an argument in the PixelGrabber constructor in a language that does not have pointers. My answer was to point out that what the constructor receives is the reference value of the actual parameter. The value of the actual parameter will remain unchanged, but it is a pointer and, consequently, can be used to change the array's contents.

    I did not address the OP's initial problem - "I want to reassign a value of a variable passed from another class" - because I figured they could join the dots themselves once they had more precisely posed the problem. (They had, after all, drawn attention to how what they are after was actually done in the case of PixelGrabber.)
    Last edited by pbrockway2; 02-26-2011 at 09:43 PM.

  8. #8
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    The point of changing a byRef-argument is that you can change the object it’s pointing to.
    It is possible to change things by passing them byRef as argument.

    (I would strongly discourage this in most cases though because it makes your code very difficult to grasp.)

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

    Default

    The point of changing a byRef-argument is that

    Not only is "changing" - still - ambiguous. But there are now byRef-arguments to deal with and a notion of passing byRef.

    Anyone interested in how Java goes about passing things (including variables declared to be instances of a mutable class: where the value - a reference - is copied and passed) see JavaRanch Campfire - Cup Size: a Story About Variables and JavaRanch Campfire - Pass By Value, Please

Similar Threads

  1. split() function problem.
    By robWhittle in forum New To Java
    Replies: 2
    Last Post: 04-09-2010, 01:34 PM
  2. Problem with split function
    By a.tajj in forum New To Java
    Replies: 4
    Last Post: 04-14-2009, 03:30 AM
  3. Weird problem upon calling same function twice
    By alin_ms in forum New To Java
    Replies: 2
    Last Post: 12-20-2008, 06:14 PM
  4. problem with Vectors and getTotal() function
    By java_fun2007 in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 01:55 PM
  5. Function declaration problem.
    By snooze-g in forum Advanced Java
    Replies: 3
    Last Post: 07-18-2007, 09:15 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
  •