2021年2月6日星期六

java: If I assign a variable the same value as just before, does it change the memory or does JIT recognize this?

For example:

class Main {     public hasBeenUpdated = false;       public updateMain(){       this.hasBeenUpdated = true;       /*       alternative:       if(!hasBeenUpdated){         this.hasBeenUpdated = true;       }       */     }       public persistUpdate(){       this.hasBeenUpdated = false;     }  }    public instance = new main()  instance.updateMain()  instance.updateMain()  instance.updateMain()  

Does instance.hasBeenUpdated get updated 3 times in memory?

The reason I ask this is because I hoped to use a boolean("hasBeenUpdated") as a flag, and this could theoretically be "changed" many, many times, before I call "instance.persistUpdate()".

Does the JVM's JIT see this and perform an optimization?

https://stackoverflow.com/questions/66083380/java-if-i-assign-a-variable-the-same-value-as-just-before-does-it-change-the-m February 07, 2021 at 08:25AM

没有评论:

发表评论