//********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;
??
Java does not have the concept of Reference Variables. We cannot access the memory location where the data is stored in Java.
Reference 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
Pass by Reference does not create a copy of the data items. So, it is faster.
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& p, int& 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; }
Java does not have the concept of Reference Variables. We cannot access the memory location where the data is stored in Java.
Reference variables
a=a+b; b=a-b; a=a-b;
Primitive variables are variables that are not objects and carry primitive values like numbers, boolean etc. The primitive data types in java are:intbytefloatcharlongbooleanshortdouble
Pass by Reference does not create a copy of the data items. So, it is faster.
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
Literals are the values assigned to variables. int num = 10; Here 10 is the integer literal.
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.
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.
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& p, int& 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; }
No , Java does not support call by reference.
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).