Problem with pass by reference for array.

Hi,

I am having requirement to process an array of string. which will populate two arrays.

Now, there can or can not be value for two result arrays.

Issue is I am having one main method and I am calling one method to parse the array.

I am passing total three arrays to this method. One is the souceArray and other two arrays will be used to store the results.

Now, if I am setting any one as null in the method, its not getting reflected to the main method.

Below is the sample code,

class MyClass{

publicstaticvoid main(String[] args){

String[] sendTo3 ={"-4738","-4735","-34","-30","-3","-22","-25","-432"};//for -3 + all minus

int[] myList1 =newint[sendTo3.length];

int[] myList2 =newint[sendTo3.length];

String flagVal ="n";

System.out.println(" before call myList1 : "+myList1);

System.out.println(" before call myList1 : "+myList2);

flagVal = parseRecipientList(sendTo3,myList1,myList2);

System.out.println(" after parseRecipientList call myList1 : "+myList1);

System.out.println(" after parseRecipientList call myList1 : "+myList2);

System.out.println(" For -3 + All minus is null1 "+(myList1 ==null));

System.out.println(" For -3 + All minus is null2 "+(myList2 ==null));

printValues(" For -3 + All minus : "+flagVal,sendTo3,myList1,myList2);

System.out.println(" after printValues myList1 : "+myList1);

System.out.println(" after printValues myList1 : "+myList2);

}

privatestaticvoid printValues(String comment,String[] selectedValues,int[] teamIdList,int[] tpIdList){

System.out.println("=========Start :: "+comment+"===========");

System.out.println(" in printValues myList1 : "+teamIdList);

System.out.println(" in printValues myList1 : "+tpIdList);

System.out.println(" Value at first index for selectedValues : "+selectedValues[0]);

if((teamIdList !=null) && teamIdList[0] != 0){

for(int cnt=0;cnt < teamIdList.length;cnt++){

if(teamIdList[cnt] == 0)

break;

System.out.println("Element at teamIdList ["+(cnt+1)+"] "+ teamIdList[cnt]);

}

}else

System.out.println(" teamIdList array is null\t\t ");

System.out.println("\t\t ");

if((tpIdList !=null) && tpIdList[0] != 0){

for(int cnt=0;cnt < tpIdList.length;cnt++){

if(tpIdList[cnt] == 0)

break;

System.out.println("Element at tpIdList ["+(cnt+1)+"] "+ tpIdList[cnt]);

}

}else

System.out.println(" tpIdList array is null\t\t ");

System.out.println("=========End :: "+comment+"===========");

}

privatestatic String parseRecipientList(String[] selectedValues,int[] teamIdList,int[] tpIdList){

String groupFlag ="N";

int toId = 0;

int teamCnt = 0;

int tpIdCnt = 0;

boolean allScoringTeamFlag =false;

System.out.println(" in method tpIdList "+tpIdList );

System.out.println(" in method teamIdList "+teamIdList);

for(int cnt = 0 ;cnt < selectedValues.length;cnt++){

toId = Integer.parseInt(selectedValues[cnt]);

if(toId == 0)

continue;

if(toId == -1){

groupFlag ="N";

selectedValues[0] = selectedValues[cnt];

break;

}elseif(toId == -2){

groupFlag ="N";

allScoringTeamFlag =false;

selectedValues[0] = selectedValues[cnt];

break;

}elseif(toId == -3){

groupFlag ="G";

allScoringTeamFlag =true;

}elseif(!allScoringTeamFlag && toId < -3){

groupFlag ="G";

teamIdList[teamCnt++] = toId;

}

elseif(toId > 0){

groupFlag ="G";

tpIdList[tpIdCnt++]=toId;

}

}//end of for loop

System.out.println(" before allScoringTeamFlag tpIdList "+tpIdList );

System.out.println(" before allScoringTeamFlag teamIdList "+teamIdList);

if(allScoringTeamFlag)

{

System.out.println(" inside allScoringTeamFlag ~~~~ ");

if(tpIdList[0] == 0)

{

System.out.println(" inside allScoringTeamFlag + tpIdLis[0]==0 ~~~~ ");

tpIdList =null;

groupFlag ="N";

selectedValues[0] ="-3";

teamIdList =null;

}else{

System.out.println(" inside else of allScoringTeamFlag + tpIdLis[0]==0 ~~~~ ");

teamIdList[0] = -3;

}

}//end of allScoringTeamFlag

System.out.println(" tpIdList isNull1 "+(tpIdList ==null));

System.out.println(" teamIdList isNull2 "+(teamIdList==null));

System.out.println(" tpIdList "+tpIdList );

System.out.println(" teamIdList "+teamIdList);

return groupFlag;

}//end of parseRecipientList

}

[9227 byte] By [Ritesh.Patela] at [2007-10-2 13:54:56]
# 1

This is the result for above program. As given in bold text,

its setting referece to null in local method call, but in main method it is again showing the origional referece value.

Any pointer why its happening, ideally it should set null as this is pass by reference.

Am I missing something ?

before call myList1 : [I@1ead1120

before call myList1 : [I@1ead111f

in method tpIdList [I@1ead111f

in method teamIdList [I@1ead1120

before allScoringTeamFlag tpIdList [I@1ead111f

before allScoringTeamFlag teamIdList [I@1ead1120 inside allScoringTeamFlag ~~~~

inside allScoringTeamFlag + tpIdLis[0]==0 ~~~~

tpIdList isNull1 true

teamIdList isNull2 true

tpIdList null

teamIdList null

after parseRecipientList call myList1 : [I@1ead1120

after parseRecipientList call myList1 : [I@1ead111f

For -3 + All minus is null1 false

For -3 + All minus is null2 false

=========Start :: For -3 + All minus : N===========

in printValues myList1 : [I@1ead1120

in printValues myList1 : [I@1ead111f

Value at first index for selectedValues : -3

Element at teamIdList [1] -4738

Element at teamIdList [2] -4735

Element at teamIdList [3] -34

Element at teamIdList [4] -30

tpIdList array is null

=========End :: For -3 + All minus : N===========

after printValues myList1 : [I@1ead1120

after printValues myList1 : [I@1ead111f

Ritesh.Patela at 2007-7-13 11:58:20 > top of Java-index,Java Essentials,Java Programming...
# 2

Java is pass by value, always. This includes array references.

Pass-by-value

- When an argument is passed to a function, the invoked function gets a copy of the original value.

- The local variable inside the method declaration is not connected to the caller's argument; any changes made to the values of the local variables inside the body of the method will have no effect on the values of the arguments in the method call.

- If the copied value in the local variable happens to be a reference (or "pointer") to an object, the variable can be used to modify the object to which the reference points.

Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory.... The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.

-- James Gosling, et al., The Java Programming Language, 4th Edition

yawmarka at 2007-7-13 11:58:20 > top of Java-index,Java Essentials,Java Programming...