1

Sorry if this is a silly question but I'm kinda new in java programming.

I have a boolean stored in a Thread and I don't know if it is true or false.

I have a method setBooleanTrue() that set the boolean true, but what if the boolean is already true? It will set the boolean again to true?

For this reason I want to ask you if I should add the following control:

if(!testboolean)
    testboolean=true;

Thanks.

Clara
  • 23
  • 2
  • what are you trying to achieve, you want to toggle the boolean? – Amer Qarabsa May 11 '17 at 12:22
  • 2
    No. What should be the problem if `true` is overridden with `true`? – Jens May 11 '17 at 12:22
  • @AmerQarabsa to avoid an unnecessary set. – Clara May 11 '17 at 12:24
  • @Jens No problem at all. Just to avoid an unnecessary set. – Clara May 11 '17 at 12:24
  • Don't worry about the previous value, it will dissapear without pain. And the condition would probably take more process than the actual setting – AxelH May 11 '17 at 12:25
  • @Clara then you have an unnecessary condition check – Jens May 11 '17 at 12:26
  • I think that it takes mor effort (processing) to check first and then set the boolean, rather then just overwrite it – kism3t May 11 '17 at 12:26
  • @AxelH that was indeed my question. So the condition would take more time than the actual setting? – Clara May 11 '17 at 12:27
  • @Clara probably, no evidence here, but that is an unnecessary check for a primitive value. With instance that could take more resource, I would rethink this but here, don't bother. Your code would grow really fast if you start to check every value in a setter – AxelH May 11 '17 at 12:29
  • @AxelH, would be the same for a reference type like enum? – Clara May 11 '17 at 12:56
  • Only the reference would change since enums are constants so don't worry. I was thinking about a value that would need a constructor call, without expressing the idea completly. Once the instance is already created, don't bother, the hardest as been done. – AxelH May 11 '17 at 13:24

2 Answers2

1

If you're interested in the amount of time saved by adding the conditional, have a look at this:

Below I have written out both versions of your code, and the corresponding generated java bytecode.

 boolean b = true;
 if(!b)
      b=true;

 0: iconst_1
 1: istore_1
 2: iload_1
 3: ifne
 6: iconst_1
 7: istore_1
 8: return


 boolean b = true;
 b=true;

 0: iconst_1
 1: istore_1
 2: iconst_1
 3: istore_1
 4: return

As you can see, the direct reassignment of the boolean generates less bytecode.

At first, this may seem like the answer. Unfortunately, it isn't that simple. See runtime environments do this fancy thing called branch prediction. If a line of code is being run continuously, the JVM will attempt to "predict" the outcome of the conditional.

This can be a really good thing, or a really bad thing. Your expected values of the boolean, the number of times it is to be checked in rapid succession and whether it is likely to remain a certain value through many calls will determine whether you should add the conditional statement or not.

Community
  • 1
  • 1
CraigR8806
  • 1,584
  • 13
  • 21
-1

when you assign a value to any variable, the variables current value doesn't get evaluated, it just sets the variable to the value you want. So there's no need to check if a bool is true or false before setting it to true. if its true it will stay true, if its false it will become true.

testfile
  • 2,145
  • 1
  • 12
  • 31