
Sir, What is the difference between pass by value and pass by reference. can u give an example?

Pass by value calls a function or returns a value based on the number or "value".While Pass by reference calls a function and passes a pointer to a memory location that contains the value.
Pass By Value: It refers to pass the variables or a constant that holds the primitive data type to a method.
Example:
public class PassByValue{
public static void display(int a){
a=5;
System.out.println(a);
}
public static void main(String[]args){
int i=20;
System.out.println(i);
display(i);
}
}
Pass By Reference: It refers to pass an object variable to a method.
Example:
public class PassByRefernce{
public static void display(StringBuffer sb){
sb=sb.insert(8,"to");
System.out.println(sb);
}
public static void main(String[]args){
StringBuffer sb1=new StringBuffer("Welcome RoseIndia");
System.out.println(sb1);
display(sb1);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.