Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing boolean by reference

 
karl holmgren
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seem to have some problems passing Boolean object by reference. Variable
successful does not become false in Main() even though I alter it to that in retrieveNext.

Boolean successful = new Boolean(true);

while(successful.booleanValue() && counter<15){
System.out.println(TH2.retrieveNext(successful));
counter++;
}

public double retrieveNext(Boolean successful){
if(channels[currCh].size()>currSampl+1){
currSampl+=1;
successful=true;
return (Double)(channels[currCh]).get(currSampl);
}else{
successful=false;
return -1;
}
}

Thanks!
[ October 11, 2005: Message edited by: karl holmgren ]
 
Julien Grenier
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Boolean object are immutable so you can't change the value of it.
so since you are returning -1 you could consider check if the returned value is -1 and remove the boolean logic.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Julien pointed out, primitive wrappers (like Boolean) are immutable. So when you say successful = false, you're actually reassigning the reference to a new Boolean instance (using Java 1.5's autoboxing).

But in addition to this, you need to recognize that method arguments are copies -- local to the scope of the method. So within your retrieveNext method, reassigning the variable "successful" only changes the object that the local variable is pointing to. However, the "successful" variable outside of the method will continue to point to the original object.

(To verify this, experiment using combinations of "successful" and "this.successful" within the method.)
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java only has "pass by value", not "pass by reference." However, there are other techniquest to achieve the same effect. The most obvious is to return a boolean. In this situation, it doesn't seem to make sense because retrieveNext() already returns a double. So what does the boolean value represent? It looks like it indicates whether the method succeeds based on some criterion. In such as situation, you might consider throwing an exception. This is particularly ideal if "failure" indicates that the class has entered an invalid state that should not occur. This usually means that something is wrong in your code that is causing somethign to happen that should never happen in the finished product. I think exception handling is an ideal way to deal with "success" and "failure" in such a situation. Another suggestion is to have a member field that indicates success or not. However, this is only helpful to other methods in the same class. Otherwise you need to add a getter method. I think an exception would be a much cleaner way to deal with this than a member variable.

I hope this help. If anything is unclear, please let me know so I can answer any questions you may have.

Layne
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this

[code]
Boolean [] successful = new Boolean [] {new Boolean(true)};

while(successful[0].booleanValue() && counter < 15){
System.out.println(TH2.retrieveNext(successful[0]));
counter++;
}

public double retrieveNext(Boolean [] successful){
if(channels[currCh].size() > currSampl+1){
currSampl+=1;
successful[0]=true;
return (Double)(channels[currCh]).get(currSampl);
}else{
successful[0]=false;
return -1;
}
}
[code]

Now you are still passing by value since that is all java knows. But this solves your problem by using an array. A wrapper would work just the same.

Hope you spend time now to comprehend completely that Java does not pass by reference. Java does not even assign by reference. there is no 'by reference' in java anywhere.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic