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.
LVL 7
gudii9Asked:
Who is Participating?
 
for_yanCommented:

>>>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);


This text above
I was writing about your code which you can find below in this piosting

In this code below you have a method
public int another(int i){...}

then in another place you are calling this method:

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

What I was saying is that even though this will not cause
an error - this call
another(i);
is pretty senseless operation,
because even if
another(i) returns some integer, you are not providing
variable in the calling sequence to capture the result of this method.
If so then why to run this method?

What may have more sense would be:

.....
     System.out.println("Before another i= " +i);
             int j = another(i);
              System.out.println("After another i= " + i + " and j = " + j);
....

Otherwise you are just calling method which returns you some
int value, but  the calling code simply does not care what it returns.

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


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

}
----------------------------------------------


0
 
objectsCommented:
Java passes parameters by value so the *value* is passed in and used by the method . The value of i in amethod() is never changed.
http://stackoverflow.com/questions/40480/is-java-pass-by-reference
0
 
for_yanCommented:
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
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
gudii9Author Commented:
we cannot define i as static right to change it globally. please advie
0
 
objectsCommented:
yes you could do that. though the point of the example is to show you that the value passed in does not get changed
0
 
gudii9Author Commented:
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
0
 
objectsCommented:
those examples are passing objects, the question is about passing primitives
0
 
ioantonCommented:
You should take it in the following manner: Java always creates a copy of the variable and that copy is passed to the method as argument. The called method hasn't access to the original variable.
- if the variable is a primitive, the method can alter the value of the copy only.
- 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.
0
 
Sathish David Kumar NArchitectCommented:
>>>>>       public void another(int i){
              i+=10;
              System.out.println("In another i= " + i);
        }//End of another

your not return anything more over both i values are local to that class then how the value will change ???
0
 
gudii9Author Commented:
>>>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
0
 
gudii9Author Commented:
>>>- 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.
0
 
for_yanCommented:
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).

0
 
objectsCommented:
> 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.
0
 
gudii9Author Commented:

>>>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
0
 
gudii9Author Commented:
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.
0
 
gudii9Author Commented:
>>Java always creates a copy of the variable


u mean both primitive variable as well as object instance reference variable both. please advise
0
 
objectsCommented:
Java always passes by value
http://javadude.com/articles/passbyvalue.htm
0
 
objectsCommented:
don't starting thinking of it as a copy that will just confuse you.
It simply passes the value
0
 
for_yanCommented:


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.





 
0
 
for_yanCommented:
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.
0
 
gudii9Author Commented:
>>>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.
0
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.

All Courses

From novice to tech pro — start learning today.