akshaymattoo
BAN USERI am pasting a code where i did the same thing but it is changing the calling code reference.
{
package com.test;
public class CallByReference {
public static void main(String[] args) {
Number a = new Number();
a.x = 3;
System.out.println("Value of a.x before calling increment() is " + a.x);
increment(a);
System.out.println("Value of a.x after calling increment() is " + a.x);
}
public static void increment(Number n) {
Number temp = n;
System.out.println("Value of temp before incrementing x is " + temp.x);
temp.x = temp.x + 1;
System.out.println("Value of temp.x after incrementing x is " + temp.x);
}
}
class Number {
int x;
}
}
the output is
Value of a.x before calling increment() is 3
Value of temp before incrementing x is 3
Value of temp.x after incrementing x is 4
Value of a.x after calling increment() is 4
I have written in context to java , where everything is call by value , but when we pass the actual object the modifications done in the calle function makes it affect to the actual object
- akshaymattoo November 01, 2014In call by value a copy of variable is made and all the computation is done on the copied variable not affecting the original value .like if we have int x=9 , int y=8; and we want to pass this to add function
int add(int u,int v){} .... then the copies of x and y are made into u and v respectively , without affecting the values of x and y .
In contrast in call by reference the actual object is being copied and the computation being done in the method affects the actual object.
like we have a order object .
Order or = new Order();
or.setName("Adobe");
now we have method pass this order object to a method
public int orderSubmission(Order or){} and set the name of order different as Google , Now the original value that was Amazon gets changed .
public class HelloWorld{
public static void main(String []args){
String s1="xyz";
String s2="xkk";
int len =s1.length();
int len1 =s2.length();
int diffLength=Math.abs(len-len1);
if(diffLength>1)
System.out.println("Cannot procced");
else
{
int max = Math.max(len,len1);
int min = Math.min(len,len1);
boolean dflag=true,iflag=true,uflag=true;
if(len-len1>0)
dflag = delete(s1,s2);
if(len-len1<0)
iflag = insert(s1,s2);
if(len-len1==0)
uflag = update(s1,s2);
if(dflag && iflag && uflag)
System.out.println("True");
else
System.out.println("False");
}
}
public static boolean delete(String s1,String s2){
boolean flag=true;
int diff=0;
char c1[]=s1.toCharArray();
char c2[]=s2.toCharArray();
int i =0, j=0;
while(i<c1.length && j<c2.length){
if(c1[i]==c2[j])
{
i++;
j++;
}
else{
diff++;
i++;
}
}
if(diff>=2)
{
flag=false;
// System.out.println("diff 2");
}
return flag;
}
public static boolean insert(String s1,String s2){
boolean flag=true;
int diff=0;
char c1[]=s1.toCharArray();
char c2[]=s2.toCharArray();
int i =0, j=0;
while(i<c1.length && j<c2.length){
if(c1[i]==c2[j])
{
i++;
j++;
}
else{
diff++;
j++;
}
}
if(diff>=2)
{
flag=false;
}
return flag;
}
public static boolean update(String s1,String s2){
boolean flag=true;
int diff=0;
char c1[]=s1.toCharArray();
char c2[]=s2.toCharArray();
int i =0, j=0;
while(i<c1.length && j<c2.length){
if(c1[i]==c2[j])
{
i++;
j++;
}
else{
diff++;
j++;
i++;
}
}
if(diff>=2)
{
flag=false;
}
return flag;
}
}
Perfect that what is expected ...i got what you mean ....
- akshaymattoo November 01, 2014