This is misleading. The process described in the video you linked is the same as that described by Daniel. Furthermore, saying that Java is always pass by value is semantic hairsplitting. A variable holds a reference to an object. Copying that reference when passing it into a function is what passing by reference _means._ Calling it "pass by value" just because the reference is a value, and that value is copied is misleading and confusing because it conflates copying a primitive value with copying a reference value, which end up having _very_ different consequences for how the program runs.
APaleDot , think it like this. when you have a String variable assign to some literal and if you update/changes the same variable then the variable will point to a new address because String objects are immutable and the previous literal will still be in the heap with no reference variable to it; later that will be removed by garbage collector. As we all know pass-by-value means we pass a copy of the data and the original data can’t be affected in the called fuction and pass-by-reference- means we pass the actual address to that data which can be affected in the called method. Now when we say java is totally pass by value what does it mean? The correct sentence would be “java pass reference by value”. To keep it simple whenever you hear the term pass-by-value, remember a copy of the data is created and passed. int a=5; //address 100 Integer b= 5; // address 15 if i pass ‘a’ to a function a copy will be created of the content and passed and if I passed ‘b’ a copy of reference will be passed (Remember, pass-by-value means passing a copy) to the function. Here is the trick if you change the content for ‘b’ inside the function, the original will ‘b’ will be unaffected. If java was really pass-by-reference then you can update the content of ‘b’ inside the function. And not to forget what I mentioned earlier regarding string objects that they are immutable and changing the variable content essentially means changing the reference of that variable. In Java all wrapper classes are immutable and that is why you will not b able to change the content of ‘b’(changes the reference). Any data Pass-by-value to a function will remain unaffected no matter what you do with it inside the function. So java is not pass-by-reference for objects and pass-by-value for primitives. It’s only pass-by-value .
@@TheCodingTrain .. you shouldn't clarify things to people, until it's clear for you. Confusing people could make them lose interest in what you teach.
Man, you explained something , that im struggling to get for weeks, just in 7 mins, and i got it completely. Thank you very much u have a talent to teach!
finally, this made sense, not the part where you were overlapping circles but the first part was perfect, I finally understand this concept. Thank you!
Thanks for brilliant explanation! But there is something I can't understand. 6:30 if fill(col); is under ellipse(x,y,r*2,r*2); the particle that changes color will be p1 rather than p2. I can't figure out why..
Very nice tutorial, thank you. The tutorial hits the core of object-oriented programming. It would even better if there is a outline/summary at the beginning/end.
if we're talking about using 'pass by value' VS 'pass by reference' in a method, here's a TIP you can understand. "pass by reference uses ampersand -> '&' " . If you see that symbol like " public String changeColor(String &favColor ) ", immediately that's 'pass by reference' . Everything else is pass by value. How about that? : )
C# (Because it is a far superior language) is pass by value always by default. You can pass by reference by using the "ref" keyword in the parameter declaration and also when calling the function. change(ref x); static void change(ref int x){}
This is a tricky concept when you are learning programming languages like c ,etc,3 pages of gibberish text from the book solve ,hope you tube was there back in the 90's learning programming could be fun...thanks master!!!!
Thanks a lot. We have the exact topic on "comparative programming languages" class at UoPeople this week and the video clarified the distinction between passing by reference and value.
No, it does not. It explains the difference between a value type and a reference type, not the difference between passing by value and passing by reference. Java doesn't support passing by reference at all. Some people have posted links to other videos which explain it correctly. See this SO question: stackoverflow.com/questions/1068760/can-i-pass-parameters-by-reference-in-java
Thank you for this video. I didn't know what Pass by Reference meant before watching it to be honest. Apple's programming language, Swift, uses this a lot when dealing with UnsafeMutableRawPointers which makes sense. It makes me wonder if referencing and using the & operator actually causes less crashes than using other passing methods.
Pass by value (using primitives) you make a copy, and that is what gets messed with, without affecting the original. Pass by ref(using objects) you are messing with the object itself because it isn't being copied. You could use a copy constructor to make a "clone" of an object and mess with that one while preserving the original.
When you created 'particle other' is this ('other) a new instance of the Class particle? But it is only declared inside the boolean function? How does processing recognise the 'other.x' as an example of type 'particle' and how does it link it with P2?
"other" is just an argument of type Particle. Meaning you can only pass a Particle object in the overlaps method. It's like saying 'int x'. However, this time it's of type Particle instead of 'int'. Therefore, when you pass P2 in the overlaps method, processing checks whether P2 is a Particle object and finds that P2 matches the required argument type you specified (Particle) and recognizes it as the "Particle other". This is a wordy reply but I hope it helps.
edit: I think it has something to do with the " ' other' " question. tried to add a particle but... (read more!) Heyy, Big Up from Brasil!! thanks for everything!!! I tried to add one more particle and the overlap function doesn't apply fully to the first particle called(p2) . I mean, when it overlaps one of the particles, the mouseParticle (p2) goes red, and the other goes green, but when the p2 overlaps the other circle(p3), it doesn't go red, only the overlapped goes green but (p2) keeps black. i figured it out that the order of the functions does matter, and the "problem" occurs on the first function declared. example: p2.overlaps(p1); p2 overlaps(p3); in this case, when p2 overlaps p1, p2 is still black and p1 goes green, and p2 goes red normally when overlaps p3.... and if I change the order p2.overlaps(p3); p2.overlaps(p1); then p2 only goes red when overlaps p1... i just don't understand why.... mycode: Particle p1; Particle p2; Particle p3; void setup() { size(600,400); p1 = new Particle(100, 100, 50); p2 = new Particle (); p3 = new Particle(200, 200, 30);
I think this should fix the problem. It's a very small fix. The draw function is calling overlaps twice, so the second undoes the first's change to p2.col due to calling by reference (this is true no matter the order they are called). It's a simple fix, in the main program add the p2.col change to black each time, and in the class just remove the two.col = color(0) from the else statement, as that will update each time for the moving circle to be black when it overlaps one or the other circles...and you don't want that. Hope this helps. Here's the main program: void setup(){ size(600, 400); p1 = new Particle(100, 100, 50); p2 = new Particle(500, 200, 100); p3 = new Particle(500, 100, 50); } void draw(){ background(0); //reverts p2 back to black if not overlapping p2.col = color(0); //p2 reference passed into two, so when you change values of two //you also change values of p2 (by reference) p1.overlaps(p2); p3.overlaps(p2); //allows circle 2 to move with mouse p2.x = mouseX; p2.y = mouseY; //displays circles p2.display(); p1.display(); p3.display(); } Here's the class setup: class Particle { float x, y, r; color col; Particle(float tempX, float tempY, float tempR) { x = tempX; //xposition y = tempY; //yposition r = tempR; //radius col = color(0); //color } //checks to see if circles overlap void overlaps(Particle two){ float d = dist(x, y, two.x, two.y); if ( d < r + two.r){ col = color(255, 0, 0, 100); two.col = color(0, 0, 255); } else{ col = color(0); } } //draws particle void display() { stroke(255); fill(col); ellipse(x, y, r*2, r*2); } }
I've been trying to understand this for years...now I think I've got it. You input a value (50 in this vid) into a function. You want to do some operations on that value to obtain a result. ...but the result of the operation (100) is stored in a separate variable call val. ...and since anything in a function is local, only the input values (50) and the return value can be seen by the rest of the program. You want the return value, not the original input value. So you to pass the result back to the main program. Why couldn't you could just say "return val;"?
I feel like understanding value and reference types are fundamental to a programmer's understanding of async and multithreaded code. I find that programmers are often told not to use pass by reference without being explained it's to safely avoid bad habits in the future when passing by reference could lead to race conditions and non-threadsafe code.
7 лет назад
How a can do that when overlap the particles change color but when no overlap conserv the original color, i.e. if I use one constructor than have color as a input argument, how can return to this color when no overlap the particles?
How would I do this if they are two different classes instead of one? Also, the circle class is increasing in size and I want the square class to overlap only when the circle is bigger than a certain size? Thanks
So, do I understand correctly that, if I actually wanted to do the stuff the change function does, I need to change the return type from void to something like int or float then? there has to be a way to make change (x) return 100 and assign it to x again, otherwise, why would we do it?
+awsomeguy563 without using a function. so like this: x = x * 2; but if you wanted to print the value of x after it's been altered by the change function just put println(val); inside the function after val = val * 2;
Hi Daniel, sorry if this is a silly question, but I don't understand if change() {} is a function which is native to Processing, or a function created solely for this video for demonstration purposes. I haven't found change() in the Processing Reference. Thanks for the wonderful series, I've learned a lot from them!
I have another question: If Java (and processing) only pass by value (as explained by other people here), why does the thing Daniel does at the end, the changing of the color-values of the particles work? If it is only ever referencing to the original value? Thanks!
People who say that Java is only pass-by-value are splitting hairs and playing semantic games. Java has primitive types and reference types. Whenever you create an instance of an object, it gets stored as a reference type. Passing this reference to a function requires Java to copy that reference. This is what pass-by-reference means. People just like to be "right" about everything, so they say that because the reference is copied it's actually a pass-by-value, but the "value" being passed just happens to be a reference. It's just confusing nonsense.
How is this correct? I mean I've tested with String, Integer and an object I've created and still have not have not the same results, it is always pass by value with what I've tested. The argument object does not change, once the called method is finished. I have not yet seen any pass by reference in Java.
Hi, sir, I have a problem that i create 3 particles(particleC is moving when mouseX/Y is changing, others are created with random variables), so I write the code that just like you did in your video. However, my particle only change color when it overlays particleA but not change its color when it overlays particleB. How to fix this?
Can someone help me with thus please A method that takes an array of double. Return whether or not every element in the array appears in ascending order, begining with the SECOND element AND if the first element is equal to the sum of all elements beginning with the second element. You must use 1 loop at the most for this and recall the difficulty of checking equality with doubles that may contain math error.
OMG, i can't believe people teach this simple concept the wrong way all the time in all programming languages... First of all JAVA *always* passes *variables* by "copy" or "value". Note that i said ALWAYS. There is not such a thing as passing a *variable by reference* . People seem to confuse what "variable" means. A variable is a piece of memory that holds information. For primitive types / value types the variable memory directly contains the *value* . However for reference type variables (like classes) our variable only contains a reference to another memory address. The main difference between *passing* by value and *passing* by reference is that the content of the *variable* can't be changed when it's passed by value as its value / content is copied into a local parameter variable of the method when you call it. On the other hand when passing a *variable* by reference you can actually modify the *content of the variable* inside the method. Of course, as i said, *this does not exist in JAVA*. However this is possible in C#, C++, C, ... In C# when you define a method like this void SomeMethod(ref int someNumber) { someNumber++; } "someNumber" is *not* a normal local variable inside the method and when calling the method you do *not* copy the content of the variable, but instead you *pass a reference* to the *variable* itself. Methods like this have to be called like this: int x = 5; SomeMethod(ref x); print(x); This will print "6". We give our method a reference to the x variable location in memory so inside SomeMethod "x" and "someNumber" refer to the same memory location. Therefore changes to someNumber will affect the content of x. C and C++ allow to directly pass a pointer to a variable. However C# ref parameters are "safe" as you can't store ref parameters for "later use". But again JAVA does *not support passing a variable by reference* . See this SO question: stackoverflow.com/questions/1068760/can-i-pass-parameters-by-reference-in-java Passing a reference type variable by value does the same as with primitive type variables. It copies the *content* of the variable into the local parameter variable inside the method. Of course the content of a reference type variable is the *reference* . However you still pass that reference by value as you copy the reference from the source variable to the local parameter. So the content of the variable can't be changed. You can of course change the object that is referenced by the reference value.
Thank you for this. I have since learned several times over how I botched the explanation in this video. I'll hopefully find the time to correct someday.
I'm seeing this a lot in the comment section of this video, and it's causing a lot of confusion, so let me just say: Passing a reference by value _is_ passing by reference. That's how _all_ programming languages do it, and anyone who tells you otherwise is playing semantic games and making up unneeded concepts. Saying that copying the value of a reference type is "pass by value" is confusing. When you copy the reference to an object and pass it into a function, that _is_ passing it by reference. You are passing the reference to the object into the function.
Hi Dan, thank you so much for these in-depth and newbie friendly videos on OOP. My question1 is, beside multiple constructors for class, I wonder is there a way to build multiple constructors for methods: without parameter and with parameters. I tried to make two methods with difference on whether having parameter or not, and it works. is it an appropriate way to do it? My question2 is, can we split a class tab into multiple tabs, as more and more methods make it difficult to organize in just one tab? maybe I should just split this one class into two related two classes to organize the methods. But the drawbacks would be any fields shared by both classes have to be taken out to become global variables, which will make first tab look messy. well, in this case, I guess, I should keep one class as one class no matter how many methods inside it? or maybe object interactions can help? thanks a lot
+Kenny L 1) Indeed, this is called method "overloading". You can write a function with the same name, but different parameters and Java will know which one to call based on the arguments. 2) I wouldn't recommend splitting a class over multiple tabs as the start and end brackets could get out of place too easily. You could have a class store instances of other objects - this is probably the best solution for you.
I got it but I think you could make it better by using a much simpler method. Print one attribute value of the object. Pass the object to a method, and just do one thing to one attribute of that object and then print it again. Return from the method and print that attribute once more to show that the change is permanent..
I like very much your videos, thanks you. I have a doubt with this code. Why the Particle p3 do not behave the same with other Particles, p1 and p2; what is wrong here? Particle p1; Particle p2; Particle p3; void setup() { size(600,400); p1 = new Particle(100,220,50); p2 = new Particle(450,180,70); p3 = new Particle(); } void draw() { background(0);
Umm...I know it's been 3 years since this video but, could we continue this in depth Java tutorial please? Or are there other videos on the channel I am not seeing? You are a great teacher and I'm currently learning from codecademy and this is a great supplement to that.
This concept is very confusing to explain, especially to trainee devs even now being a senior dev for a fairly long time. Basically, there is no such thing in Java as pass-by-reference. As already pointed out, the primitive explanation is correct. Primitives reside on the stack. A copy of the value is passed to the method param and cannot be changed. Objects are stored in the heap. When the object reference is passed to the method param a copy is made on the stack which points to the same location to the original object on the heap. Therefore you can change the attributes of the object but the original version (memory reference) is not actually being changed, if that makes sense. Both references are just pointing to the data (values), its these values that are actually getting passed using pointers.
Java is always pass by value, as explained here : ruclips.net/video/BHtfb3lfc-g/видео.html
Yeah, I got this wrong, someday I'll try to remake this video to fix!
#TheCodingTrain Yeah but when I watch this in 2018 and it feels lime you're dead
This is misleading. The process described in the video you linked is the same as that described by Daniel.
Furthermore, saying that Java is always pass by value is semantic hairsplitting.
A variable holds a reference to an object. Copying that reference when passing it into a function is what passing by reference _means._ Calling it "pass by value" just because the reference is a value, and that value is copied is misleading and confusing because it conflates copying a primitive value with copying a reference value, which end up having _very_ different consequences for how the program runs.
APaleDot , think it like this. when you have a String variable assign to some literal and if you update/changes the same variable then the variable will point to a new address because String objects are immutable and the previous literal will still be in the heap with no reference variable to it; later that will be removed by garbage collector.
As we all know pass-by-value means we pass a copy of the data and the original data can’t be affected in the called fuction and pass-by-reference- means we pass the actual address to that data which can be affected in the called method.
Now when we say java is totally pass by value what does it mean? The correct sentence would be “java pass reference by value”. To keep it simple whenever you hear the term pass-by-value, remember a copy of the data is created and passed.
int a=5; //address 100
Integer b= 5; // address 15
if i pass ‘a’ to a function a copy will be created of the content and passed and if I passed ‘b’ a copy of reference will be passed (Remember, pass-by-value means passing a copy) to the function. Here is the trick if you change the content for ‘b’ inside the function, the original will ‘b’ will be unaffected. If java was really pass-by-reference then you can update the content of ‘b’ inside the function. And not to forget what I mentioned earlier regarding string objects that they are immutable and changing the variable content essentially means changing the reference of that variable. In Java all wrapper classes are immutable and that is why you will not b able to change the content of ‘b’(changes the reference).
Any data Pass-by-value to a function will remain unaffected no matter what you do with it inside the function. So java is not pass-by-reference for objects and pass-by-value for
primitives. It’s only pass-by-value .
@@TheCodingTrain .. you shouldn't clarify things to people, until it's clear for you. Confusing people could make them lose interest in what you teach.
3 minutes into the video and I understand more about this than my professors ever thought me. Amazing!
I had to quickly review pass-by-value/pass-by-reference and this was the most concise/clear explanation I've run into so far. Thank you!
Man, you explained something , that im struggling to get for weeks, just in 7 mins, and i got it completely.
Thank you very much u have a talent to teach!
Thank you 3 years and you did not fix it yet. You are making it frustrating to people who want to learn more.! Such a thoughtful person.
what a shame you dont have topics in java
you r a great teacher, and explain everything so good... very very good...
keep making new videos
Best video to clearly differentiate between PbV & PbR. Great work!
I was having so much trouble understanding this concept, dude just did that in 4:46 minutes, what a great teacher
I know you got lots of hate about Java stuff BUT you helped me understand the passing by reference stuff that I needed for C++ so thank you! :)
finally, this made sense, not the part where you were overlapping circles but the first part was perfect, I finally understand this concept. Thank you!
Finally, understood the difference between passing by value and passing by reference. Great content.
This is indeed one of the most clear and helpful tutorials that explain the difference between pass by value and pass by reference. Well done!
Thanks for brilliant explanation! But there is something I can't understand.
6:30 if fill(col); is under ellipse(x,y,r*2,r*2); the particle that changes color will be p1 rather than p2.
I can't figure out why..
Very nice tutorial, thank you. The tutorial hits the core of object-oriented programming. It would even better if there is a outline/summary at the beginning/end.
UGH. It feels like nobody online is able to explain this concept in a way I can understand it :(
Check out the videos of mycodeschool..I think that channel could!!
if we're talking about using 'pass by value' VS 'pass by reference' in a method, here's a TIP you can understand. "pass by reference uses ampersand -> '&' " . If you see that symbol like " public String changeColor(String &favColor ) ", immediately that's 'pass by reference' . Everything else is pass by value. How about that? : )
@@ownagejuice1394 except that this video is about Java, not C++.
C# (Because it is a far superior language) is pass by value always by default. You can pass by reference by using the "ref" keyword in the parameter declaration and also when calling the function.
change(ref x);
static void change(ref int x){}
Thank you so much. This is the first explanation I've found that actually helped rather than confusing me more
This is a tricky concept when you are learning programming languages like c ,etc,3 pages of gibberish text from the book solve ,hope you tube was there back in the 90's learning programming could be fun...thanks master!!!!
The Best video i have found about pass by value and pass by reference.Thank u so much, Best teacher ever
WTF why did everyone knew java as both pass by reference and pass by value earlier and later in life realized that its always pass by value !! :D
You've given very easy and understandable program in board
What a great teacher :)
Thanks a lot. We have the exact topic on "comparative programming languages" class at UoPeople this week and the video clarified the distinction between passing by reference and value.
No, it does not. It explains the difference between a value type and a reference type, not the difference between passing by value and passing by reference. Java doesn't support passing by reference at all. Some people have posted links to other videos which explain it correctly.
See this SO question:
stackoverflow.com/questions/1068760/can-i-pass-parameters-by-reference-in-java
Thank you for this video. I didn't know what Pass by Reference meant before watching it to be honest. Apple's programming language, Swift, uses this a lot when dealing with UnsafeMutableRawPointers which makes sense. It makes me wonder if referencing and using the & operator actually causes less crashes than using other passing methods.
Your a great teacher ever. Thank you so much for your efforts. 🏆🏆🏆🏆🏆
most entertaining tech guy by far
Pass by value (using primitives) you make a copy, and that is what gets messed with, without affecting the original.
Pass by ref(using objects) you are messing with the object itself because it isn't being copied.
You could use a copy constructor to make a "clone" of an object and mess with that one while preserving the original.
Best explanation in RUclips
Sir,I respect you.
Hi Dan, I am new to Processing, and your tutorials are excellent! Thank you so much for posting these!!
+Tokidoki Knitter glad to hear, thank you!
Java doesnt support pass by reference
Thank you for clarifying this topic.
When you created 'particle other' is this ('other) a new instance of the Class particle? But it is only declared inside the boolean function? How does processing recognise the 'other.x' as an example of type 'particle' and how does it link it with P2?
"other" is just an argument of type Particle. Meaning you can only pass a Particle object in the overlaps method. It's like saying 'int x'. However, this time it's of type Particle instead of 'int'. Therefore, when you pass P2 in the overlaps method, processing checks whether P2 is a Particle object and finds that P2 matches the required argument type you specified (Particle) and recognizes it as the "Particle other". This is a wordy reply but I hope it helps.
edit: I think it has something to do with the " ' other' " question.
tried to add a particle but... (read more!)
Heyy, Big Up from Brasil!!
thanks for everything!!!
I tried to add one more particle and the overlap function doesn't apply fully to the first particle called(p2) . I mean, when it overlaps one of the particles, the mouseParticle (p2) goes red, and the other goes green, but when the p2 overlaps the other circle(p3), it doesn't go red, only the overlapped goes green but (p2) keeps black.
i figured it out that the order of the functions does matter, and the "problem" occurs on the first function declared.
example:
p2.overlaps(p1);
p2 overlaps(p3);
in this case, when p2 overlaps p1, p2 is still black and p1 goes green, and p2 goes red normally when overlaps p3.... and if I change the order
p2.overlaps(p3);
p2.overlaps(p1);
then p2 only goes red when overlaps p1...
i just don't understand why....
mycode:
Particle p1;
Particle p2;
Particle p3;
void setup() {
size(600,400);
p1 = new Particle(100, 100, 50);
p2 = new Particle ();
p3 = new Particle(200, 200, 30);
}
void draw(){
background(0);
p2.overlaps(p3);
p2.overlaps(p1);
p2.x = mouseX;
p2.y = mouseY;
p1.display();
p2.display();
p3.display();
}
///
class Particle {
float x, y;
float r;
color col;
Particle() {
x = random(width);
y = random(height);
r = random(20, 50);
col = color(0, 100);
}
Particle(float tempX, float tempY, float tempR) {
x = tempX;
y = tempY;
r = tempR;
}
void overlaps(Particle other) {
float d = dist(x, y, other.x, other.y);
if (d < r + other.r) {
col = color(255, 0, 0, 100);
other.col = color(0, 255, 0, 100);
} else {
col = color(0, 100);
other.col = color (0, 100);
}
}
void display() {
stroke(255);
fill(col);
ellipse(x, y, r*2, r*2);
}
}
I think this should fix the problem. It's a very small fix. The draw function is calling overlaps twice, so the second undoes the first's change to p2.col due to calling by reference (this is true no matter the order they are called).
It's a simple fix, in the main program add the p2.col change to black each time, and in the class just remove the two.col = color(0) from the else statement, as that will update each time for the moving circle to be black when it overlaps one or the other circles...and you don't want that. Hope this helps.
Here's the main program:
void setup(){
size(600, 400);
p1 = new Particle(100, 100, 50);
p2 = new Particle(500, 200, 100);
p3 = new Particle(500, 100, 50);
}
void draw(){
background(0);
//reverts p2 back to black if not overlapping
p2.col = color(0);
//p2 reference passed into two, so when you change values of two
//you also change values of p2 (by reference)
p1.overlaps(p2);
p3.overlaps(p2);
//allows circle 2 to move with mouse
p2.x = mouseX;
p2.y = mouseY;
//displays circles
p2.display();
p1.display();
p3.display();
}
Here's the class setup:
class Particle {
float x, y, r;
color col;
Particle(float tempX, float tempY, float tempR) {
x = tempX; //xposition
y = tempY; //yposition
r = tempR; //radius
col = color(0); //color
}
//checks to see if circles overlap
void overlaps(Particle two){
float d = dist(x, y, two.x, two.y);
if ( d < r + two.r){
col = color(255, 0, 0, 100);
two.col = color(0, 0, 255);
}
else{
col = color(0);
}
}
//draws particle
void display() {
stroke(255);
fill(col);
ellipse(x, y, r*2, r*2);
}
}
you are truely amazing teacher you help me much thank you sir
Such a beautiful explanation. You are awesome. 😘
What an intuitive explanation! Many thanks!
Thanks for the explanation. Do you have any more JAVA tutorials? I looked through you other videos but I can only find JS tutorials :(
Too good ..!!! Very helpful video for java. 👌
This is helpful indeed
I never thought about that before
amazing ....my concepts are now clear
aP = new Particle ( x, y ); would be a good addition to this review showing the pass by value nature of Java
You are an amazing teacher!
His smiling nature is enough to understand things
You are cool sir. So is your explanation.
I've been trying to understand this for years...now I think I've got it.
You input a value (50 in this vid) into a function.
You want to do some operations on that value to obtain a result.
...but the result of the operation (100) is stored in a separate variable call val.
...and since anything in a function is local, only the input values (50) and the return value can be seen by the rest of the program. You want the return value, not the original input value. So you to pass the result back to the main program.
Why couldn't you could just say "return val;"?
Thank you so much, bearded Harry Potter 😃❤
I feel like understanding value and reference types are fundamental to a programmer's understanding of async and multithreaded code. I find that programmers are often told not to use pass by reference without being explained it's to safely avoid bad habits in the future when passing by reference could lead to race conditions and non-threadsafe code.
How a can do that when overlap the particles change color but when no overlap conserv the original color, i.e. if I use one constructor than have color as a input argument, how can return to this color when no overlap the particles?
Heyy youuuu i just wanna thanks to you 🥺 my Mind is going to brust bcz of this concept but not after this video🌻
Fantastic explanation, thank you!
how could you do the right side example the right way modyfing the argument of val?thanks
How would I do this if they are two different classes instead of one?
Also, the circle class is increasing in size and I want the square class to overlap only when the circle is bigger than a certain size?
Thanks
So, do I understand correctly that, if I actually wanted to do the stuff the change function does, I need to change the return type from void to something like int or float then? there has to be a way to make change (x) return 100 and assign it to x again, otherwise, why would we do it?
+Artur Karlov Yes, this is indeed correct!
awesome, thanks to you I feel like I am finally getting programming!
What is the way to change the value of x then?
+awsomeguy563 without using a function. so like this: x = x * 2; but if you wanted to print the value of x after it's been altered by the change function just put println(val); inside the function after val = val * 2;
This video has been very usefull for me thanks u man !
so cool your explanations ! great job.
Thanks so much!
Hi Daniel, sorry if this is a silly question, but I don't understand if change() {} is a function which is native to Processing, or a function created solely for this video for demonstration purposes. I haven't found change() in the Processing Reference. Thanks for the wonderful series, I've learned a lot from them!
just for this video!
When I play around with this concept it doesn't seem to apply to String or BigInteger...could someone explain why?
I have another question: If Java (and processing) only pass by value (as explained by other people here), why does the thing Daniel does at the end, the changing of the color-values of the particles work? If it is only ever referencing to the original value?
Thanks!
People who say that Java is only pass-by-value are splitting hairs and playing semantic games. Java has primitive types and reference types. Whenever you create an instance of an object, it gets stored as a reference type. Passing this reference to a function requires Java to copy that reference. This is what pass-by-reference means. People just like to be "right" about everything, so they say that because the reference is copied it's actually a pass-by-value, but the "value" being passed just happens to be a reference. It's just confusing nonsense.
How is this correct? I mean I've tested with String, Integer and an object I've created and still have not have not the same results, it is always pass by value with what I've tested. The argument object does not change, once the called method is finished. I have not yet seen any pass by reference in Java.
Hi, sir, I have a problem that i create 3 particles(particleC is moving when mouseX/Y is changing, others are created with random variables), so I write the code that just like you did in your video. However, my particle only change color when it overlays particleA but not change its color when it overlays particleB. How to fix this?
try pC.overlaps(pA) && pC.overlaps(pB) since it's pC that's moving.
Can someone help me with thus please
A method that takes an array of double. Return whether or not every element in the array appears in ascending order, begining with the SECOND element AND if the first element is equal to the sum of all elements beginning with the second element. You must use 1 loop at the most for this and recall the difficulty of checking equality with doubles that may contain math error.
OMG, i can't believe people teach this simple concept the wrong way all the time in all programming languages...
First of all JAVA *always* passes *variables* by "copy" or "value". Note that i said ALWAYS. There is not such a thing as passing a *variable by reference* . People seem to confuse what "variable" means. A variable is a piece of memory that holds information. For primitive types / value types the variable memory directly contains the *value* . However for reference type variables (like classes) our variable only contains a reference to another memory address. The main difference between *passing* by value and *passing* by reference is that the content of the *variable* can't be changed when it's passed by value as its value / content is copied into a local parameter variable of the method when you call it.
On the other hand when passing a *variable* by reference you can actually modify the *content of the variable* inside the method. Of course, as i said, *this does not exist in JAVA*. However this is possible in C#, C++, C, ...
In C# when you define a method like this
void SomeMethod(ref int someNumber)
{
someNumber++;
}
"someNumber" is *not* a normal local variable inside the method and when calling the method you do *not* copy the content of the variable, but instead you *pass a reference* to the *variable* itself. Methods like this have to be called like this:
int x = 5;
SomeMethod(ref x);
print(x);
This will print "6". We give our method a reference to the x variable location in memory so inside SomeMethod "x" and "someNumber" refer to the same memory location. Therefore changes to someNumber will affect the content of x. C and C++ allow to directly pass a pointer to a variable. However C# ref parameters are "safe" as you can't store ref parameters for "later use".
But again JAVA does *not support passing a variable by reference* . See this SO question:
stackoverflow.com/questions/1068760/can-i-pass-parameters-by-reference-in-java
Passing a reference type variable by value does the same as with primitive type variables. It copies the *content* of the variable into the local parameter variable inside the method. Of course the content of a reference type variable is the *reference* . However you still pass that reference by value as you copy the reference from the source variable to the local parameter. So the content of the variable can't be changed. You can of course change the object that is referenced by the reference value.
Thank you for this. I have since learned several times over how I botched the explanation in this video. I'll hopefully find the time to correct someday.
This explanation was lost on my...I'm not a textual learner normally so i got a little lost on this vs your explanation in the video.
I'm seeing this a lot in the comment section of this video, and it's causing a lot of confusion, so let me just say: Passing a reference by value _is_ passing by reference. That's how _all_ programming languages do it, and anyone who tells you otherwise is playing semantic games and making up unneeded concepts. Saying that copying the value of a reference type is "pass by value" is confusing. When you copy the reference to an object and pass it into a function, that _is_ passing it by reference. You are passing the reference to the object into the function.
Thank you sir! Great examples!
Oh my god. you explain so well
Great explanation
I really like your chanel. Thanks a lot and cheers 🍻
Hi Dan, thank you so much for these in-depth and newbie friendly videos on OOP.
My question1 is, beside multiple constructors for class, I wonder is there a way to build multiple constructors for methods: without parameter and with parameters.
I tried to make two methods with difference on whether having parameter or not, and it works. is it an appropriate way to do it?
My question2 is, can we split a class tab into multiple tabs, as more and more methods make it difficult to organize in just one tab?
maybe I should just split this one class into two related two classes to organize the methods. But the drawbacks would be any fields shared by both classes have to be taken out to become global variables, which will make first tab look messy. well, in this case, I guess, I should keep one class as one class no matter how many methods inside it? or maybe object interactions can help?
thanks a lot
+Kenny L 1) Indeed, this is called method "overloading". You can write a function with the same name, but different parameters and Java will know which one to call based on the arguments. 2) I wouldn't recommend splitting a class over multiple tabs as the start and end brackets could get out of place too easily. You could have a class store instances of other objects - this is probably the best solution for you.
Thanks Dan
Can't seem to find anywhere what "change()" does.. Can someone help?
I got it but I think you could make it better by using a much simpler method. Print one attribute value of the object. Pass the object to a method, and just do one thing to one attribute of that object and then print it again. Return from the method and print that attribute once more to show that the change is permanent..
you confused me ! duahh !! but hats off liked your concept and java thingi !! :] thanks man !
Great video! Thanks
Lmao I don't get the point of Pass by Value still, cause it just returns the same number. What's the point?
perfect explanation
Thanks a lot for these videos, really helps me ;)
+Arod L. I'm so glad to hear!
Can we use &p instead of ap?
Hallo is There a site were i van download these sketches?
thank you very much , nicely explained
I'm so glad to hear, thank you!
I like very much your videos, thanks you. I have a doubt with this code. Why the Particle p3 do not behave the same with other Particles, p1 and p2; what is wrong here?
Particle p1;
Particle p2;
Particle p3;
void setup() {
size(600,400);
p1 = new Particle(100,220,50);
p2 = new Particle(450,180,70);
p3 = new Particle();
}
void draw() {
background(0);
p3.overlaps(p1);
p3.overlaps(p2);
p1.display();
p2.display();
p3.display();
p3.x = mouseX;
p3.y = mouseY;
}
class Particle {
color col;
float x;
float y;
float r;
Particle() {
x = random(width);
y = random(height);
r = random(30, 100);
col = color(200);
}
Particle(float tempX, float tempY, float tempR) {
x = tempX;
y = tempY;
r = tempR;
col = color(200);
}
void display() {
stroke(250);
strokeWeight(2);
fill(col);
ellipse(x, y, r*2, r*2);
}
void overlaps(Particle other) {
float d = dist(x, y, other.x, other.y);
if (d < r + other.r) {
col = color( 255, 0, 0, 200);
other.col = color(0, 255, 0, 200);
} else {
col = color(0, 200);
other.col = color(0, 200);
}
}
}
Thanks you for answering, i am so excited for that and that you take time to help others. I will to ask at the forum, thanks very much.
there's no pointers concept in java?
hey sir i wanna ask is pass by value equal to pass by copy that you stated ?
Yes, it is.
Thank you nong!!
thats a great understanding ... thanksyou a lot . :)
java pass by value, always!
Thank you so much
thank you so freaking much
This guy looks like the professor from Money Heist
Thank you
awesome. thank you and see you later alligator
Umm...I know it's been 3 years since this video but, could we continue this in depth Java tutorial please? Or are there other videos on the channel I am not seeing? You are a great teacher and I'm currently learning from codecademy and this is a great supplement to that.
This video should be updated.
It's the same in C++ ?
Question or statement? Worded as statement but ends in question mark.. I'd like to know the answer to this as well?
wQuestion.Because i know C++ has beside references pointers as well.
yes the idea is the same
is it possible to control whether i want to send a primitive data type by reference like in C++
THANK YOU ! ! ! 🤓👍
hi, what is this programming langage ?
processing.org (Java)
and what about python ?
omg!! amazing it was..
This concept is very confusing to explain, especially to trainee devs even now being a senior dev for a fairly long time.
Basically, there is no such thing in Java as pass-by-reference. As already pointed out, the primitive explanation is correct. Primitives reside on the stack. A copy of the value is passed to the method param and cannot be changed.
Objects are stored in the heap. When the object reference is passed to the method param a copy is made on the stack which points to the same location to the original object on the heap. Therefore you can change the attributes of the object but the original version (memory reference) is not actually being changed, if that makes sense. Both references are just pointing to the data (values), its these values that are actually getting passed using pointers.
Lol make sense... after watching it again ... after 1 year
gerrard pique!
aP will point to P and not to underlying value so far i know
you look like you planning to rob a bank. bank of spain, perhaps? kiddin, thank u for this
int x=10;
int y;
y=x;
print x;
----------------
x=10
pass by ref.