java_buzz
java_buzz

Reputation: 67

debugger behavior in if, else if, else

We have an application on Java 6 running on Weblogic application server 11. I am trying to debug an issue. My code is

if(a==0){
    //Do  A
}
else if(a==1){
    // Do B
}
else{
    // do C
}

My problem is that even if my if block is true and line 2 is executed still the debugger will go to line 8 in else block. The code on my machine and on server are synched, so no mismatch. Please help me finding what may be wrong with my code.

Upvotes: 2

Views: 1208

Answers (3)

James Cotter
James Cotter

Reputation: 808

Shouldn't it be:

if(a==0)
...
else if(a==1)
...
else
...

Upvotes: 0

ssedano
ssedano

Reputation: 8432

We need to know what does the // Do C.

If there is an assignation:

if (a == 0) {
      b = 1;
} else  if (a == 1) {
      b = 0;
}
else {
      b = null;
}

a typical optimization that some compilers do would be:

b = null;
if (a == 0) {
    b = 1;
} else if (a == 1) {
    b = 0;
}

And that is the bytecode executed. Let alone if the code is compiled to native by a JIT compiler. So depending on your debug client and jdk you could see crazy current instruction lines. But it is fine as long as the state at the end of the block is the expected.

Sometimes the executions needs to close the statement. So it goes to the closing.

Make sure that no static values is assigned to a and if so, that is recompiled. Assignations of static are linked in compile time.

Upvotes: 1

Basic
Basic

Reputation: 26766

don't forget that = is an assignment and == is a comparison - in your case, you're assigning the value and then checking the result of the assignment, NOT performing a comparison.

Simply change

if(a=0){
    //Do A
}
else if(a=1){
    // Do B

to

if(a==0){
    //Do A
}
else if(a==1){
    // Do B

Upvotes: 0

Related Questions