answersLogoWhite

0


Best Answer

//********Shivram singh Rathour Lucknow Gpl************

call by reference or call by value in java?I am programming in Java for quite sometimes now, but I never got stuck with the problem I am facing now.

Suppose I send a list to a function like:

List list = new ArrayList<Integer>() //code 1 in some class say Class1

//assign some numbers to 'list'

Now if I call another class method:

methodCustom(list)

which is defined as

void methodCustom(List list1) //code 2 in another class say Class2

then changing list1 in methodCustom i.e in code 2 of class 2 changes list from code1 of class 1. Weird!! I cant really understand what is happening. So I guess it is sending the reference.. M I correct? if so then what should I do if I only want to send the values? is there anything like deep copying and shallow copying?

kindly give me what is best way to send the array by value.

Along with this how can I get sublist from a list with separate pointers, I dont want my original list to be changed if I change the sublist.

So what is happening here in these cases?

Case1: List sl2 = list.subList(10,20);

Case2: List sl2 = new ArrayList<Integer>(list.subList(10,20));

Case3: List sl2 = new ArrayList(list.subList(10,20));

so can I copy array like:

for using it as call by value:

list=new ArrayList<Integer>(listPrev):

for using call by reference:

list=listPrev;

??

User Avatar

Wiki User

βˆ™ 11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 16y ago

There is no explicit pass by value and pass by reference in Java.

Internally Java does a pass by value of primitive data types like int, float etc and a pass by reference of object data types like business models or collections. This is Java's explicit behavior and we cannot influence Java to explicitly use pass by value or reference based on our wish.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 9y ago

There is no concept of pass by reference in Java; everything is passed by value.

When we say "pass by value" what we really mean is that the function receives a copy of the value that was passed to it. In the case of primitive types, such as int and float, this is fairly self-explanatory. If we assign the value 42 to an integer named x and then pass x to a function that accepts an integer argument named y, then y is assigned the value of x. Thus x and y have the same value (42) but are completely independent of each other; any changes made to y by the function will not be reflected in x.

Non-primitive types, such as objects and arrays, are also passed by value, however the value that is passed is not the object's value but rather a reference to the object itself! This is not the same as pass by reference, however, because the reference is passed by value. That is, the function receives a copy of the reference, not the reference itself.

Consider the following:

Foo f = new Foo();

The variable f refers to a Foo object. The object and the variable that refers to that object are actually separate entities. That is, the variable f might reside at address 42 while the object might reside at address 666. The value of f is therefore 666 because that's the address of the object it is currently referring to. Let's suppose we have the following function:

public void x (Foo y) {

// ...

}

If we were to pass f to this function, we'd actually be passing the value 666 which would then be assigned to the variable y. Now both f and y will refer to the same object and any operations we perform on y will now be reflected in f. To most people, this is what is meant by pass by reference, but that's not strictly true. To understand why, consider the following definition of function x:

public void x (Foo y) { y = new Foo();

}

Let's suppose this new Foo object resides at address 999, thus y now holds the value 999. But what about f? If f really were passed by reference then f would now hold the value 999 as well. But it does not; it still holds the value 666. Thus any operations we now perform on y will no longer be reflected in f because they now refer to completely separate objects.

In a language that supports pass by reference, like C++, we simply wouldn't be able to re-assign y to another address. With pass by reference semantics, a reference is simply an alias (an alternate name) for an already existing object. If we we wish to change the object being referred to we have to use pass by value semantics instead. In C++ we would need to pass a pointer variable (by value) not a reference, just as we would in C. So in this respect, Java actually has more in common with C than it does C++, even though Java has no concept of a pointer variable. However, unlike Java, if we wish to enable reference semantics upon the pointer itself then we have to pass a pointer-to-pointer by value instead. This places much of the responsibility of calling conventions upon the programmer but that is not a concern for Java programmers given there is only one calling convention, call by value.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 15y ago

All primitive types (boolean, char, byte, short, int, long, float, double) plus String objects are passed by value in Java. All other objects are passed by reference.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 15y ago

Java only supports pass by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: In Java are variables passing by values or by reference?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What are the differences between C and Java reference variables?

Java does not have the concept of Reference Variables. We cannot access the memory location where the data is stored in Java.


In java all variables declared using a class are?

Reference variables


What are primitive variables?

Primitive variables are variables that are not objects and carry primitive values like numbers, boolean etc. The primitive data types in java are:intbytefloatcharlongbooleanshortdouble


Why pass by reference is quicker than pass by values in java?

Pass by Reference does not create a copy of the data items. So, it is faster.


Swap two number using pointer?

The only way to swap two values using call by value semantics is to pass pointer variables by value. A pointer is a variable that stores an address. Passing a pointer by value copies the address, the value of the pointer, not the pointer itself. By passing the addresses of the two values to be swapped, you are effectively passing those values by reference. Both C and C++ use pass by value semantics by default, however C++ also has a reference data type to support native pass by reference semantics. By contrast, Java uses pass by reference semantics by default. In C, to swap two variables using pass by value: void swap (int* p, int* q) { int t = *p; *p = *q; *q = t; } In C++, to swap two variables using pass by reference: void swap (int&amp; p, int&amp; q) { std::swap (p, q); } Note that C++ is more efficient because std::swap uses move semantics; there is no temporary variable required to move variables. With copy semantics, a temporary is required. However, with primitive data types, there is a way to swap values without using a temporary, using a chain of exclusive-or assignments: void swap (int* p, int* q) { *p^=*q^=*p^=*q; }

Related questions

What are the differences between C and Java reference variables?

Java does not have the concept of Reference Variables. We cannot access the memory location where the data is stored in Java.


In java all variables declared using a class are?

Reference variables


Java program to swap values of 2 variables?

a=a+b; b=a-b; a=a-b;


What are primitive variables?

Primitive variables are variables that are not objects and carry primitive values like numbers, boolean etc. The primitive data types in java are:intbytefloatcharlongbooleanshortdouble


Why pass by reference is quicker than pass by values in java?

Pass by Reference does not create a copy of the data items. So, it is faster.


What is Literal in java?

Literals are the values assigned to variables. int num = 10; Here 10 is the integer literal.


Can object extends?

objects are instances of class only and moreover objects are object reference variables extension of variables is meaningless so objects in java can't be extend


What is meant by Java Lang nullpointerexeption?

The expression is termed for an error in the Java Script software provided by Oracle. It means that one of the variables passing is null, but the code still tries to use it.


Explain reference variable and how it is different from pointer variable?

In JAVA, all variables are reference variables, and there are no pointer variables. Even though the platform may implement them as pointers, they are not available as such. In C, no variables are reference variables. They are a C++ enhancement. In C++ a reference variable is syntactically the same as a pointer variable, except that the use of the indirection operator (*) is implicit. You do declare reference variables slightly differently than pointer variables but, once you do so, they can be treated as non-pointer variables. Reference variables also cannot be redefined once they have been initialized to point to some object. They are const. Structurally, there is no difference between a pointer variable and a reference variable. They are both still pointers. The compiler just makes it easier to treat reference variables and non-pointer variables the same way.


Swap two number using pointer?

The only way to swap two values using call by value semantics is to pass pointer variables by value. A pointer is a variable that stores an address. Passing a pointer by value copies the address, the value of the pointer, not the pointer itself. By passing the addresses of the two values to be swapped, you are effectively passing those values by reference. Both C and C++ use pass by value semantics by default, however C++ also has a reference data type to support native pass by reference semantics. By contrast, Java uses pass by reference semantics by default. In C, to swap two variables using pass by value: void swap (int* p, int* q) { int t = *p; *p = *q; *q = t; } In C++, to swap two variables using pass by reference: void swap (int&amp; p, int&amp; q) { std::swap (p, q); } Note that C++ is more efficient because std::swap uses move semantics; there is no temporary variable required to move variables. With copy semantics, a temporary is required. However, with primitive data types, there is a way to swap values without using a temporary, using a chain of exclusive-or assignments: void swap (int* p, int* q) { *p^=*q^=*p^=*q; }


What are the different parameter passing methods used by c plus plus?

Pass by value, constant value, reference and constant reference. Pass by value is the default in C++ (pass by reference is the default in Java).


Does java support call by reference?

No , Java does not support call by reference.