Learn Java Tutorial for Beginners (Video), Part 49: Passing by Value

Several people have asked me to create a video on passing by value vs. passing by reference (which Java doesn't implement), so here it is. We'll take a look at exactly how arguments are passed to methods in Java, and what it means for developers.

When the video is running, click the maximize button in the lower-right-hand corner to make it full screen. Don't forget to change to HD also if the video appears fuzzy.



Code for this tutorial:

App.java:

 
public class App {

    
    public static void main(String[] args) {
        App app = new App();
        
        //===========================================
        
        int value = 7;
        System.out.println("1. Value is: " + value);
        
        app.show(value);
        
        System.out.println("4. Value is: " + value);
        
        //===========================================
        System.out.println();
        
        Person person = new Person("Bob");
        System.out.println("1. Person is: " + person);
        
        app.show(person);
        
        System.out.println("4. Person is: " + person);
    }
    
    public void show(int value) {
        System.out.println("2. Value is: " + value);
        
        value = 8;
        
        System.out.println("3. Value is: " + value);
    }
    
    public void show(Person person) {
        System.out.println("2. Person is: " + person);
        
        
        
        person = new Person("Mike");
        person.setName("Sue");
        
        System.out.println("3. Person is: " + person);
        
    }
    
    
}

 

Person.java:

 

public class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + "]";
    }
}