A get what you're saying, but it seems a bit misleading to say that it's always passes by value. From what I've understood from most languages I've used, I've always assumed that passing a reference/pointer/memory address of an object is what Pass by Reference meant, and that Pass by Value is like passing a whole copy of an entire object itself. The process that you've just shown in this video is what I believe many people would describe as Pass by Reference.
@nelbr3 жыл бұрын
I absolutely agree with this comment. In most languages, pass by reference is when the address of the contents of a variable is passed to the function, which is exactly what was described here. Claiming that in Java this is pass by value just because seems a bit of a stretch to me.
@liviuursulescu29913 жыл бұрын
Pass by reference means that you have the same parameter in the called function and in the caller passed argument. In other words, if you change anything in the called function on that parameter, it will be also reflected in the caller. Here, if you try to change the passed cheese pointer (yes, even in Java, it's still a pointer), you only affect the local copy of the pointer, and not the myCheese variable defined in the main()... The misleading part here is that may times, when you sent a pointer to an object as a parameter, people think that you're sending the object itself, when instead you only send a copy of that pointer, hence cheese = new Cheese(); will affect only the copy, not the original pointer.
@smrtfasizmu61613 жыл бұрын
Pass by values is the only correct wording to describe this. I will show you immediately why. Let's say you have an array and you pass that array to a void method. The void method increases the number of elements of the array. What do you think happens outside of that method? The array length remains the same, because Java is pass by value. Here is the example. In the main method you have: int[] array = {0, 1, 2, 3}; doSomething(array); Where the body and definition of the doSomething method are as following public static void doSomething(int[] array){ array = new int[10]; for(int i=0; i
@nelbr3 жыл бұрын
@@smrtfasizmu6161 I think it is just that Java programmers don't have the same understanding of pass by reference as C programmers do. Of course, even if you pass by reference, if you explicitly tells your function to create a new variable to work on, using the command new (as you did in your example above and also was done in the video), then the changes applied to the new variable will not be captured on the original variable. However, what would have happened if you did not use the new command inside the function?
@smrtfasizmu61613 жыл бұрын
@@nelbr The variable array will be affected outside of the function. However, if I don't use new I can't make the array bigger. Which means that I have to pass an array of the size which is at least as big as it is necessary to the function, because the function can't increase it. Why would I want to increase the length of the array in the method? The reason why this may be useful is if I want to have different outputs of a function, some of them I want to be arrays. In C I would pass a double pointer to the function (a pointer to the array), while in Java I could create a class which have an array as its field and then pass an instance of that class (I can do this in C with struct though). Of course, it is all just data, it is all just series of bytes, you can pack all the outputs of your function in one big array and read it sequentially, but this is less readable solution. The reason why I came to this video today is because I run into this "issue" today of wanting to have a method which has several outputs, some of them arrays.
@poulsander78993 жыл бұрын
All the confusion comes from the Java marketing people demanding that name "pointer" is never used. This made sense at the time since "pointer" at the time implied a raw C-style pointer which you could manipulate. Thinking of Java as "passing by managed pointer" makes much more sense in today's context.
@andreffrosa3 жыл бұрын
Exactly
@hoen35613 жыл бұрын
agreed
@sebastiangudino93772 жыл бұрын
Nice way to explain the idea!
@aleksey85302 жыл бұрын
And also they don't show example about 'pass by value' in the C++! C++ can't change reference variable, and we can't redefine memory for it. We can't make such situation the JAVA speaks. It is just their imagination
@JanBebendorf3 жыл бұрын
By your definition pass by reference doesn't exist in C either. Having a pointer parameter in C will also allocate a copy of that pointer and changing the pointer's address in the function won't change the original pointer variable. A better name for it might be "pass by address" to make clear that we don't talk about variable references but as we don't have something called a reference (like C++ or PHP has) we might aswell call our object references or pointers "reference" and therefore call this pattern pass by reference. It's all about the wording of a particular language. Also the object references in Java internally aren't direct memory addresses but rather a reference token for that object to allow more efficient memory usage and defragmentation but that's up to the implementation of the JVM and the behavior is as if we were copying immutable pointer address around.
@hrgwea3 жыл бұрын
Correct. There's no pass-by-reference in C. Passing a pointer by value is the way to achieve the same effect as pass-by-reference, but it's a mechanism that you have to implement by yourself; the language doesn't handle it for you.
@smrtfasizmu61613 жыл бұрын
Of course it doesn't. I truly understood the difference of the two in C, in C you have to pass the double pointer if you want to change the address of the variable that you are passing to a function. For instance if you have a linkedList that you pass to a void function and you want to change the place where that linked lists starts in the memory (you want to add a new member, you want to change the head of the linked list), then you pass a double pointer to the void function. If you pass a pointer instead of a double pointer, your void function gets a copy of that pointer, in other words, it can't change where the pointer which was passed as an argument points to. That's why if you want to have a void function which adds a new head to the linked list, that void function needs to receive a double pointer as an argument. Of course, if your function doesn't return void and instead it returns whatever struct you used to create linked list, then you don't have to use double pointers.
@smrtfasizmu61613 жыл бұрын
It is not about the wording, you always pass by value, pass by value of a pointer if you are passing a pointer. The difference isn't just semantics, as you yourself have pointed out if a function takes a pointer as an argument, it takes a copy of that pointer. The function can't change the original pointer. Functions in C and Java always receive the copy. And this matters. I have already given you an example in C where it matters, now I will give you an example in Java (where matters even more because you can't use pointers like in C). Let's say you have an array like this int[] array = {}; You can't pass this array to a function which arbitrarily increases the number of elements of that array. Why does this matter? It matters because this means that you can't pass this array to a function with intention of using this array as the output of a function. Let's say you want your function to have several different outputs, some of them being arrays. You can't pass an array to a function which you will use effectively as an output variable, unless you now the size of the array produced in the function in advance.
@smrtfasizmu61613 жыл бұрын
This Java problem that I have talking about is easily solved in C, either by passing a double pointer or by using realloc. Neither of the two exist in Java though. The problem is you want a function to have several outputs, some of them are arrays and you don't know the size of the arrays in advance. I guess in Java you can create a class which has all of these arrays as its fields and then you pass an instance of the class to the function. I mean yes, you can solve this problem in C in this same way as well, you can create a struct which has bunch of pointers as its fields.
@JanBebendorf3 жыл бұрын
@@smrtfasizmu6161 It totally depends on how you define the word "reference" and the scope it is used in. Comparing it to C++ you are totally right but from a Java or C developer's perspective who doesn't have a concept that's similar to C++'s references you might aswell call the concept of passing by mutable objects (or pointers in case of C) "pass by reference". It's all a matter of the scope (:
@mohamedhegazy21822 жыл бұрын
Thank you for reaffirming my understanding! I always thought of passing references to objects in Java as passing a key of a room : in Java we always make a copy of the key as we pass it. That way the recipient of the key will only make changes in the room if the room is mutable, but if they end up messing with their copy of the key, my original key will still successfully access the room with no changes in the room. I think in other languages that have the concept of pointers, we may be sharing the original key with the recipient, who can change it for us to allow us to access another room.
@CodingWithJohn2 жыл бұрын
That's an even better way to explain it then I did!
@TheRiseLP Жыл бұрын
@@CodingWithJohn "Primitive types are passed by value, everything else is passed by reference" There, that is the clearest way to explain it. I really don't understand why you feel the need to go through that kind of mental gymnastics only because you refuse to call it pass by reference.
@zdiscover1 Жыл бұрын
@@TheRiseLP I was stuck thinking why String even if passed as referencem do not change in the calling fn, and then I saw this view and it cleared out the confusion over how strings which are immutable must be handled internally. So basically the "mental gymnastics" worked!
@gokulnath87107 ай бұрын
This is the best explanation of the crux of the issue
@findlestick3 жыл бұрын
Commenting to assist with KZbin algorithm. Thanks for much-needed inspiration with Java.
@justinmeskan44102 жыл бұрын
John you have become my new Online Java mentor, I've been developing javascript for the last 10 years so I'm not totally new to programming so your fast detailed explanations are pure brain candy. please keep them coming. On another note, if you could someday do some springboot videos OMG you'd hit rockstart status!!
@1over1373 жыл бұрын
Consider this: public void static main(String[] args) { Integer myInt = new Integer( 12 ); aMethod( myInt ); System.out.println("myInt: "+myInt); // will print 13! This is NOT pass by value behaviour. } void aMethod( final Integer myInt ) { myInt.increment(); }
@thefab5223 жыл бұрын
Yes, it is pass by value behaviour. You're using the Integer wrapper class, not the int primitive type. myInt is not the value 12 itself, it's a reference to an object that contains the integer value 12.
@1over1373 жыл бұрын
@@thefab522 Exactly. It's a reference to an object. So it's not the object itself... so.... it's pass by reference.
@nunya11203 жыл бұрын
You cover the same stuff as my professor, but in a different way. Hearing it two ways is sooo helpful.
@zghxzwpc3 жыл бұрын
That's clear, thks ! But I think the term "reference" is a bit misleading for C++ developpers like me. Actually, from your description "Cheese" is actually a "pointer", not a "reference". So pointers are passed by value, which totally makes sense. But in C++ that's what we call "passing parameters by pointers", not "by value". In C++, when a parameter is passed "by reference", actually it's totally different. In this case, no copy at all is happening, neither the object itself, neither its adress. If you look at the stack when the function is called, no value is stacked at all for reference parameters when the function is called. So it's really different from Java "references". So my question is : Why not just saying "In Java, parameters are passed by pointers" ? This would be much less confusing...
@WakkaFlocka3 жыл бұрын
"Pointers" may be a term that you understand as a C++ developer, but It's not a term used in Java development; this is why it's called a reference instead.
@zghxzwpc3 жыл бұрын
@@WakkaFlocka : I understand this point. But I think it's a bit sad to use two different terms to express the same thing in two different languages. Or even worse, to use the same term to express two different things in two different languages ! And that's the case here : Java reference == C++ pointer, and C++ reference doesn't exist in Java (as far as I know). Many developpers are using several languages to develop, this kind of ambiguity is confusing and I think it should be avoided as much as possible.
@vadimkholodilo28833 жыл бұрын
I guess, we can't really say that it's passed by pointer, because there are no pointers in Java. Yes, variables hold nothing more than just a pointer, but since Java doesn't have pointers, this term can't be really used. Also, pointers in C/C++ let you work with addresses. For example, you can do something like myPointer++. However, in Java you can't really do it.
@zghxzwpc3 жыл бұрын
@@vadimkholodilo2883 I agree that those "Java pointers" are not working exactly the same as C++ ones. You can't modify the address, you don't need to dereference the pointer to get the value, etc... But the concept is still much closer to "pointers" than to "references". So if we need to choose, "pointers" make much more sense.
@jame-rock3 жыл бұрын
To answer your question: you would not say that because it is not true. Parameters are pass-by-value in Java. In C++, what you call "passing parameters by pointers" is actually a pass-by-value operation. The function parameter is a variable of type "pointer" (which points to the variable you wish to pass in) and a local copy of that pointer is created upon function call. Any changes to this new local pointer will not affect the original. Java References are like a combination of C++ pointers and C++ references, they have some properties of each, so there is no 1:1 comparison between the two languages. In this example, myCheese is just like a C++ pointer to a nameless object in memory. This pointer is passed by value, just like pointers in C++.
@benkimoon3 жыл бұрын
you are THE Java professor everyone is looking for :)
@deconline13203 жыл бұрын
Your explanation describes precisely pass by reference (or address)... Yes you pass the address of the object by value, but this value is a reference to your object. The term pass by value or reference refers to the object itself and not to it's pointer. I have no idea why Java decided to confuse people with this terminology... It doesn't make any sense.
@jarlfenrir3 жыл бұрын
I learned that java passes primitives by value and objects by reference... is this offical statement that java passes always by value?
@thierrydecorte16323 жыл бұрын
@@jarlfenrir The spec doesn't use wording such as pass by value/reference (as far as I know). It defines two types of variables: primitives and references and specify the following: "(8.4.1) When the method or constructor is invoked (§15.12), the values of the actual argument expressions initialize newly created parameter variables, each of the declared type, before execution of the body of the method or constructor." The word "value" here is used... But when the variable is a "reference type" the value passed is a reference to the object. Which makes it a pass by reference. Primitive types are not references which makes them pass by value.
@jame-rock3 жыл бұрын
The term pass by value or reference refers to how function parameters are handled. myCheese is a pointer to a nameless object in memory. The variable myCheese (a pointer) is passed by value, and there is no way to pass in the nameless object, because Java does not allow variables with a true data type of Object (they are all references). Metaphorically, the nameless object gets passed by reference, but what actually happens is the reference object gets passed by value
@jarlfenrir3 жыл бұрын
@@jame-rock Then explain how does this differ from "pass by reference" in C++
@thierrydecorte16323 жыл бұрын
@@jame-rock "Pass by reference" means you are passing a reference to an object. The fact that the reference (address) is copied into another memory location is not relevant since the term apply to the object being referenced and not the reference itself. It is the same in other languages where passing a pointer/reference means pass by reference even though the pointer itself copied. To pass the pointer itself by reference would require a pointer of pointer. As a programmer, if you tell me I'm getting an object by value, I would assume that I'm getting a "copy" of the object and not the same object that was passed by the method caller.
@pejko892 жыл бұрын
I would always make a method like this return an object and store it as a new variable. This is important to understand. You explain in such a way that it's easy to understand
@kraxmalism2 ай бұрын
ffs, this clarifies lots of confusing things in Java. it's been fckng my life for a year now, but none of those tutorials properly explained this fundamental rule. thanks a million
@jagadeeshgurana44902 жыл бұрын
I tried understanding this thru articles for about 2hrs and got nothing, this 5 min video just clarified all my doubts. Thanks a lot, John❣
@ravireddyism2 жыл бұрын
Bas abhi Daal kaakey soojaav bachhe
@v01d_r34l1ty3 жыл бұрын
So let me get this straight: passing an object reference is not "passing by reference"? I understand the pass by value argument, but everything is pass by value regardless since you're can't just magically make something appear out of thin air. There's only 3 ways of passing an argument: by explicit copying of the entire object, by copying it's address (pass by pointer), or by copying it's address and using it like it's in the same context (pass by reference).
@D0Samp3 жыл бұрын
The main point is that Java does not have references *to* references (or references to primitive types without a boxing class). As an example, you can't pass a String *variable* (as the allocated space for storing the reference to a String object) to a method as an "out" parameter and have it filled with another String reference on return, which is what "passing by reference" accomplishes in other high-level programming languages that don't have "pointers" either. References (and primitive types) are always copied, which could be described as "passing by value with a layer of indirection", which doesn't help with the confusion at all. Especially since "reference" in the context of Java is the technical equivalent of a pointer to a specific position in the heap and the actual data can still be modified by everyone who knows where it is, as long as the language permits it.
@v01d_r34l1ty3 жыл бұрын
@@D0Samp yeah that’s confusing. It’s all name scheming, in essence it’s still just doing pass by reference from what I’m gathering.
@smrtfasizmu61613 жыл бұрын
@@v01d_r34l1ty No, it is not. It is pass by value. Here is an example which hopefully illustrates it. Let's say you have an array and you pass that array to a void method. The void method increases the number of elements of the array. What do you think happens outside of that method? The array length remains the same, because Java is pass by value. Here is the example. In the main method you have: int[] array = {0, 1, 2, 3}; doSomething(array); Where the body and definition of the doSomething method are as following public static void doSomething(int[] array){ array = new int[10]; for(int i=0; i
@v01d_r34l1ty2 жыл бұрын
@@smrtfasizmu6161 sorry for the late response, but both C/C++ and Java treat arrays more as pointers. So “=“ will change the address and “[]” will dereference based on an index (product of x and the size of 1 element). Believe arrays are the most common reason to get a NullPtrException in Java, but tbf I haven’t touched either language in 2 months cuz I’ve been at BMT so I’m a little out of the loop right now. Essentially pass by reference is meant for normal variables, say a variable that is technically a pointer being passed by value but being treated as if it’s a pointer actively being dereferenced when utilized within the scope. But officially Java can call the operation whatever it wants technically speaking since they have jurisdiction over the terminology only in regards to their language.
@smrtfasizmu61612 жыл бұрын
@@v01d_r34l1ty I never claimed otherwise. I said that array variables hold the memory address both in C and java, then passing an array by memory address is pass by value, not pass by reference. If when we pass an array, we actually pass the memory address where the array variable is stored, then that would be pass by reference, because we are passing a reference to the array variable (and yes that array variable itself holds a memory address of the start of the array).
@karentechnologies39907 ай бұрын
This video is the best explanation for the Java pass by value/reference topic on KZbin. Subscribed
@rainmanrick3 жыл бұрын
This is misleading... pass by value means you push a copy on the stack before the call. Pass by reference means the code passes the address of the object (of course the address is a 'pass by value') -- I think that is the point you wish to make, but for new devs, they may be confused . There are languages where object types can be defined to be 'value type' and the entire memory foot print of the object is (copied) and pushed on the stack - meaning the function only has a stack copy of the object and not the address (reference) to a mutable object that could have side affects. I think it is simple enough to point out that, in the end, everything is passed by copying value(s) pushed on the stack, but the implications of pass by value vs pass by reference are important -- and this makes it seem like all methods in java ar PBV, and hence get a copy of the object..not a reference to an existing one in the caller's stack. Looks like several others made a similar comment - perhaps an edit is in order? Otherwise, nice work on all the other videos.
@aleksey85302 жыл бұрын
If I would developer of C++ compiler and I would have a task make 'reference', what have did I? I would make wrapper on pointer and prohibit change reference. And such solution doesn't have any problem with a C++ standard. So, do we have such situation - 'pass by reference'? That, the JAVA does mean? I think we don't. They speak about 'pass by reference', but their explanation isn't correct
@bobbobthingymibob3 жыл бұрын
I work with a java card, we always pass the object reference, this is decoded to an address 'when needed', as in the background we can perform defragmentation, ie. Object addresses can move. Primitives by value, objects by reference token.
@hoen35613 жыл бұрын
by reference actually means that it's passing a pointer by value
@aleksey85302 жыл бұрын
@@hoen3561 and where isn't so? For C++ and C. show me example)
@bobbobthingymibob2 жыл бұрын
@@hoen3561 I think the answer from Jan Bebendorf makes it clear java passes a reference token not a pure pointer to allow for defragmentation.
@asherkhan26563 жыл бұрын
Nice short and sweet videos that help reinforce fundamental concepts 👍
@williamskiba67862 жыл бұрын
This explanation is akin to cutting the definition of "literally" out of the dictionary and pasting in the definition of "figuratively." That is, it all depends on your definition of the word "VALUE." In C++, when you execute 'x = 5' the VALUE of x is, by definition, 5. Certainly, that value is stored somewhere in memory, but when most programmers talk about the VALUE of a variable, they do not mean "the address of where it is stored in memory." They mean "the contents at that memory location." Just because Java hides the existence of pointers from the programmer, that does not automagically change the meaning of the word "VALUE." Java programmers may be content with cutting the definition of "value" out of the dictionary and pasting in the definition of "reference" but that does not change the truth of what is actually happening.
@stephenweber333 жыл бұрын
I really love your racecar voice explaining. I remember distinctly the same issue alone years ago working mostly with python. And no one talks about cheese ,, REAL Cheese ,, not memory cheese as fast as you in real life successfully!
@spudhead1692 жыл бұрын
A pointer to an object is a reference to that object. Saying that you're passing the value of that pointer and thus are passing by value is technically correct but a little too specific. Methods are passing object POINTERS by value, but they are also, less specifically passing objects by reference.
@HaveAGreatDayStranger3 жыл бұрын
So... The cheese variable holds a reference to the object in memory. We pass the cheese variable (reference) to the function. Therefore, it's pass by value? It's literally a reference...that we are... passing. What am I missing?
@yahelbraun54713 жыл бұрын
A "real" reference is an alias of the object. Meaning, assume: Cheese myCheese = new Cheese(); Now myCheese in pointing to that object. If we pass myCheese to a function in C++ by a reference, it gives myCheese another name, an alias (not copying the memory address that will make another pointer to the object).
@HaveAGreatDayStranger3 жыл бұрын
@@yahelbraun5471 exactly, which is why i think its frivelous to try and export a C++ concept to other languages. Its meaningless to ask whether some language is pass by value or pass by reference.
@CodingWithJohn3 жыл бұрын
You're right that a lot of the argument for whether something is pass by reference or value is less meaningful than it was for older languages. That said, I know this can be a question asked in settings like entry level Java programming jobs, and it helps to be able to respond with a good understanding of what's going on underneath.
@jame-rock3 жыл бұрын
You are missing that pass-by-reference and pass-by-value are descriptions of how a function/method handles incoming parameters, not a description of what data types you are passing. In pass-by-reference, a local parameter has the same memory address as the external variable that was passed in. In pass-by-value, a local parameter has a different memory address than the external variable that was passed in. When we pass the myCheese variable to the function in the video, the variable "cheese" is stored at a different location in memory than the original variable myCheese.
@smrtfasizmu61613 жыл бұрын
@@jame-rock Yes.
@radiosparrow8513 жыл бұрын
if "pass by the value of the reference" is "pass by value" then what's actually "pass by reference"?
@jame-rock3 жыл бұрын
In a pass-by-reference language, the following two functions do the same thing. Var refers to the same spot in memory as x. int x = 10; void function(int var) { var += 1; } int x = 10; void function2() { x += 1;}
@radiosparrow8513 жыл бұрын
@@jame-rock right, so leaving aside primitive passing, even though in your reply, it's int x, I presume it's meant as an object by context. so for objects: "pass by value" is like you write down an address on a piece of paper for me and I copied the address by noting it down in my own notebook. "pass by reference" is like you write down an address on a piece of paper and give that piece of paper to me. so it actually has nothing to do with the value (the destination at the address), it's about how to handle the reference (the piece of paper where you wrote the address on). which also means, for objects, unless you use the keyword 'new', otherwise it doesn't matter practically. (which probably why you said in the video, it appears like a "pass by reference"), you either take that piece of paper or follow the information in your own notebook, you still end up at the same destination.
@Lucas-sw5js2 жыл бұрын
I see you point and i agree John. Now the question is: is there any language that truly passes the object to the method? I mean, that you can change the original object address from inside? 🤔
@dinushachathuranga76578 ай бұрын
Thanks a lot for the correct and clear explanation❤❤
@limx60653 жыл бұрын
this video did not preface by defining what is pass by reference and by value. i could have said java passes reference of the [object] or passes value which is the [reference of the object] and meant the same thing. in other words, in the parameter position, what the argument was before in the caller is the same as the variable in the callee.
@luckyluckydog1233 жыл бұрын
I agree. IMO the explanation of the mechanism is clear, but the naming and general context don't make sense to me. Specifically: 1. He doesn't define what he means by "pass by reference" and "pass by value", so the whole discussion is moot. To me, "pass by value" means that the caller makes a copy in memory of the data to be passed and gives to the called method the memory address of the copy. In this scenario one cannot modify the original data, only its copy. And to me, "pass by reference" means no copy is made and the memory address of the original data is passed. 2. According to the definitions above (which may or may not be the relevant ones), Java is "pass by reference" for objects (as illustrated in the video), but it is "pass by value" for primitives (int, Integer, String, etc). So if you have something like public static void main(String[] args) { int i = 5; increment(i); System.out.println(" i = " + i); } static void increment(int k) {k++;} then output will be 5, not 6 (the "increment" method acted on its own copy of the data, so it has no effect outside its function scope). He didn't mentioned that primitives behave differently... so I can't see how saying 'it's always by value, because you always pass the value of the memory address' is a useful definition.
@jeanjacquesbarros3 жыл бұрын
Thanks! This concept was very confusing to me. Now it's clearer!
@ukaszr15602 жыл бұрын
What about something like: void main() { String zText = ""; fillString(zText); printf(zText); } void fillString(String zText) { zText += "foo"; } What is the result and why is that ?
@dicousdev259211 ай бұрын
Good question! String in Java is immutable, if we try to concatenate something with String, Java will create a new object as is the case in the code. So the result will always be an Empty String
@gustrbyt3 жыл бұрын
Totally agree with you, but the only thing that I could not figure out is why there is such distinction between pass by value and pass by reference since, even in languages like c there is only passing by value, since the data that you are actually passing is simply the memory address I believe its just a simplification to make the concept easier for the students to wrap their heads around it, what do you think?
@fredoverflow3 жыл бұрын
Other languages actually offer both path by value and pass by reference, e.g. C# and C++. Java does not, and that was a conscious design decision to keep things simple.
@jarlfenrir3 жыл бұрын
In C you have no references, but in C++ passing by reference and value means a huge difference. Passing by reference basically works the way he described in the video how java works, but passing by value makes a copy of whole object.
@gustrbyt3 жыл бұрын
@@jarlfenrir Yes when I said that I meant in like, c, Java and more modern interpreted languages, I especially didnt mention c++ or rust because i knew they handled references in a different way. I said that because when I learnt C in college, my professor took like a day to explain what passage by reference is, when there is no such thing, you até always passing by value even If It is a pointer
@aleksey85302 жыл бұрын
@@jarlfenrir But we don't have a strict rules for reference. I can make pointer wrapper and it will work like reference. And such C++ compiler will work as should using standard. Also we can't change refreneces in C++ because of they always const. So, the java case 'pass by reference' doesn't make any sense. We can't produce 'pass by reference' in C++) I mean value which java make for 'pass by reference'
@Rudxain3 жыл бұрын
Pass by value is what happens when you download an image. Pass by reference is when you buy an NFT that points to the same image
@Sollace2 жыл бұрын
>Java is ALWAYS Pass By Value. Here's Why No, it's pass by reference. And it's also not always pass by reference. Class types are passed by reference whilst primitives are passed by value. You can tell it's passing things by reference because making changes (updating fields) on a class type object causes the same change to reflect on the instance held by the calling method. With pass by value, you always get a copy and change propagations like that are not possible.
@CalvinL.Stevens2 жыл бұрын
If I had this kind of content available at the time, I wouldn't have had to repeat my Programming 2 exam at university 2 times.
@elvispontes41652 жыл бұрын
your tutorials are simply the best... thanks
@brunomonteiro3646 Жыл бұрын
John is right here. I know it sound pedantic, but this behaviour is ACTUALLY different than a "true" pass by reference, where no copies at all are made and the function cannot make the parameter point to something else without affecting the original one.
@renanaoki714 Жыл бұрын
Thanks for the explanation!
@stevenshrii2 жыл бұрын
What is the difference between a object, an instance and a pointer?
@xetbytetechnology14452 жыл бұрын
I have been traveling on 🌍 Earth with Java for 10 years just 5 miles, but after watching your video tutorial approx 5:21 minutes I have traveled with Java 24,887.64 miles which is exactly the circumference of the Earth. Now I am 🦋🦋🦋 flying with Java in ✈🛩🪐. A lot of thanks. I really appreciate.
@suryajit73 жыл бұрын
the clarity you get by watching Johns video (ta daaa)
@anujpotdar35293 жыл бұрын
The value that Java passes each time in the context of call by value is the address of the said object. Have I understood this correctly? Edit: Btw, once again a great video as always 🙃
@TheOfficialJw2 жыл бұрын
This is such a great video!! Thank you!
@flex_prods3 жыл бұрын
Nice video, now i understand a bit more about the new operator
@Bill-gc9bt9 ай бұрын
John speaks about the "values" of memory addresses of objects that Java passes to functions. For any C / C++ developers out there, these "values" are called pointers.
@_danl63272 жыл бұрын
Thanks for clear explanation
@MrDavidjRay2 жыл бұрын
John you are the absolute best thank you!
@jarlfenrir3 жыл бұрын
I was taught that java passes objects only by reference, so I find that explanation a bit confusing. Maybe you should do same stuff in C++ to show when java behaves similarly and when differently? In C++ when you pass by value, your whole object and variable pointing to it are copied. When you pass by reference I believe your variable is copied, but object is not (haven't written in C++ for ages), so... it's the same behaviour as in Java. So why inventing something like "passing by value but only reference, not object" instead of just saying "passing by reference"?
@himanish45413 жыл бұрын
@jarlfenrir that's my point exactly. As far as I understood before this video, objects in java are essentially passed by reference. If what is being said in this video means pass by value, what would pass by reference look like?
@Abstractor213 жыл бұрын
@@himanish4541 this is what we've originally known as pass by reference. He says that its always pass by value because the adress of the object is always pass by value. for me, the name doesnt matter because you can still think this value as a reference to your object
@sxx24912 жыл бұрын
Nice contents, really helped me a lot.
@dllm3tommy741 Жыл бұрын
Thanks for the video
@vigneshparameshwaran49212 жыл бұрын
thanks! great explanation
@ensarthewicked10 ай бұрын
I thought I forgot to change playback speed xd. you got me in the first half sir, not gonna lie
@smorebytes2 жыл бұрын
Weird. I considered what you just explained to be pass by reference, reference being the memory address being passed around...maybe conceptually confused it for a long time.
@omaral-dahrawy1134 Жыл бұрын
Amazing explanation as usual. I have one question though, if I pass a String to a method that adds a symbol '#' to the end of the String, the original String still isn't affected. Strings are non-primitives so according to this video the value of the original String should be changed, just like the levelOfStinkiness changes.
@ppl_call_me_tima2 ай бұрын
cool stuff sleek video!
@libertymedicalcommunicatio49083 жыл бұрын
Liking sharing and commenting (and watching all the way through)
@mohamedtoba83613 жыл бұрын
Thank you very much for your efforts and I hope you make a video about dynamic binding in java
@kauanmocelin3 жыл бұрын
Nice explanation! A "simple" concept that I already confused sometimes.
@sreeshakv54052 жыл бұрын
Great information.. Thank you sir
@Infinitesap3 жыл бұрын
What happens then when the value is not an object? Where is the reference then (pointing to)? I assume that it will point to the address, but how of not through a reference - which a primitive doesn't have.
@techsupport12943 жыл бұрын
You can say that about any language, because a "value of a reference" is a reference. This is much more obvious in a language where you specify passing by value or reference (C++ below): void cpp_example(int &a){ //a is an l-value *reference* int b; a = b; //as you can see, your example doesn't really mean this isn't a reference }
@smrtfasizmu61613 жыл бұрын
You can change the variable a in c++ If you write int b=5; a = b; As the body of the cpp function, you can see the a variable being changed in the main function. This doesn't happen in Java, or C. In those you can't change the variable that you pass to a function, in C++ you can change the content of a variable that you pass thlo a function. C++ can be pass by reference. I can also imagine a language which has pass by reference. Such a language, when it is compiled, passes both the stack memory location (or heap memory location) of its arguments and the content of what's in the stack/heap. When you write in C and you pass a pointer (memory address) to a function, that pointer is also stored in the stack (or in the heap), you are not passing the memory location on the stack/heap where the variable that you pass to a function is. I can make machine code/assembly code which will have functions that are truly pass by reference.
@kirumaqq Жыл бұрын
Great video thanks
@uchennannamani57953 жыл бұрын
Your videos are great....... good job John
@szhhh13262 жыл бұрын
Let's start with this: Some of this debate boils down to usage of natural language. Namely, there are two ways to use the English word “pass”: “pass” + direct object, and “pass by value” + direct object or “pass” + direct object + “by value”. Saying only “pass by reference” or “pass by value” is misleading, because what exactly is being passed is not said. In order to avoid confusion, we should stop omitting the direct object when we use phrases like "pass by value" or "pass by reference". Thoughts?
@krukoski2 жыл бұрын
So, after increaseStinkiness function scope, the cheese memory adress stays the same as it used to be before the function call, doesn’t it? 🧐
@69k_gold2 жыл бұрын
It's actually pretty easy to understand and makes more sense than that of other languages.
@boomsandapples26403 жыл бұрын
This is a great explanation to a topic that can be a little confusing. Thanks!
@pejko892 жыл бұрын
How lucky am I to learn this for free. Thank you so much!
@suvraneelsaha89732 жыл бұрын
Im a beginner in java can anybody tell me what is cheese object Great video totally understood . you are creating a new variable in the function / method not accessing or modifying the actually passed var
@arekxv3 жыл бұрын
This video is not ok without a disclaimer for people starting out in Java. Disclaimer: People, you should NOT learn this until you are on a advanced Java level to understand this better. To people learning Java, skip this video, it is not for you yet. More info: Yes, this is technically true but the REASON why we say pass by reference is so that people can understand that things PASSED into a function by reference CAN be changed. People are going to learn this wrong and it is going to bite them later in their learning process.
@AhmadAhmadi-wb9ny Жыл бұрын
why do we use by pass value?
@nitfitnit3 жыл бұрын
Very clear, great video!
@Johnny-tz2dx3 жыл бұрын
love your videos!!!
@smaug98333 жыл бұрын
You don't have to worry about pass by value or pass by reference doesn't matter at all as long as you make your function always return something and use that for further implementation. Which I do, I don't care if it's best practice or not.
@isotoxin3 жыл бұрын
I'm thinking about this as a "passes the value of an whole object".
@dotanon2 жыл бұрын
While I fully understand the underlying technical reasons that make people always stress that Java is ALWAYS pass by value, and this isn't aimed at you (your explanation was very good and easy to digest), but man, it is INCREDIBLY misleading for a beginner to try to look into this. Hearing that it's always pass by value to me intuitively makes me think I am manipulating a copy of an object and will need to return a value and store it in a variable in order for a method to actually affect objects in its argument. And to then add that references are passed by value feels like tiptoeing around what people are actually wanting to know, which is the practical considerations necessary to write code. I spent some time yesterday reading responses to similar questions on Stack Overflow and it just made me extra bloody confused, because in the middle of decent answers there are people who simply say "That's incorrect, Java is always pass by value" with no further explanation, which just makes any beginner reading it confused and begin to doubt themselves. To me it seems intuitive to call what is happening in Java "passing by reference". Because an address is in essence a reference to some object. Anyway, this video (and the lively debates in the comments) has given me a much better understanding at least, so thank you :)
@sacredfroakie16593 жыл бұрын
I swear you have literally incredible videos bro. I know Iv said it on multiples vids but ima say it again for that youtube algorithmn haha
@amirbabazadeh304 Жыл бұрын
hi John. i tries it with int type but i got two different values in main method and another method. would you please explain it? is it for being immutable?
@gauravkohirkar3 жыл бұрын
Thanks KZbin for pointing my reference to this video..
@john_salt3 жыл бұрын
Im kinda curious, how else would you be able to pass a reference, other than by its value? Is there any difference between reference itself and its hard copy, since they both resolve to same memory address and therefore are indistinguishable, and therefore hold the same value? (other than the fact that the reference in java cannot be overridden, but how does that relate to value? This is more of a question of mutability of that value) So, does it make any sense to call this "pass by value"? If so, then are all languages technically "pass by value"?
@smrtfasizmu61613 жыл бұрын
C++ can be pass by reference. I can also imagine a language which has pass by reference. Such a language, when it is compiled, passes both the stack memory location (or heap memory location) of its arguments and the content of what's in the stack/heap. When you write in C and you pass a pointer (memory address) to a function, that pointer is also stored in the stack (or in the heap), you are not passing the memory location on the stack/heap where the variable that you pass to a function is. I can make machine code which will have functions that are truly pass by reference.
@Aaron319092 жыл бұрын
How is this different from pass by reference?
@TimLondonGuitarist2 жыл бұрын
Maybe someone can provide a succinct description in this form: Pass a pointer in C++ & that address can be changed: change the address & current value will be lost in the caller. Pass a reference in C++ or Java: the address can't be changed, the value can be read or reset to be read in the caller. If that method assigns a new reference to the variable it will not affect the reference in the caller. If a primitive is passed (in C++ or Java) the value can be changed but the caller will not read that, definitely pass by value.
@mehnaazmohiuddin Жыл бұрын
Ya same pass by value is not a good term at all. After 10-12 years of knowing java . a pop up quiz just of pass by value / pass of reference tricked me into going delulu over my career . But your video bought back my peace
@christopherfrog23713 жыл бұрын
I really envy your abillity to share knowledge so it's all so simple to understand :) Great job, keep it up!
@ChristopherOrlowski Жыл бұрын
this may be an old video, but does anyone have useful resources regarding how coding interacts with the memory and memory allocation as John showed us on his whiteboard? this is one thing I have always had trouble understanding, and any resources that are concise like this that explain the basics with regards to memory allocation and how coding actually interacts with memory would be greatly appreciated
@shanewalsch10 ай бұрын
You probably should just learn c++😂
@codercoder72702 жыл бұрын
can you explain differences between instance and object? with examples
@davidkeimer54743 жыл бұрын
Good Video, but you should additionally have explained what pass by reference would mean. One could say, that the memory address of an object actually is nothing else than the reference to that object.
@florianfanderl66743 жыл бұрын
Isn't everything always in any programming language pass by value? Even if you do call by reference, the memory address aka pointer of the object in question, is passed to the function as a value.
@Badz_B34chst4r3 жыл бұрын
I think when people use "pass by value" what they mean is "make a copy of the argument, pass the function a reference to the copy, and have the function use the copy without ever having knowledge of/access to the original object in the caller scope".
@florianfanderl66743 жыл бұрын
@@Badz_B34chst4r well that's what happens to the pointer in C. Also in Java, if you assign a new object to the argument within the function, for example because you want to use a default object, , the reference that you passed into the function will also not be touched. For me call by reference is built on top of call by value. They are not on the same level of abstraction, although I understand what this should tell you in theory.
@barneylaurance18653 жыл бұрын
No. My main language is PHP. That mostly passes objects references and primitives by value just like Java. But if you add an ampersand to the parameter in the method definition in PHP then it becomes pass by reference. In that case you can assign the variable to a completely new object, or an new primitive value, and when control returns to the caller the variable there will have been modified.
@florianfanderl66743 жыл бұрын
@@barneylaurance1865 yeah but then the value of the reference is passed by value 😁 I know it's a little bit nitpicky 😁
@aleksey85302 жыл бұрын
@@barneylaurance1865 I don't see any difference with java. It works the same way)
@zekumoru3 жыл бұрын
So basically Java objects are comparable to pointer variables in C, right?
@sivaram34823 жыл бұрын
I addicted to your videos John
@CodingWithJohn3 жыл бұрын
I better make more then! Don't want anyone going through withdrawals
@SanchitSnehashish3 жыл бұрын
Great lesson, professor! I just have one question. Is this the same for data types and their wrapper classes? "int" stores an integer value in memory but an object of Integer class stores the address of memory where the integer value is stored?
@andreffrosa3 жыл бұрын
int stores an integer in the stack (i.e., the variable itself) and Integer stores an reference/address in the stack to a portion of memory in the heap that stores an integer. Its like an "indirect" int
@jackofnotrades153 жыл бұрын
Hi, please do a video on java nio
@ivanchl Жыл бұрын
Why does the null have no effect on the referenced array in the memory since it is pointing to the same location in memory? public static void main(String[] args) { int[] array = {1,2,3}; changeArray(array); System.out.println(Arrays.toString(array)); clearArray(array); System.out.println(Arrays.toString(array)); } private static void changeArray(int[]array){ array[1] = 200; } private static void clearArray(int[]array){ array=null; } } Console output: [1, 200, 3] [1, 200, 3]
@sebastianlenzlinger92912 жыл бұрын
I‘m a bit confused. I get how you say what java calls pass by reference is actually also passing a value. But I don‘t get what else pass by reference would be? Can someone help me out? I feel there‘s a good subtle lesson to be learned.
@aleksey85302 жыл бұрын
@@williams322 So, we have that behavior in all languages. But only JAVA call it 'pass by value'. More correct is 'pass by sharing'. Also the example of 'pass by reference' from JAVA doesn't exist in the nature)
@dimitrisgangas28973 жыл бұрын
Excellent explanation!
@MrCoolTester7 ай бұрын
Flashbang warning at 1:34 !!
@wassimtahraoui98882 жыл бұрын
does java do the same process for primitive data types?
@FlexGC3 жыл бұрын
So what's the difference then? Isn't it just semantics?
@vishalsundararajan12282 жыл бұрын
Use hibernate and the problem becomes double with the level 1 session. setting the chest stinkiness to 756 will fire an update query if the cheese reference is passed
@marcello42583 жыл бұрын
And don't forget.. strings are also objects hence Holding the address