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 :: java find duplicates in array 
Java :: java to kotlin tutorial 
Java :: char to charsequence in java 
Java :: java lambda 
Java :: file handling in java 
Java :: android list to string 
Java :: call activity method from adapter 
Java :: java abstract method 
Java :: mp3 player java 
Java :: how to get last index of array in java 
Java :: java remove double spaces 
Java :: how to add a number to the ascii value of a char in java 
Java :: @runwith junit 5 
Java :: configuration spring boot dependency for freemarker configuration 
Java :: get sum of int array and return string 
Java :: write ajva program to vheck if anumber is less than 20 and greater than 5. It generates the exception out of range otherwise. If the number is within the range , then it displays the square of that number. 
Java :: mambalam srardham online booking 
Sql :: mysql set safe mode off 
Sql :: oracle change nls_date_format permanently 
Sql :: select all fields in soql 
Sql :: safe update mode in mysql 
Sql :: list all triggers in sql server 
Sql :: display all databases 
Sql :: show databases in sql server 
Sql :: to date oracle with time 
Sql :: mysql update part of string 
Sql :: how to start mysql 
Sql :: how to unlock table in mysql 
Sql :: sql skip the first row 
Sql :: oracle stop job 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =