8.6: Pass by Value vs. Pass by Reference - Processing Tutorial

Поділитися
Вставка
  • Опубліковано 22 лип 2024
  • This video covers passing arguments into a function and what happens when you pass an object vs. a primitive value.
    Book: Learning Processing A Beginner's Guide to Programming, Images,Animation, and Interaction
    Chapter: 8
    Official book website: learningprocessing.com/
    Twitter: / shiffman
    annotations added: click here to subscribe
    Help us caption & translate this video!
    amara.org/v/QbxH/
    📄 Code of Conduct: github.com/CodingTrain/Code-o...

КОМЕНТАРІ • 175

  • @AnjaniKumar
    @AnjaniKumar 7 років тому +81

    Java is always pass by value, as explained here : ua-cam.com/video/BHtfb3lfc-g/v-deo.html

    • @TheCodingTrain
      @TheCodingTrain  6 років тому +37

      Yeah, I got this wrong, someday I'll try to remake this video to fix!

    • @miroko8477
      @miroko8477 5 років тому +3

      #TheCodingTrain Yeah but when I watch this in 2018 and it feels lime you're dead

    • @APaleDot
      @APaleDot 5 років тому +27

      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.

    • @mehrosenasir3966
      @mehrosenasir3966 5 років тому +5

      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 .

    • @Farsmezan
      @Farsmezan 4 роки тому +6

      @@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.

  • @zenryucds747
    @zenryucds747 5 років тому +31

    3 minutes into the video and I understand more about this than my professors ever thought me. Amazing!

  • @RoundCorner93
    @RoundCorner93 7 років тому +6

    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!

  • @mariosasic4251
    @mariosasic4251 4 роки тому +6

    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!

  • @tarkmenguc8031
    @tarkmenguc8031 2 роки тому

    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.

  • @benesranur
    @benesranur 5 років тому +1

    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!

  • @astghmelqumyan8303
    @astghmelqumyan8303 6 років тому +13

    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

  • @resai17
    @resai17 Рік тому +2

    Best video to clearly differentiate between PbV & PbR. Great work!

  • @piyushpatil2668
    @piyushpatil2668 3 роки тому

    I was having so much trouble understanding this concept, dude just did that in 4:46 minutes, what a great teacher

  • @xiangli3388
    @xiangli3388 6 років тому +1

    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.

  • @jbei9981
    @jbei9981 7 років тому

    Thank you so much. This is the first explanation I've found that actually helped rather than confusing me more

  • @apeiy2107
    @apeiy2107 5 років тому +14

    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! :)

  • @saltanatkokjalova9097
    @saltanatkokjalova9097 2 роки тому

    Finally, understood the difference between passing by value and passing by reference. Great content.

  • @angrybunny1993
    @angrybunny1993 6 років тому

    The Best video i have found about pass by value and pass by reference.Thank u so much, Best teacher ever

  • @TheEnglishQuail
    @TheEnglishQuail 7 років тому +41

    UGH. It feels like nobody online is able to explain this concept in a way I can understand it :(

    • @jayantjolly
      @jayantjolly 6 років тому

      Check out the videos of mycodeschool..I think that channel could!!

    • @ownagejuice1394
      @ownagejuice1394 6 років тому +5

      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? : )

    • @niche9015
      @niche9015 5 років тому +8

      @@ownagejuice1394 except that this video is about Java, not C++.

  • @demizhang8917
    @demizhang8917 4 роки тому +1

    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..

  • @sentenced7259
    @sentenced7259 7 років тому +1

    Thanks for the explanation. Do you have any more JAVA tutorials? I looked through you other videos but I can only find JS tutorials :(

  • @Julian-mc7cm
    @Julian-mc7cm 6 років тому

    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

  • @RS-so7ke
    @RS-so7ke 7 років тому +13

    Java doesnt support pass by reference

  •  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?

  • @dtaspinar
    @dtaspinar 6 років тому

    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.

    • @Bunny99s
      @Bunny99s 6 років тому

      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

  • @queenkaine3619
    @queenkaine3619 3 роки тому

    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.

  • @aura-audio
    @aura-audio 5 років тому +2

    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.

  • @pedromateus9575
    @pedromateus9575 5 років тому

    how could you do the right side example the right way modyfing the argument of val?thanks

  • @flashyvirtouso7423
    @flashyvirtouso7423 4 роки тому

    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!!!!

  • @STDrepository
    @STDrepository 5 років тому +1

    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){}

  • @annalisetrite7281
    @annalisetrite7281 4 роки тому

    When I play around with this concept it doesn't seem to apply to String or BigInteger...could someone explain why?

  • @tokidokiknitter1293
    @tokidokiknitter1293 8 років тому

    Hi Dan, I am new to Processing, and your tutorials are excellent! Thank you so much for posting these!!

    • @TheCodingTrain
      @TheCodingTrain  8 років тому

      +Tokidoki Knitter glad to hear, thank you!

  • @applesmith4012
    @applesmith4012 2 роки тому +1

    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!

  • @georgianadan9380
    @georgianadan9380 4 роки тому

    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.

  • @leifan9740
    @leifan9740 5 років тому

    What an intuitive explanation! Many thanks!

  • @johanpicocrie7798
    @johanpicocrie7798 3 роки тому

    Hallo is There a site were i van download these sketches?

  • @kevinbenavides92
    @kevinbenavides92 7 років тому +2

    Thank you for clarifying this topic.

  • @fubaralakbar6800
    @fubaralakbar6800 5 років тому +1

    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;"?

  • @oorschot5626
    @oorschot5626 3 роки тому

    Can't seem to find anywhere what "change()" does.. Can someone help?

  • @monicamonica6975
    @monicamonica6975 5 років тому +1

    You've given very easy and understandable program in board

  • @user-be1wl6sb7q
    @user-be1wl6sb7q 3 роки тому

    Can we use &p instead of ap?

  • @surajpoudel6646
    @surajpoudel6646 5 років тому +1

    Such a beautiful explanation. You are awesome. 😘

  • @Dyslexic_Neuron
    @Dyslexic_Neuron 3 роки тому +1

    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

  • @creative.coderie
    @creative.coderie 7 років тому +44

    What a great teacher :)

  • @familyzarazaga9906
    @familyzarazaga9906 7 років тому +3

    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?

    • @creative.coderie
      @creative.coderie 7 років тому +1

      "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.

    • @lucasvisionvjbitar
      @lucasvisionvjbitar 7 років тому +1

      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);
      }
      }

    • @Philanthrophxyz
      @Philanthrophxyz 7 років тому +1

      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);
      }
      }

  • @monikachuttani6645
    @monikachuttani6645 5 років тому

    This is helpful indeed
    I never thought about that before

  • @anujmishra4013
    @anujmishra4013 8 років тому +1

    you are truely amazing teacher you help me much thank you sir

  • @shanedetsch
    @shanedetsch Рік тому

    aP = new Particle ( x, y ); would be a good addition to this review showing the pass by value nature of Java

  • @NobleAbsinthe
    @NobleAbsinthe Рік тому

    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.

  • @travisc2746
    @travisc2746 6 років тому

    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?

    • @TheChanclasVerdes
      @TheChanclasVerdes 6 років тому

      try pC.overlaps(pA) && pC.overlaps(pB) since it's pC that's moving.

  • @simm5622
    @simm5622 2 роки тому

    Your a great teacher ever. Thank you so much for your efforts. 🏆🏆🏆🏆🏆

  • @saiashwinkumarc4221
    @saiashwinkumarc4221 4 роки тому

    there's no pointers concept in java?

  • @arturkarlov3000
    @arturkarlov3000 8 років тому +1

    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?

    • @TheCodingTrain
      @TheCodingTrain  8 років тому +1

      +Artur Karlov Yes, this is indeed correct!

    • @arturkarlov3000
      @arturkarlov3000 8 років тому

      awesome, thanks to you I feel like I am finally getting programming!

    • @awsomeguy563
      @awsomeguy563 7 років тому

      What is the way to change the value of x then?

    • @creative.coderie
      @creative.coderie 7 років тому

      +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;

  • @owningyourfinances
    @owningyourfinances 6 років тому

    Thank you sir! Great examples!

  • @DerKever
    @DerKever 6 років тому

    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!

    • @APaleDot
      @APaleDot 5 років тому

      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.

  • @annalisetrite7281
    @annalisetrite7281 4 роки тому

    Fantastic explanation, thank you!

  • @juanmanuelmillansanchez8165
    @juanmanuelmillansanchez8165 4 роки тому

    This video has been very usefull for me thanks u man !

  • @jagdeepsinghuppal8310
    @jagdeepsinghuppal8310 5 років тому +1

    Too good ..!!! Very helpful video for java. 👌

  • @harshalwaghmare440
    @harshalwaghmare440 2 роки тому

    Heyy youuuu i just wanna thanks to you 🥺 my Mind is going to brust bcz of this concept but not after this video🌻

  • @abora4766
    @abora4766 7 років тому +9

    Sir,I respect you.

  • @user-si5zq8sc4b
    @user-si5zq8sc4b 7 років тому

    hey sir i wanna ask is pass by value equal to pass by copy that you stated ?

  • @aaronaaronaaron5922
    @aaronaaronaaron5922 4 роки тому +1

    I really like your chanel. Thanks a lot and cheers 🍻

  • @rimshanadeem6759
    @rimshanadeem6759 7 років тому

    amazing ....my concepts are now clear

  • @colorfulcodes
    @colorfulcodes 4 роки тому

    You are an amazing teacher!

  • @rabinbk5014
    @rabinbk5014 5 років тому +2

    You are cool sir. So is your explanation.

  • @tarajano1980
    @tarajano1980 7 років тому

    so cool your explanations ! great job.

  • @arrowmid
    @arrowmid 8 років тому

    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!

  • @bradweir5579
    @bradweir5579 2 роки тому

    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.

  • @aryan_karim
    @aryan_karim 5 років тому

    most entertaining tech guy by far

  • @samstoyroom
    @samstoyroom 5 років тому

    Best explanation in UA-cam

  • @CyberAbyss007
    @CyberAbyss007 7 років тому

    Great video! Thanks

  • @kennyl7542
    @kennyl7542 8 років тому

    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

    • @TheCodingTrain
      @TheCodingTrain  8 років тому

      +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.

    • @kennyl7542
      @kennyl7542 8 років тому

      Thanks Dan

  • @kaz9070
    @kaz9070 8 років тому

    Thanks a lot for these videos, really helps me ;)

  • @ARNafi-ev2xr
    @ARNafi-ev2xr 7 років тому

    Great explanation

  • @torus151
    @torus151 3 роки тому

    His smiling nature is enough to understand things

  • @mohsankayani
    @mohsankayani 7 років тому

    you confused me ! duahh !! but hats off liked your concept and java thingi !! :] thanks man !

  • @md.safikhasan8517
    @md.safikhasan8517 6 років тому

    thats a great understanding ... thanksyou a lot . :)

  • @juancarlosrestreposaldarri2386
    @juancarlosrestreposaldarri2386 7 років тому

    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);
    }
    }
    }

    • @juancarlosrestreposaldarri2386
      @juancarlosrestreposaldarri2386 7 років тому

      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.

  • @turkkis3955
    @turkkis3955 5 років тому

    perfect explanation

  • @mnava19
    @mnava19 7 років тому

    thank you very much , nicely explained

  • @PaulBaird
    @PaulBaird 6 років тому

    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..

  • @JustGotHeated
    @JustGotHeated 7 років тому +6

    Lmao I don't get the point of Pass by Value still, cause it just returns the same number. What's the point?

  • @liveyounganastasia
    @liveyounganastasia 2 роки тому

    Oh my god. you explain so well

  • @fart_m0nster_420
    @fart_m0nster_420 5 років тому

    No wonder my PVector function acted so weird... thankyou!

  • @stefolino
    @stefolino 6 років тому

    hi, what is this programming langage ?

  • @the_basketball_is_the_life
    @the_basketball_is_the_life 2 роки тому

    Thank you so much

  • @qtSoph
    @qtSoph 2 роки тому

    thank you so freaking much

  • @piotrjazdzyk253
    @piotrjazdzyk253 2 роки тому

    This is The Professor!

  • @evanahannah2895
    @evanahannah2895 Рік тому +2

    Thank you so much, bearded Harry Potter 😃❤

  • @helloworld5794
    @helloworld5794 3 роки тому

    Thank you nong!!

  • @therealantonym8039
    @therealantonym8039 6 років тому

    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.

  • @ShinobiEngineer
    @ShinobiEngineer 4 роки тому

    THANK YOU ! ! ! 🤓👍

  • @sharma93anks
    @sharma93anks 2 роки тому

    omg!! amazing it was..

  • @farooqawan693
    @farooqawan693 4 роки тому

    Thank you

  • @MegaBdboy
    @MegaBdboy 7 років тому +2

    It's the same in C++ ?

    • @tylertyler82
      @tylertyler82 7 років тому

      Question or statement? Worded as statement but ends in question mark.. I'd like to know the answer to this as well?

    • @MegaBdboy
      @MegaBdboy 7 років тому

      wQuestion.Because i know C++ has beside references pointers as well.

    • @kalarlapastos4824
      @kalarlapastos4824 7 років тому

      yes the idea is the same

    • @aniekanumoren6088
      @aniekanumoren6088 7 років тому

      is it possible to control whether i want to send a primitive data type by reference like in C++

  • @armanx2
    @armanx2 3 роки тому

    aP will point to P and not to underlying value so far i know

  • @Bunny99s
    @Bunny99s 6 років тому +14

    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.

    • @TheCodingTrain
      @TheCodingTrain  6 років тому +6

      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.

    • @Luke-eg1gn
      @Luke-eg1gn 5 років тому

      Exactly right. There is no such thing as pass by reference in Java. You can pass variables by reference/address in languages like C and Go with the use of Pointers ( * & ) or in the Perl language variables are passed by address by default, but never in Java.

    • @Vercinaigh
      @Vercinaigh 5 років тому

      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.

    • @APaleDot
      @APaleDot 5 років тому

      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.

  • @rangerlone8092
    @rangerlone8092 3 роки тому +3

    This guy looks like the professor from Money Heist

  • @SSoundtruck
    @SSoundtruck 6 років тому

    awesome. thank you and see you later alligator

  • @Cornellie
    @Cornellie 3 роки тому

    This video should be updated.

  • @ianmakeedits
    @ianmakeedits 3 роки тому

    you look like you planning to rob a bank. bank of spain, perhaps? kiddin, thank u for this

  • @FlySilky
    @FlySilky 5 років тому

    Help.

  • @shanisarathchndra5284
    @shanisarathchndra5284 5 років тому +2

    java pass by value, always!

  • @amrelhadidy6800
    @amrelhadidy6800 3 роки тому

    ty