Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java pass by reference

// Java is always pass-by-value. When passing a reference variable, 
// a copy of the value of the pointer was passed, not the object itself. 

import java.util.Arrays;
class Dog {
    public String name;
    public Dog() {}     //default constructor
    public Dog(String name) {this.name = name;}
}
public class Main {
    public static void update_obj(Dog myDog) {
        myDog.name = "Changed";
    }
    public static void update_obj_v2(Dog myDog) {
        myDog = new Dog();
        myDog.name = "Changed v2";
    }
    public static void update_int(int i) {i++;}
    public static void update_Integer(Integer i) {i++;}
    public static void update_String_to_lower(String str) {str.toLowerCase();}
    public static void update_array(String[] ary) {ary[0] = "Changed";}

    public static void main(String[] args) {

        // ---------- Custom mutable objects, arrays, changed --------------------------------
        Dog max = new Dog("Max");
        update_obj(max);
        System.out.println("Dog's name is now: " + max.name);   //Changed

        Dog fido = new Dog("Fido");
        Dog fido_alias = fido;
        update_obj_v2(fido);
        System.out.println("Dog's name is now (v2): " + fido.name);     // Fido
        System.out.println("Fido object address has not changed (v2): " + String.valueOf(fido == fido_alias));  // True

        String[] fruits = {"Apple", "Orange"};
        update_array(fruits);
        System.out.println(Arrays.toString(fruits));    // [Changed, Orange]

        // ---------- Primitives, Immutable objects, NOT changed ------------------------------
        int i = 1;
        System.out.println("int before update: " + i); // 1
        update_int(i);
        System.out.println("int after update: " + i);  // 1

        Integer j = 1;
        System.out.println("Integer j before update: " + j); // 1
        update_Integer(j);
        System.out.println("Integer j after update: " + j);  // 1

        Integer k = 2;
        k++;
        System.out.println(k);      // 3

        String word = "SDSE";
        System.out.println("String before update: " + word);    //SDSE
        update_String_to_lower(word);
        System.out.println("String after update: " + word);     //SDSE
    }
}
Comment

is java pass by value or pass by reference

Both basic data types and Objects in java are pass by value. 
The difference is the value held by Objects in Java is their reference.
So the answer is: Java is always pass by value.
Comment

PREVIOUS NEXT
Code Example
Java :: set array length java 
Java :: java "-" 
Java :: what is deserialization in java 
Java :: join strings in java 
Java :: javafx 
Java :: java eth 
Java :: java firebase syncrhonous data read 
Java :: how does plus works in Java 
Java :: how to call the main method in java 
Java :: how to choose an element in a array list Java 
Java :: add checkbox box rows in tabulator 
Java :: factorial recursion java 
Java :: android java how to show progressdialog in fragment 
Java :: java method overloading 
Java :: how to repeat string in java 
Java :: ex: java loop 
Java :: spring secutiry urls redirecting 
Java :: textfield invisible java 
Java :: string to integer online 
Java :: trémaux’ methode 
Java :: viewresolver in spring boot 
Java :: how to use asynx task to make api connection java 
Java :: jtable fill panel 
Java :: conexion a mysql java 
Java :: import txt.xz file to android studio app 
Java :: how to get data from array list to set java 
Java :: check if char is letter 
Java :: Java Shuffling Using shuffle() 
Java :: Algorithms - count 
Java :: overwrite confguration value spring 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =