Passing variables to Java functions

NamyaLG
3 min readJun 14, 2023

There are 8 primitive types in Java, apart from these types, everything else is an object in Java. In the view of memory management, there is a stack for every thread and a common shared heap space.

Primitive data types are — byte , short , int , long , float , double , boolean and char.

The primitive types are stored on the stack, i.e. name of the variable and value, while for objects, a reference to it is stored on the stack. The object is created in the heap space.

It is important to note Java always follows Pass By Value. Based on whether it is a Primitive or not, the behaviour varies.

Primitives

Primitives are passed by value, i.e. a copy of the variable is created on the stack; after the function concludes, goes out of scope.

To illustrate the same, consider the snippet of code (According to the document, System.identityHashCode(obj) static method returns the identity hashcode of the object, which is a non-negative integer between [0, 2³¹-1]. The identity hashcode of a null object is 0)

public class Primitive{

static void changeValueOfPrimitive(int x){
x = 100;
System.out.println("The hashcode of x outside the function is " + System.identityHashCode(x));
}


public static void main(String args[]){
int x = 10;

System.out.println("\n------------------------------------------------\n");
System.out.println("The hashcode of x outside the function is " + System.identityHashCode(x));
System.out.println("Value of x before the function is " + x);
System.out.println("\n------------------------------------------------\n");

changeValueOfPrimitive(x);
System.out.println("Value of x after the function is " + x);
System.out.println("\n------------------------------------------------\n");
}
}
The output of changing the value of a Primitive in a function

Non-Primitives

Consider the case of a Non-Primitive in the following snippet of code —

public class NonPrimitive{

static void changeValueOfNonPrimitive(WithOneIntegerAndFloat objToFunction){
objToFunction.integer = 100;
objToFunction.decimal = 89.293;
System.out.println("The hashcode of objToFunction inside the function is " + System.identityHashCode(objToFunction));
}


public static void main(String args[]){
WithOneIntegerAndFloat obj = new WithOneIntegerAndFloat(10, 19.67);

System.out.println("\n------------------------------------------------\n");
System.out.println("The hashcode of obj outside the function is " + System.identityHashCode(obj));
System.out.println("Value of instance variable integer and decimal before the function is " + obj.integer + " " + obj.decimal);
System.out.println("\n------------------------------------------------\n");

changeValueOfNonPrimitive(obj);

System.out.println("Value of instance variable integer and decimal after the function is " + obj.integer + " " + obj.decimal);
System.out.println("\n------------------------------------------------\n");
}
}

class WithOneIntegerAndFloat {
int integer;
double decimal;

WithOneIntegerAndFloat(int integer, double decimal){
this.integer = integer;
this.decimal = decimal;
}
}
The output of changing the value of a NonPrimitive in a function

Concluding, it can be seen that in the case of NonPrimitives, a reference to the object is passed as function arguments, i.e. a copy of the reference is passed to the function, not a copy of the object. Thus, any modification to the object in the function will reflect in the original.
A super useful reference in understanding memory management in Java — https://youtube.com/playlist?list=PL58GoKYDWT_G2qq9YpwIuS2ZGrWZMZ4sq

--

--

NamyaLG

Tech-enthusiast | Runner_for_life | #NoHumanIsLimited