Home > Java > javaTutorial > body text

Java Call by Value

WBOY
Release: 2024-08-30 15:33:58
Original
945 people have browsed it

In any programming language, including Java, when we call a function and pass parameters as values instead of objects or pointers, we refer to it as “call by value.” The specific Java implementation, where pointers are not explicitly used, treats this as “call by value.” In this scenario, the function receives a copy of the variable’s value stored in memory as its argument.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

The syntax of “call by value” is used in all languages and is more or less similar.

Function: Function_name( parameter1, parameter2)
{
Int variable_name1;
Int variable_name2;
}
Copy after login

Here, function parameters are passed as a value rather than objects.

How Call by Value works in Java?

“Call by value” allocates a data variable to the memory location. The data associated with it can be stored in this memory location. However, manipulating the data within the same memory region after the initial value assignment is not supported unless the variable is destroyed. For example, here:

Int value=5;
Copy after login

Examples

Here are the following examples mentioned below.

Example #1

The below example explains how data is passed using value to a function named addition(). The addition() function will take data as a parameter and will give out manipulated data after adding 200 to it, as we can see in the function definition. But since we are using value here in every function, including the printing function, the value of the “input” variable remains unchanged.

Code:

public class Main {
int input=20;
// The below function will manipulate the data passed to it as parameter value.
void addition(int input){
input=input+200;
}
public static void main(String args[])
{
Main t_var=new Main();
System.out.println("before change "+t_var.input);
t_var.addition(1000);
// Here we pass 500 value instead of any reference.
System.out.println("after change "+t_var.input);
}
}
Copy after login

Output:

Java Call by Value

Example #2

The below example has a function named “multiply.” This function takes two parameter values and then multiplies these parameters in the functions to provide the final output. Here since we have a new memory byte allocated to store an integer so the value will be successfully stored and displayed on the output screen by the print function, unlike in the previous case.

Code:

public class Main
{
public static void main(String[] args)
{
int a = 30;
int b = 45;
System.out.println("The values we have inputted are: a = " + a + " and b = " + b);
System.out.println("");
multiply(a, b);
System.out.println("Here we are checking that if we pass parameters by value then what will be the product of multiplication of two values.");
}
public static void multiply(int a, int b)
{
System.out.println("Before multiplying the parameters, a = " + a + " b = " + b);
int product = a*b;
System.out.println("After multiplying the parameters, product = " + product);
}
}
Copy after login

Output:

Java Call by Value

Conclusion

“Call by Value” is a significant concept used in programming languages regardless of the specific language being used. Be it JAVA, C, C++, python, or any other, every language uses functions that take one or more parameters to provide a result. “Call by reference” uses an object rather than the value of the variable itself. We use “call by reference” in dynamic programming since it creates an object of the variable.

The above is the detailed content of Java Call by Value. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!