oop - Pass by value and pass by reference in java -



oop - Pass by value and pass by reference in java -

i reading pass value , pass reference in java, got many articles,some of them saying java next 'pass value " , of them saying difference between primitive , object. wrote next sample code. , putting output also. please comment , share reply is.

i checked int, string , stringbuffer , employee class, working pass reference employee class only.

package test; class emp { public string name=""; public int age=0; public emp(string name, int age) { super(); this.name = name; this.age = age; } public string tostring() { homecoming "name: "+ this.name + "....age: "+ this.age; } } public class class1 { public class1() { super(); } public void dochange(int i) { = +10; system.out.println("value of integer in method:>"+ i); } public void dochange(emp i) { i.age=29; i.name="rishu"; system.out.println("value of employee in method "+i.tostring()); } public void dochange(string i) { = + " hello"; system.out.println("value of i->"+ i); } public static void main(string[] args) { int =10; string str="xxx"; class1 c= new class1(); stringbuffer sb= new stringbuffer(); emp e= new emp("abhi",28); sb.append("abc "); system.out.println(""); system.out.println("value of integer before method:->"+ i); c.dochange(i); system.out.println("value of integer after method:->"+ i); system.out.println(""); system.out.println("value of string before method:->"+ str); c.dochange(str); system.out.println("value of integer after method:->"+ str); system.out.println(""); system.out.println("value of stringbuffer before method:->"+ sb); c.dochange(sb.tostring()); system.out.println("value of stringbuffer after method:->"+ sb); system.out.println(""); system.out.println("value of employee before method:->"+ e.tostring()); c.dochange(e); system.out.println("value of employee after method:->"+ e.tostring()); } }

output:

value of integer before method:->10 value of integer in method:>20 value of integer after method:->10 value of string before method:->xxx value of i->xxx hello value of integer after method:->xxx value of stringbuffer before method:->abc value of i->abc hello value of stringbuffer after method:->abc value of employee before method:->name: abhi....age: 28 value of employee in method name: rishu....age: 29 value of employee after method:->name: rishu....age: 29

java pass-by-value only.

however, objects, passed value reference object. no, that's not same pass-by-reference. difference this:

if in this:

public void dochange(emp i) { = new emp("changed!", 42); }

it has absolutely no effect outside method - because reference i re-create of reference used outside method. however, refers same object, if utilize reference alter fields of object, these changes visible outside method.

java oop core

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -