Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

Primitives as method parameters

public class Parm{
public static void main(String argv[]){
                Parm p = new Parm();
                p.amethod();
        }//End of main

        public void amethod(){
              int i=10;
              System.out.println("Before another i= " +i);
              another(i);
              System.out.println("After another i= " + i);
        }//End of amethod

        public void another(int i){
              i+=10;
              System.out.println("In another i= " + i);
        }//End of another

}
The output from this program is as follows

Before another i= 10
In another i= 20
After another i= 10



I am looking to above program from link

http://www.jchq.net/certkey/0504certkey.htm

I have not clearly understood the output.

Why it is 10 after another as well.

Any ideas, resources,sample code,links,  highly appreciated. thanks in advance.
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I guess, better to say that the value of i in another() did change, but no one passed this
value back to amethod, therfore in amethod variable i remains independent and value of i remained the same.

This is the difference with the object passing, where there is physically just one object in memory and
we just pass the reference to its location in memory from one method to another.
When we pass primitive type the primnitive value in method another() is stored
in a separate memory cell and when you chnage it in the another() method, unless
you pass it back, say through assignement of the return value, in the calling method
that "i" is absolutely different variable and it will not be influenced what happens with i in the another() method
Avatar of gudii9

ASKER

we cannot define i as static right to change it globally. please advie
yes you could do that. though the point of the example is to show you that the value passed in does not get changed
Avatar of gudii9

ASKER

I was going through the link

>>>>

Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references passed by value.

It goes like this:

public void foo(Dog d) {
  d.name == "Max"; // true
  d = new Dog("Fifi");
  d.name == "Fifi"; // true
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.name == "Max"; // true
In this example aDog.name will still be "Max". "d" is not overwritten in the function as the object reference is passed by value.

Likewise:

public void foo(Dog d) {
  d.name == "Max"; // true
  d.setname("Fifi");
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.name == "Fifi"; // true


I am still not clear on this concept. please advise
those examples are passing objects, the question is about passing primitives
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

>>>I guess, better to say that the value of i in another() did change, but no one passed this
value back to amethod, therfore in amethod variable i remains independent and value of i remained the same.

>>>your not return anything more over both i values are local to that class then how the value will change ???

I changed like



public class Parm{
public static void main(String argv[]){
                Parm p = new Parm();
                p.amethod();
        }//End of main

        public void amethod(){
              int i=10;
              System.out.println("Before another i= " +i);
              another(i);
              System.out.println("After another i= " + i);
        }//End of amethod

        public int another(int i){
              i+=10;
              System.out.println("In another i= " + i);
              return i;
        }//End of another

}


now essentially i am returning value back to calling method still i get output like(After another i= 10)


Before another i= 10
In another i= 20
After another i= 10


please advise
Avatar of gudii9

ASKER

>>>- if the variable is a primitive, the method can alter the value of the copy only.

but the calling program wont see the change right. i mean in the program at

>  another(i);
              System.out.println("After another i= " + i);

>>.if the variable is a reference, the same rule applies; only the copy reference variable itself can be changed (e.g. set to null or to another instance), but because the copy points to the same object as the original reference, the method can change the properties of the object the reference points to.


this is not clear. can you please elaborate.
No, when you say
another(i);
you are not returtning anything even if you declared that
method shouild returt integer.
You need to get that what method returns like that:

int i = antother(i);

then your new vallue of i will really be something
which method returned,

If you just say

another(i);

you do not in fact return value even if you declare
that method shoudl return integer
(sorry for repetition - but it is very important to unsderstand).

> now essentially i am returning value back to calling method still i get output like(After another i= 10)

but you do nothing with the returned value

              another(i);

so it gets 'lost'

> >  another(i);

When you call that a copy of i is made in the another method. It is that copy that gets updated. So the value of i in amethod() remains unchanged.
Avatar of gudii9

ASKER


>>>When we pass primitive type the primnitive value in method another() is stored
in a separate memory cell



separate memory cell than original i cell or something else. I am really confused about pass by value, pass by reference. is there is any simple practical example with which i can relate and wont forget. please advise
Avatar of gudii9

ASKER

i guess i need more clarity and real world example relation to understand this concept. please advise
Java is always pass-by-value vs
Java passes objects as references passed by value.
Avatar of gudii9

ASKER

>>Java always creates a copy of the variable


u mean both primitive variable as well as object instance reference variable both. please advise
don't starting thinking of it as a copy that will just confuse you.
It simply passes the value


we went may times at many exapmples above; and I think at this point examples will only more confuse you.
JJust rememeber one very simple thing :

Objects are passed by reference - it means method gets the address of the begining of the object (always assume that
object takes consecutive area in memory, and method receive the address of the first celll);
Look, I soemwere evevn gave you examples with the cell addresses relative to the beginning.

Primitives are passed by value - so java createas a copy of the value in another cell, and methods
deals with tha new cell, not with the old cell and not with the original of this primnitive..

So these two statements explain everything :
If you passed instance of object to the method and then method after receiveing this address of the first cell
calculated where you store some particuler field of this object and modified this field. After that if you reach the same
object form the part of the code which called this method, from the calling code, after you retruned
to the calling frlow after method returned, - we'll see modified field of that object - because we had one and only one
actual instance of the object.

If you passed primnitive to the method - then method got its own copy in a special cell - it can modify
this copy, but the calling code would still see the old value of that prmitive, becauyse it was special
ciopy wahich was modified by the method, not the original which remained the same.

Just read attentively the above four paragraphs and I'm sure you'l understand - this is really very simple,
there is nothing to be confused here.





 
No, you actually need to understand that in passing of primitives mathod gets value in another location in memory,
otherwise you would not understand why when you return to the calling code from the method
after you did something with the primitive in the method, its value in the calling code has not channged.
Call it copy or not call it copy but method manipulates with another physical cell which contains the primitive, rather than the calling code.
Avatar of gudii9

ASKER

>>>No, when you say
>>>another(i);
>>>you are not returtning anything even if you declared that
>>>method shouild returt integer.
>>>You need to get that what method returns like that:

>>>int i = antother(i);









you mean i need to modify the program like this?




public class Parm{
public static void main(String argv[]){
                Parm p = new Parm();
               int k= p.amethod(13);
        }
        public int another(int i){
              i+=10;
              System.out.println("In another i= " + i);
              return i;
        }//End of another

}

But i am getting error like 'create amethod(int)'.

Please advise how can i modify the program to work/run.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial