C K Sanjay Babu
Posted on May 7, 2022
Again Java passes variables in functions similar to js. Check out the detailed explanations here.
Day-6 - Is javascript pass by value or pass by reference?
C K Sanjay Babu ・ May 6 ・ 1 min read
#javascript
#beginners
#programming
Pass by Value
All primitive data types are passed by value in java.
class passByValue {
public static void main(String args[]) {
Integer i=0;
increment(i);
System.out.println("Value of i outside the function: "+i);
}
static void increment(Integer i){
i++;
System.out.println("Value of i inside the function: "+i);
}
}
/*
Output :
Value of i inside the function: 1
Value of i outside the function: 0
*/
Pass by Reference
Objects & arrays are pass by reference in java
class PassByValue {
public static void main(String[] args) {
Integer[] array = new Integer[2];
array[0]=2;
array[1]=3;
add(array);
System.out.println("Result from main: " +(array[0]+ array[1]));
}
private static void add(Integer[] array){
array[0] = 10;
System.out.println("Result from method: " +(array[0]+ array[1]));
}
}
/*
Output:
Result from method: 13
Result from main: 13
*/
💖 💪 🙅 🚩
C K Sanjay Babu
Posted on May 7, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
webdev A Tale of WeakMap and WeakSet in JavaScript: The Guardians of Forgotten Secrets
November 29, 2024