简体   繁体   中英

Difference between assigning a boolean variable a value and assigning a result of a comparison in Java

My collegues passed me some code, and there are problems when I run the code. Here is some part of it

boolean purchased = false;
conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
stmt = conn.createStatement();
String sql = new StringBuilder().... // some query

rs = stmt.executeQuery(sql);
while (rs.next()) {
    //some code
    purchased = rs.getInt("purchased") == 1;
    print(" purchased:" + purchased);
}

This always printed purchased as false. I changed that part to this:

while (rs.next()) {
    //some code
    if(rs.getInt("purchased") == 1) purchased = true;
    print(" purchased:" + purchased);
}

Now it works perfectly.

note: my ResultSet can have only 1 record. And in DB purchased is bit type, so there can be no other values than 0/1.

What is the difference between those two ways of writing ? as I know both should work the same way.

What is the difference between those two ways of writing ? as I know both should work the same way.

No, definitely not. In your first piece of code, if rs.getInt("purchased") returns a value other than 1, you assign a value of false .

In your second piece of code, once purchased has been set to true , it stays true for the rest of the loop.

So suppose your first row has a value of 1 for purchased , and your second row has a value of 2, then in your first piece of code the variable will become true then false , but in your second piece of code the variable will be set to true in the first iteration and then not changed in the second iteration.

Actually it's this code that's equivalent to your colleague's code:

while (rs.next()) {
    // some code
    if(rs.getInt("purchased") == 1) 
        purchased = true;
    else purchased = false;
    print(" purchased:" + purchased);
}

Now they'll behave the same since purchased is not set to true forever once it's set.

The difference is that once the variable becomes true you are never changing it to false. You need to determine which is correct, based on the contract of the method, or the expected behaviour.

It doesn't seem at all likely that your version is correct.

How many records are in ResultSet and corresponding values?

It would not have made a difference without while loop ( if first record returns value as 1 ) or if ResultSet has only one record with value as 1.

while loop in first code might be setting false value in second iteration or so. So at the exit time of while loop, purchased value being carried is false . In second way, you are setting purchased value to true when value is 1 and that is not again getting reset to false ( because purchased value is not getting reset for other rows of ResultSet).

Hope it helps !!

I agree with Jon Skeet that in your first answer the value changes on each iteration of the while loop, but in the second piece of code the value of the boolean variable does not change every time.

I don't know your requirement but I think you should also add the else part in code as well to change the boolean the value of rs.getInt("purchased") other than 1 , as in your existing code you only make the boolean variable true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM