PLS HELP.. Passing by refrence problem....

I thght Integer, Double etc the so called wrppers can be thght of as pbject which make them automatiocally passed by refrence to any function just like any class object that we make... But the same is not working outt.....

heres an exmple
class sample
{
public static void main(String args[])
{
Integer i=new Integer(1);

call_me(i);

System.out.println(b1.b);
}
static void call_me(Integer c)
{
int s=10;
c=new Integer(s);
}
}

The output is 0... How to make int, double etc like data types passed as refrence just for eliminating the return statement in the same .....
sharma_deepak83Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
What is
     System.out.println(b1.b);
?
0
nesnemisCommented:
Hi sharma_deepak83,

you don't put any value into b1.b

nesnemis
0
girionisCommented:
Everything in java is passed by value. What is the "b1" variable?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

zzynxSoftware engineerCommented:
>> just for eliminating the return statement
Why trying to eliminate that?
0
zzynxSoftware engineerCommented:
Replace
    System.out.println(b1.b);
by
    System.out.println(i);
0
sciuriwareCommented:
You don't put anything back in 'call_me'
the 'c' is just a copy of a reference that's replaced by:
                  c=new Integer(s);
So, 'c' points at something new that's ignored on return.
<*>
0
zzynxSoftware engineerCommented:
Right.  That's why

>>Replace
>>    System.out.println(b1.b);
>>by
>>    System.out.println(i);

will output "1" (instead of the expected 10)
0
zzynxSoftware engineerCommented:
...while this prints 10 of course:

class sample
{
    public static void main(String args[]) {
        Integer i=new Integer(1);
        i = call_me();

        System.out.println(i);
    }
   
    static Integer call_me(){
        int s=10;
        return new Integer(s);
    }
}
0
zzynxSoftware engineerCommented:
At http://www.mindprod.com/jgloss/parameters.html you can read:
parameters are always passed by value not by reference. The called method gets a copy of each primitive and a copy of the pointer to each object. The called method can therefore not change any local variables in the caller. However it can change the contents of objects (which includes strings and arrays) passed to it. Some people may argue that counts as call by reference, however a callee cannot make the callers variables point to different objects, just change the contents of objects. Objects are always accessed by reference, (literally via a reference) even when parameters are not involved. You can kludge call by reference by passing values back to your caller inside an array or by returning a freshly minted object full of values.

http://www.mindprod.com/jgloss/callbyvalue.html
0
sciuriwareCommented:
Even on a fibre I'm too slow for you ..............................
0
zzynxSoftware engineerCommented:
The interesting is: "The called method gets a copy ... of the pointer to each object"

So this does work:

class sample
{
    public static void main(String args[]) {
        MyInteger i = new MyInteger(1);
        call_me(i);
        System.out.println(i.getValue());
    }
   
    static void call_me(MyInteger i){
        int s=10;
        i.setValue(new Integer(s));
    }
}

with

class MyInteger {
    private Integer theInt;

    public MyInteger(int i) {
        theInt = new Integer(i);
    }
    public Integer getValue() {
        return theInt;
    }
    public void setValue(Integer i) {
        theInt = i;
    }
}
0
zzynxSoftware engineerCommented:
>> Even on a fibre I'm too slow for you
;°)
0
sharma_deepak83Author Commented:
class inh
{
int b;}
class sample
{
public static void main(String args[])
{
Integer i=new Integer(1);
inh i1=new inh();
call_me(i);
call_inh(i1);
System.out.println(i,intValue());
System.out.println(i1.b);
}
static void call_inh(inh c)
{
c.b=10;
}
static void call_me(Integer c)
{
int s=10;
c=new Integer(s);
}
}


The call_inh is added just to show what I want. I dont want the return statement cos I want more than one thing to return to the calling function...
call_inh sets the value of the member b to 10 which can be seen in the main also. But how do I set the vakue of an Integer wrapper object so that the chages remain even when the call goes back to the calling function .....
0
girionisCommented:
> But how do I set the vakue of an Integer wrapper object so that the
> chages remain even when the call goes back to the calling function .....

Look at zzynx's last example. You will need to refer to another object that has the value you want.
0
zzynxSoftware engineerCommented:
>>>> But how do I set the vakue of an Integer wrapper object so that the
>>>> chages remain even when the call goes back to the calling function .....
>>Look at zzynx's last example
Exactly ;°)
0
OrtokoboldCommented:
Look:

1. You created object "i", type Integer. "i" just becomes a named reference to the object you created on the heap.
2. You passed the reference to this object to method "call_me"..
3. Method "call_me" got the reference to this object: it created local variable "c", being reference to the object which you created in step 1.
4. You typed "c = new Integer(s)". From now on local variable "c" will hold reference to the NEW object which you just created on the heap.

Got it?

So - you are not able to change the value of the wrapper object (Integer, Byte, Long,...) or String. If you want to do something like this, you can use this method:

1. Pass 1-element array:
int main()
{
  int] i = new int[] {1};
  call_me(i);
  System.out.println(i[0]);
}

void call_me(int[] c)
{
  c[0] = 10;
}

If your method will accept int[], the local method attribute will hold the reference to the array. So it will be exactly the same Object you passed to this method. And you'll be able to change its elements.

2. Use your own wrapper class:

class MyOwnIntWrapper
{
  int i;

  MyOwnIntWrapper(int i)
  {
    this.i = i;
  }
}

int main()
{
  MyOwnIntWrapper i = new MyOwnIntWrapper(1);

  call_me(i);
}

void call_me(MyOwnIntWrapper c)
{
  c.i = 10;
}
0
OrtokoboldCommented:
And my 2nd example is exactly the same, as proposed by zzynx (sorry, just noticed... :-))
0
sharma_deepak83Author Commented:
Then whats the point of using the Integer Wrapper. I have a 2700 line code that I am writing so if I start in making my own wrapper its going to add to code complexity.... I am sorry that was not the answer to my question.... I guess I finally have to make my own wrapper now....



0
CEHJCommented:
In order to do what you want, you need to use a custom mutable integer:

static void call_me(MyInteger c)
{
      int s=10;
      c.setValue(s);
}
0
zzynxSoftware engineerCommented:
>> I am sorry that was not the answer to my question
Well this is your Q:
>>I thght Integer, Double etc the so called wrppers can be thght of as pbject which make them automatiocally passed by refrence to any function

The answer is simply: you think wrong. Parameters are always passed by value not by reference.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSoftware engineerCommented:
All the rest we gave you are workarounds.
Your Q was:
>> How to make int, double etc like data types passed as refrence

If you don't like them that's your good right.
But, don't say that it was not the answer to your question.
0
OrtokoboldCommented:
Well, you use wrappers for other purposes. :-) For example: Integer is the Object, while int is not. So you can put Integers into the Collection or Map, but you cannot do it with simple ints (at least: not in JDK 1.4 and earlier).
0
OrtokoboldCommented:
Guys, I think we have a little misunderstanding here.
He said that he's sorry that Integers and similar "wrappers" are not answers to his question - not yours answers! :-))) (unless I'm mistaken - English is not my first language...)
0
sharma_deepak83Author Commented:
I am sorry for saying that and I take my words back...

But for clarification heres my question again

>>>I thght Integer, Double etc the so called wrppers can be thght of as pbject which make them automatiocally passed by refrence to any function just like any class object that we make... But the same is not working outt...

I knwo that in JAVA everything is passed as value but when I say pass by refrence its a mere play of objects  isnt it....

ex

class inh
{
int b;
}


when I make an object of the above class and pass to any func. The function modifys the value of b to 10. The change is visiblein the calling function as well.......... If I know this wouldnt I know how to write my wrapper.........

Well I am sorry. WHY?? To confuse you all with my terminology, it helps me in understanding but makes most confuse otherwise... The points will be awarded as usual........ I am again sorry but no hard feelings.....
0
sharma_deepak83Author Commented:
Exception in thread "main" java.lang.UnsatisfiedLinkError: /root/j2sdk1.4.1/jre/lib/i386/libfontmanager.so: libstdc++-libc6.1-1.so.2: cannot open shared object file: No such file or directory
 I am working on LINUX and I get this error message when I try to open any jar file using

java  -jar {filename}

How to rectify this problem????
0
girionisCommented:
>when I make an object of the above class and pass to any func. The function modifys the value of b to 10. The change is
>visiblein the calling function as well.......... If I know this wouldnt I know how to write my wrapper.........

When you pass a reference to a method it is the reference itself that is passed by value and not the object that the reference is pointing at. If you modify the object that the reference is pointing at then when you return your "old" reference will still point at the changed object, so both the calling function and the called function will point at the same object.
0
girionisCommented:
>  Exception in thread "main" java.lang.UnsatisfiedLinkError:

That's a completely different error and I suggest you open a new question for this.
0
zzynxSoftware engineerCommented:
>> That's a completely different error and I suggest you open a new question for this.
I agree with that
0
sharma_deepak83Author Commented:
OK
0
zzynxSoftware engineerCommented:
...and maybe close this one ;°)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.