Java is pass/call by value or pass/call by reference [MOST IMP. JAVA INTERVIEW QUESTION]| ode Decode
Ғылым және технология
In this video of code decode we will figure out if Java is pass/call by value or pass/call by reference.
This is very important and most controversial topic in Java
Udemy Course of Code Decode on Microservice k8s AWS CICD link:
openinapp.co/udemycourse
Course Description Video :
yt.openinapp.co/dmjvd
Any programming language can be either pass by value or pass by reference. Now the most frequently asked interview question is what is java ? A pass by value or pass by reference?
In this video we will try to figure this out.
In this video of is java pass by value or pass by reference we will try some demo examples to prove why is java pass by value though it seems to be pass by reference.
Answer to why is java pass by value or pass by reference
java pass by reference or pass by value,pass by value and pass by reference in java,pass by value and pass by reference in java by durga sir,is java pass by value or pass by reference,is java call by value or call by reference,java call by value and call by reference,is java ca by value or call by refrrence
---------------------------------------------------------------------------
#javapassbyvalueorpassbyreference #java #passbyvalueorreference
Пікірлер: 179
Thank you ma'am, I have been asked this question in interview and I was unable to answer properly, now I am confident on this topic.
@CodeDecode
3 жыл бұрын
M glad that you are. :) May you reach all the heights in your career
@nndsmsjsjs2501
Жыл бұрын
@@CodeDecode mam pass By Value Means Call by value Right?
@Neerajkushwaha-uz8wo
5 ай бұрын
Us😄
Pass By Reference: Changing the value of the real address in the memory. Pass By Value: Copying the value to another location and so the real value is not changed.(java)
@CodeDecode
Жыл бұрын
👍👍
@amitjoshi940
25 күн бұрын
Java 8 : pass by value or pass by reference
Last wala example related to refreneces, was really osm
Thankyou mam.. for explaning this in a wonderful way
Clear explanation. I hope that'll help not to forget it again.
@CodeDecode
3 жыл бұрын
Thanks 👍. Yes hopefully 😊
Real time example is very easy to understand, thank you...
@CodeDecode
3 жыл бұрын
Thanks Dheepu 👍
This is very helpful video. Thanks a lot for explaining this👍
@CodeDecode
3 жыл бұрын
Thanks Amogh for the nice words....
Thank you so much Ma'm to clearing my Concept... & Congratulations for a new subscriber 😉
@CodeDecode
3 жыл бұрын
Thanks Waqas 👍👍
Thanks mam, please continue to make videos like these to help people. Maybe after java sometother topic like database
@CodeDecode
3 жыл бұрын
Yes, definitely
Good explanation. Thank You. You can also pass any list to a method and update the list in called method to provide more in-depth analysis.
@CodeDecode
3 жыл бұрын
Sure 👍
Simple and clear explanation. Aweesome!!
@CodeDecode
3 жыл бұрын
Thanks 👍
Great explanation with nice example 👍🏻
@CodeDecode
3 жыл бұрын
Thanks 👍
Good to see this channel.
@CodeDecode
3 жыл бұрын
M glad you liked it
The topics you cover here are so unique !!! I have seen many tutorials for Java..... But the type of topics you cover here and the explanation is top notch!!! ❤ Thank you for your efforts 🎉❤
@CodeDecode
Жыл бұрын
Thanks Abhi 😊👍
Thankyou miss👍
Good content. Keep growing. Keep it up.
@CodeDecode
3 жыл бұрын
Thanks 👍
Thanks for this crystal Clear Explanantion .
@CodeDecode
Жыл бұрын
👍🙂
what a brilliant way of explaining , finally got the answer.
@CodeDecode
3 жыл бұрын
Thanks 🙃
Commenting for the 3rd time....for comparable comparator detailed video...
@CodeDecode
4 жыл бұрын
. Sorry for keeping your topic on hold. I needed to cover basics first for that video. Now since we have covered most of the basics we can take that topic now.
@CodeDecode
4 жыл бұрын
@navneet All you need to know about comparable and comparator Please like and subscribe. kzread.info/head/PLyHJZXNdCXscG1g50RaLZdXgBhC-C7dCy
Best explanation. Love it.
@CodeDecode
3 жыл бұрын
Thanks 👍
You did an excellent job explaining, thank you so much!! =)
@CodeDecode
3 жыл бұрын
You are Welcome ....
God bless you mam for your good work…your work really helps people….keep up!!!
@CodeDecode
2 жыл бұрын
Thanks a lot Tapan 🙂👍
seen many videos from your channel, everything is on point explanation and also the slides are perfect to read even though there is no audio. where can we find all your presentation slides?
@CodeDecode
2 жыл бұрын
Thanks a ton Nivas 🙂👍
Very clear explanation mam!
@CodeDecode
4 жыл бұрын
Thanks :)
Best explanation by far
@CodeDecode
2 жыл бұрын
Thanks Gargi
Awesome explanation
@CodeDecode
3 жыл бұрын
Thanks 👍👍
Thank you so much mam. Much clear till you convinced that java is pass by value but when you explained with java object then it becomes confusing. Please explain in detail for when we are changing the value using object. Also, Congratulations to you for new subscriber as well. Gone thru channel and found it very helpful.
@CodeDecode
2 ай бұрын
Sure we will create a more clarity video on this. Thanks 👍
Best explanation !
@CodeDecode
2 жыл бұрын
Thanks sanjay
Excellent
@CodeDecode
Жыл бұрын
thanks
Best explanation ❤️
@CodeDecode
2 жыл бұрын
Thanks
THIS IS GR8!!!
@CodeDecode
3 жыл бұрын
Thanks 👍👍
Great explanation 👌🙏
@CodeDecode
5 ай бұрын
Thanks
gud job
@CodeDecode
4 жыл бұрын
Thanks :)
Nice explanation!
@CodeDecode
2 жыл бұрын
Thanks abhishek
Nice explanation
@CodeDecode
Жыл бұрын
Thanks 😊👍
thanks!!!
@CodeDecode
Ай бұрын
You're welcome!
Nice Video
@CodeDecode
10 ай бұрын
Thanks
It is perfect explanation. But still, can you please add new video on this with detail explanation and example.
@CodeDecode
3 жыл бұрын
Sure Drashti, what do u want us to cover in that?
@drashtinaik5751
3 жыл бұрын
@@CodeDecode In detail with heap and stack memory allocation that will give much clarity .
@drashtinaik5751
3 жыл бұрын
@@CodeDecode And thank you for fastest reply.
@CodeDecode
3 жыл бұрын
Sure. You need Java memory management. Right?
@CodeDecode
3 жыл бұрын
No worries 😊😊
With the Last example all the doubts are cleared that Java is pass by value. "Just passing a reference as value. " 8:12 - 8:16
@CodeDecode
3 жыл бұрын
👍👍m glad you understood this so clearly 👍 appreciable
Good Video
@CodeDecode
10 ай бұрын
Thanks
Ma'am I am having a doubt, starting 4 mins of the video where you said "to access a private method you have to make it static", I didn't get that point. Please explain.
vera level
@CodeDecode
2 жыл бұрын
Thanks
Super 🌟
@CodeDecode
2 жыл бұрын
Thanks hari
Thanks 👍
@CodeDecode
2 жыл бұрын
👍
nice explanation however had a doubt, since java is pass by reference for object as per your example for Animal Object. i tried the same example with Integer since its an Object, the same was not happening. Please check the below example. public static void main(String[] args) { Integer i = 10; change(i); System.out.println("main"+i);//op is 10 } public static void change(Integer i) { i =11; System.out.println("method"+i);//op is 11 }
@thivagarmurugan2955
Жыл бұрын
u creating primitive datatypes in main function and u just passing copy of i in change method so any changes made in change only affected in change alone not in main method the both different
@niteshbhati6496
9 ай бұрын
It is because all primitive wrapper classes (Integer, Byte, Long, Float, Double, Character, Boolean, and Short) are immutable in Java
Its still not clear to me- Example 1- If I just add tempList in my base case then I get [] [] [] [] as and, as it holds the reference and it gets modified in the recursive calls, so at each step when it hits the base case I need to capture the then state of the arraylist and store it in the res. private static void backtrack(List list, List tempList, int[] nums, int remain, int start) { if (start >= nums.length && remain != 0) { return; } else if (remain == 0) { list.add(new ArrayList(tempList));--> if I do not make a new list out of it , it gets modified. REFERENCE VAIRABLE THATS WHY!! } else { for (int i = start; i if (nums[i] > remain) { break; } if ((i > start && nums[i] == nums[i - 1])) { continue; // skip duplicates } tempList.add(nums[i]); backtrack(list, tempList, nums, remain - nums[i], i + 1); tempList.remove(tempList.size() - 1); } } } But in this example, even if I set the prev pointer when my left recurison ends for the first time ,still its value is null when it backtracks- private void convertBtToDll(Node temp, Node prev) { if (temp == null) { return; } convertBtToDll(temp.left, prev); // marks the head -- leftmost node if (prev == null) { head = temp; } // make the links for the other nodes else { temp.left = prev; prev.right = temp; } prev = temp; ----> even if I set it here it is null?????? why? isn't it a reference variable? convertBtToDll(temp.right, prev); } public static void main(String[] args) { ConvertBTtoDLL2 btToDll = new ConvertBTtoDLL2(); Node root = new Node(10); root = new Node(10); root.left = new Node(12); root.right = new Node(15); root.left.left = new Node(25); root.left.right = new Node(30); root.right.left = new Node(36); System.out.println("After conversion to DLL"); Node temp = root; Node prev = null; btToDll.convertBtToDll(temp, prev); btToDll.printDll(); }
you rock
@CodeDecode
Жыл бұрын
:)
So can we say, java is pass by reference for non-premitive data types?
Thank you mam I had understand very clearly but one doubt raised in mind that why java is particularly called pass by value here we are doing both pass by value and pass by reference can you please explain mam?
@vigneshgvs
7 ай бұрын
chatgpt gave below sample on justification: In Java, you always pass the value of the reference to an object when you pass it to a method. This means you are passing a copy of the reference, not the actual object. Java itself does not provide a way to pass the actual object by reference as some other languages might. Even when you pass an object as a method parameter, you're still passing a copy of the reference to that object. (kindda pass by value). However, the confusion often arises because operations on the object itself (like modifying its fields) are visible outside the method, giving the appearance that the object is being passed by reference. class Person { String name; Person(String name) { this.name = name; } } public class Main { public static void modifyReference(Person person) { person = new Person("John"); // This reassigns the reference locally, not affecting the original reference. } public static void modifyObjectFields(Person person) { person.name = "Jane"; // This modifies the actual object, affecting the original object. } public static void main(String[] args) { Person person = new Person("Alice"); modifyReference(person); System.out.println(person.name); // Output: Alice modifyObjectFields(person); System.out.println(person.name); // Output: Jane } }
java is strictly pass-by-value method 'arguments' "value" initializes and assigns newly created method 'parameter'value
@CodeDecode
3 жыл бұрын
That's what we have discussed here also right?
@gokulaher2476
3 жыл бұрын
Perfect... Your video on java 8 Predicate,function, supplier,consumer Bi.... Predicate, fu,ction,consumer and stream are very well explained..
tq
@CodeDecode
3 жыл бұрын
👍
So we can say reference is passed as value so reference doesn't change but object value gets change in case of passing reference?
Thank You for making this video. Need small clarification, when primitive variable 'a' passed as an argument to a method you said java copies the value of 'a' to a new memory location and that location address passed to that method as argument so value wouldn't update in parent class ? Same theory isn't applicable to objects ? Java doesn't copy the object to different memory location and sends that memory location address to method as an argument ?
@abduldaniq5004
2 жыл бұрын
Exactly I got that same doubt
@nrusinghacharya
2 жыл бұрын
The problem you're facing here is statements like In Java, Objects are passed by reference, and primitives are passed by value. This is half incorrect. Everyone can easily agree that primitives are passed by value; there's no such thing in Java as a pointer/reference to a primitive. However, Objects are not passed by reference. A correct statement would be Object references are passed by value.
@shiva3895
2 жыл бұрын
@code_decode small question. If java is pass by value & values is passed after copying it. That means object reference value should also be copied first and than passed. If Yes, a memory address is being copied and that is my missconcecpt. Can you please explain. Thanks!
still a bit confusing about pass the object as a parameter
Today this question asked by interviwer...I got trapped... there is mutable/immutable thing also matter to this pass by value or reference. Please clear this also.
Mam im getting really confused with this concept? Please help me to understand
You missed to mention difference between int and animal ...
Need small clarification, when primitive variable 'a' passed as an argument to a method you said java copies the value of 'a' to a new memory location and that location address passed to that method as argument so value wouldn't update in parent class ? Same theory isn't applicable to objects ? Java doesn't copy the object to different memory location and sends that memory location address to method as an argument ?
@vaniraj8456
Жыл бұрын
No here because we send the reference variable which holds the memory address of an object rather than holding the object
Pl make it more clear.
call by rfrnce vs pass by rfrence same or dfrnt
My exam begins in 2 hours 🙏
@CodeDecode
3 жыл бұрын
All the best 👍💯
how to write junit ? myovject oldermethod() { other call// hashmap map = new hashmap(): NewObj obj = service.myservice(map); //logic value = map.get(0); // here i am getting null pointer in junit test return myobject; } class myservice{ NewObj myservice(hashmap map) { //logic map.put(key,value): return Newobj; } } after that i am getting null if i mock the myservice ...how to write junit for oldermethod()
Did u said that u a passing object by value at the end ? When u actually passing the reference!
Got it that , the reference we pass in java function is as a reference to memory location as value, so though we pass a reference it is still Pass by Value, but then what is Pass by Reference exactly
@CodeDecode
4 жыл бұрын
Your understanding is correct. Pass by reference is just a concept where you pass reference of variable or so called memory address of that variable itself as the formal argument. Doing so, if you change any thing locally in the method, the changes are no more local to that method, it's reflected every where Why so? Because you changed at the memory itself. Right? Now you got it ? If it would have been pass by value, different memory location is created for the formal argument and value will be copied. Hence if you change that value locally, the changes will be in newly created memory location and not in the original one
@deneshbabu5199
3 жыл бұрын
Then what exactly pass by reference?
@abhishekganguly165
3 жыл бұрын
@@deneshbabu5199 same question bhai.
In pass by value u mean the value doesn't change and in pass by reference value changed so ideally value changes when reference passed so it should be pass by reference?? Do let me know as I m still confused about this so
@CodeDecode
3 жыл бұрын
Ok so let's understand it in this way. Reference is nothing but memory address. Now when ever you change any thing at memory address, the change is reflected every where. Now in case of Java, This memory address is sent as values in arguments to methods. So it's pass by value, but since memory address or reference is passed as values hence changes are reflected every where
@CodeDecode
3 жыл бұрын
Now do you get the point?
@yoshitamahajan353
3 жыл бұрын
@@CodeDecode and same in case of pass by value we create copy of the variable and pass the copy.. so again we pass value.. 1. In call by value , we create copy of the value and pass to the method 2. In call by reference,we pass the value of memory address to which the object is passed So in both cases value is passed hence java is call by value Am I r8? Please correct me if I m wrong..
@abhishekganguly165
3 жыл бұрын
@@yoshitamahajan353 but both point 1 and 2 got satisfied right? so why this is only pass by value?? why it is not pass by refrerance?
Consider this snippet: public static void main(String[] args) { Integer i = new Integer(3); modify(i); System.out.println(i); //Prints 3 } static void modify(Integer i) { i = i * 10; } In this case I have created the object of the Integer class and changed it's value. So as per this video, object can be changed as it is passed as a reference, I am getting the output as the same object it has been initialized with instead of modified version. In case of arrays, we can do modify the values of array. So I just want to know what kind of values are passed by value and what kind of values are passed by reference?? Anyone please help.
@arpit2283
3 ай бұрын
Bro, String and Wrapper classes like Integer , Double , BigDecimal are immutable classes. So , the value is not changed there.
Hum value bhi pass kr rhe hai aur object reference bhi pass kr pa rahe hai to java sirf passed by value kyu 🤔?
02:51
Hi mam, In 1:18- 1:21 aapne kaha ki "we are changing the value at the real address in the memory" this is pass by referance. and that is what is happening in your last example. when you passes a value the it is changing its value. If you are passing a referance, then the address of the object is getting copied, and it is changing the value inside that object. So can you Please expain me in details for call by referance with a heap and stack memory representation. if possible.
@abhishekganguly165
3 жыл бұрын
I got it first in google search that: Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the argument by using its reference passed in. The following example shows how arguments are passed by reference. So why cant we say java supports both. ?
@sriharikaranam3330
3 жыл бұрын
Java mainly dealing with 2 types.. 1. Primitive 2. Object In first case java is passing primitive value as argument of method. So any changes happened in method then new primitive will be created(which is short lived variable). It wont affect original primitive. In second case, lets say reference is 101011. Then java is passing that reference as argument of method. If any changes happened, reference value not get affected. Java will modify the object which is pointing to that reference. The reference value will be maintained as same. So Java is pass by value... In first case if original value changed, or in second case if the reference got changed, then it will be pass by reference..
@abhishekganguly165
3 жыл бұрын
@@sriharikaranam3330 what you told I got. but point is what is pass by reference then? we will pass the referance and address will be change? or pass by reference is we pass the referance and the value inside the referance got change?
The video views is equal to your subscriber count
@CodeDecode
2 жыл бұрын
Nice observation Pradeep🙂👍
@hey_babe1557
2 жыл бұрын
@@CodeDecode 😁
Ma'am is your name Sana?
Yesterday I had an interview with Nokia , it was an on-campus placement interview.. You know what, they asked me why java is passed by value ? And they wouldn't satisfied with my answer. And hence I got rejected ☹️☹️.
@CodeDecode
3 жыл бұрын
Was I able to clear your query? Did you watch this video before or you watched this now?
@NareshKumar-dw9xp
3 жыл бұрын
@@CodeDecode That's the unfortunate mam. I watched it now. But noww I am clear amd confident. Probably next week I have an interview of TCS Digital SDE . I will never repeat this mistake ever. Thank you so much for this.
@CodeDecode
3 жыл бұрын
Don't worry. As a fresher you will get ample of opportunities and each interview will teach you something new.
@CodeDecode
3 жыл бұрын
Take it positively. Just make sure that you are clear with your basics. They will not go much high level. Prepare core Java thoroughly and I am sure you will crack something really worth cracking 👍
@CodeDecode
3 жыл бұрын
All the best for the future companies. 👍
Why java is not both i.e. java is pass by value as well as pass by reference.??plzz ans
@CodeDecode
4 жыл бұрын
Because Java is pass by value. That's why it's not both.
@gokudominic3328
4 жыл бұрын
No mam i mean java is always pass by value but by vedio java is seems to be both so how can you say tha pass by reference is equivalent to pass by value??
@gokudominic3328
4 жыл бұрын
My actual question is how pass by reference is equivalent to pass by value??
@CodeDecode
4 жыл бұрын
Consider a scenerio : You are passing address or memory location itself.
@CodeDecode
4 жыл бұрын
So while you are passing your variable as formal argument of the method, it is considered as pass by value. So yes you are correct when you say it's pass by value but actually in java, you pass memory reference itself as a value.
JAVA IS ALWAYS PASS BY VALUE ALWAYS
@CodeDecode
2 жыл бұрын
Can you please explain how?
@lazylizarts
2 жыл бұрын
@@CodeDecode its appropriate to say "Object reference is passed by value" for Non primitive type
I m still confused ⁉️
@CodeDecode
3 жыл бұрын
java is pass by value. But if you pass a reference variable as an argument and that changes then it is because you are passing memory reference as value. Hence it's never pass by reference. It's always pass by value.
@CodeDecode
3 жыл бұрын
Still confused??
@littlethings2250
3 жыл бұрын
@@CodeDecode : passing memory reference as a value? But here we are not working on copy, instead the actual object itself isn't it? And in C, the concept of pointers and reference, there we pass reference as a reference or value?
Not understood please give more detail easy way of understanding
@CodeDecode
4 жыл бұрын
Explaination : java is pass by value. But if you pass a reference variable as an argument and that changes then it is because you are passing memory reference as value. Hence it's never pass by reference. It's always pass by value. Still if it's not clear then please let me know I will put more examples for you.
@achyuth2424
4 жыл бұрын
@@CodeDecode more examples please
@narasimhakaranth9824
3 жыл бұрын
@@CodeDecode what that "value" is called? thas not memory address right?
I am unable to understand madam
Slow slow madam , not all members are quick learners as like you
@CodeDecode
3 жыл бұрын
Will try to reduce pace from next videos 👍
@GameXanime63
3 жыл бұрын
U can do by urself if u don't know the playback option just click on the three dot and u can this playback option where u can reduce the speed of the vdo btw thank me later
You got to activate windows.
@CodeDecode
4 жыл бұрын
Yeah will do that for sure...
This is too much of a hindi accent to take in..
@CodeDecode
3 жыл бұрын
We are so sorry Sudip that our mother tongue is hindi. 👍